简体   繁体   English

如何控制鼠标滚动事件,然后在 html 主体上执行 animation?

[英]How can I control the mouse scroll event and after that do an animation on the html, body?

I'm facing a very strange error, which is animation on body during mouse scroll.我面临一个非常奇怪的错误,即鼠标滚动期间身体上的 animation 。 I think its happening because of the JQuery event window.scroll .我认为它的发生是因为JQuery事件window.scroll I have tried a lot of things like unbinding of animation on mouse scroll, but nothing works.我已经尝试了很多事情,比如在鼠标滚动上解除 animation 的绑定,但没有任何效果。 Below is my code.下面是我的代码。

$(document).on("scroll", function () {

  var lastScrollTop = 0;

  var windowHeight = $(window).scrollTop();

  var seccion1 = $("#seccion1").height();

  var seccion2 = $("#seccion2").offset().top;

  var alturaseccion2 = $("#seccion2").height();

//this function returns in wich section is the user with the scroll
  var localizacion = comprobarSeccion(seccion1, seccion2);


  if (windowHeight > lastScrollTop) {
    // down scroll 

    console.log("scrollabajo");

    if (localizacion == 1) {


      $("html, body").animate({
        scrollTop: $("#seccion2").offset().top

      }, 2);
      $(document).bind("scroll");

    } else if (localizacion == 2) {

      if (windowHeight >= ((alturaseccion2 * 0.80) + seccion2) && windowHeight <= (alturaseccion2 + seccion2)) {


      } else {


      }

    }



  } else {
    // up scroll 

    console.log("scrollarriba");
  }
  lastScrollTop = windowHeight;



});
´´´

I'm not sure what you are trying to accomplish, but if your trying to trigger an event with a specific scroll value you can use the code below我不确定您要完成什么,但如果您尝试触发具有特定滚动值的事件,您可以使用下面的代码

 $(window).scroll(function () { 
        var scroll = $(window).scrollTop();

        if (scroll >= 500) {
             alert("scroll is greater than 500 px)
        } else if(scroll==500){
            alert("scroll has hit 500px");
        }

    });

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

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