简体   繁体   English

如何检查是否有人滚动到带有 jquery 的元素

[英]How to check if someone has scrolled to an element with jquery

I need to know when a user has scrolled to a certain element with an id.我需要知道用户何时滚动到具有 id 的某个元素。

This is what I've tried:这是我试过的:

$(window).on('load', function() {
  let elembestellen = document.querySelector('#bestellen');
  var bestellen = elembestellen.getBoundingClientRect().top;
  let eleminformatie = document.querySelector('#informatie');
  var informatie = eleminformatie.getBoundingClientRect().top;
  let eleminspiratie = document.querySelector('#inspiratie');
  var inspiratie = eleminspiratie.getBoundingClientRect().top;
  let elemreviews = document.querySelector('#reviews');
  var reviews = elemreviews.getBoundingClientRect().top;

  $(window).scroll(function(){
    w = Math.floor( $(window).scrollTop());

    if(window.outerWidth > 767) {
      // console.log(bestellen);

      if(  (w > parseInt(bestellen))  && (w < parseInt(informatie))     ){
        console.log('#bestellen reached');
      }

      if(  (w > parseInt(bestellen))  && (w > parseInt(informatie)) && (w < parseInt(inspiratie))    ){
        console.log('#informatie reached');
      }

      // if(  (w > parseInt(bestellen))  && (w > parseInt(informatie)) && (w > parseInt(inspiratie)) && (w < parseInt(reviews))   ){
      //   console.log('inspiratie');
      // }

      // if(w > parseInt(informatie)){
      //   console.log('informatie');
      // }

    }
  });
});

The problem is my console logs #bestellen reached at the correct time when I load the page all the way scrolled to the top.问题是当我加载页面一直滚动到顶部时,我的控制台日志#bestellen reached在正确的时间到达。 But if I scroll down a bit, and refresh the page, the logged #bestellen reached does not appear at the right moment again.但是,如果我向下滚动一点并刷新页面,则到达的记录#bestellen reached不会再次出现在正确的时间。 It appears way to early.看来还为时过早。

How can I make sure the logs only fire when the element with the correct id has been reached on the page, no matter what part of the page is loaded?无论页面的哪个部分已加载,我如何才能确保只有在页面上到达具有正确 id 的元素时才会触发日志?

I have modified your code.我修改了你的代码。 Try this one.试试这个。

$(window).on('load scroll', function() {
    let elembestellen = document.querySelector('#bestellen');
    var bestellen = elembestellen.getBoundingClientRect().top;
    let eleminformatie = document.querySelector('#informatie');
    var informatie = eleminformatie.getBoundingClientRect().top;
    let eleminspiratie = document.querySelector('#inspiratie');
    var inspiratie = eleminspiratie.getBoundingClientRect().top;
    let elemreviews = document.querySelector('#reviews');
    var reviews = elemreviews.getBoundingClientRect().top;
  
      w = $(window).scrollTop();
  
      if(window.outerWidth > 767) {
        // console.log(bestellen);
  
        if(  (w > parseInt(bestellen))  && (w < parseInt(informatie))     ){
            eleminformatie.style.backgroundColor="transparent";
            elembestellen.style.backgroundColor="red";
          console.log('#bestellen reached');
        }
  
        if(  (w > parseInt(bestellen))  && (w > parseInt(informatie)) && (w < parseInt(inspiratie))    ){
            elembestellen.style.backgroundColor="transparent";
            eleminformatie.style.backgroundColor="red";
          console.log('#informatie reached');
        }
  
        // if(  (w > parseInt(bestellen))  && (w > parseInt(informatie)) && (w > parseInt(inspiratie)) && (w < parseInt(reviews))   ){
        //   console.log('inspiratie');
        // }
  
        // if(w > parseInt(informatie)){
        //   console.log('informatie');
        // }
  
      }
  });

Note that:注意:

As the variables are declared with keywords var and let inside the function. So, variables have local scope. If you also want to use the variables outside the function, you can just remove the keywords let and var with the variables.由于变量在function内部是用关键字varlet声明的。所以,变量有local scope。如果你还想使用function之外的变量,你可以只用变量去掉关键字letvar So, variables scope will get global scope.因此,变量 scope 将获得全局变量 scope。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM