最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

arrays - How can I trigger onwheel event up and down effect in javascript? - Stack Overflow

matteradmin2PV0评论

I want to trigger a function or loop when onwheel scroll up or scroll down. Please see my snippet.

let demo = document.querySelector('#demo');
let c = 0;
window.onwheel = function() {
  c ++;
  demo.innerHTML = c;
}
body{
height: 300vh;

}
h1{
position: fixed;
text-align: center;
width: 100vw;
}
<h1 id="demo" > Hello World </h1>

I want to trigger a function or loop when onwheel scroll up or scroll down. Please see my snippet.

let demo = document.querySelector('#demo');
let c = 0;
window.onwheel = function() {
  c ++;
  demo.innerHTML = c;
}
body{
height: 300vh;

}
h1{
position: fixed;
text-align: center;
width: 100vw;
}
<h1 id="demo" > Hello World </h1>

I need when it will scroll up the number should be increment and when scroll down number should be decrements. but it's just increment the number, I need js clear solution. Thanks.

Share Improve this question asked Jan 28, 2020 at 14:50 NIKHIL CHANDRA ROYNIKHIL CHANDRA ROY 1,08713 silver badges18 bronze badges 2
  • possible duplicate of stackoverflow./questions/1795734/… – mccainz Commented Jan 28, 2020 at 14:54
  • You can use addEventListener for scroll events. Please check the documentation, you’ll find a plete example : developer.mozilla/en-US/docs/Web/API/Document/scroll_event – Kokodoko Commented Jan 28, 2020 at 14:55
Add a ment  | 

4 Answers 4

Reset to default 4

You can use wheel event listener and get the direction with event.deltaY :

let demo = document.querySelector('#demo');
let c = 0;

window.addEventListener('wheel', function(event) {
  if (event.deltaY < 0) {
    console.log('scrolling up');
    if (c == 0) { // no negative values
      demo.innerHTML = 0;
    } else {;
      c--;
      demo.innerHTML = c;
    }
  } else if (event.deltaY > 0) {
    console.log('scrolling down');
    //if (c != 0) {
    c++;
    demo.innerHTML = c;
    // }
  }
});
body {
  height: 300vh;
}

h1 {
  position: fixed;
  text-align: center;
  width: 100vw;
}
<h1 id="demo"> Hello World </h1>

window.onwheel = function(event) {
  if (event.deltaY > 0) {
    // down
  } else {
    // up
  }
}

You could also just remember window.pageYOffset and check if you actually scrolled down (must be greater than remembered value)

let demo = document.querySelector('#demo');
let c = 0;
let last_scroll= window.pageYOffset;
window.onwheel = function() {
  if(window.pageYOffset >= last_scroll && last_scroll != 0){
    c ++;
    demo.innerHTML = c;
  }
  last_scroll = window.pageYOffset;
}
body{
height: 300vh;

}
h1{
position: fixed;
text-align: center;
width: 100vw;
}
<h1 id="demo" > Hello World </h1>

 var demoHeight = demo.offsetHeight; 
 demo.onscroll = function(){}
Post a comment

comment list (0)

  1. No comments so far