简体   繁体   English

$('body,html')。is(':animated'))似乎不起作用

[英]$('body, html').is(':animated')) doesn't seem to be working

I have 4 divs with heights set to window height. 我有4个div,其高度设置为窗口高度。 I want on each scroll down to scroll to the next one, however after the first scroll down it just keeps on scrolling, seemingly ignoring is:animated 我想在每次向下滚动时滚动到下一个滚动,但是在第一次向下滚动后,它一直滚动,似乎忽略了is:animated

<style>

    .div {
        width: 100%;
    }

    .red {
        background: red;
    }

    .yellow {
        background: yellow;
    }

    .green {
        background: green;
    }

    .blue {
        background: blue;
    }

</style>

<div id="id0" data-id="0" class="nbhd red scrolledto"></div>
<div id="id1" data-id="1" class="nbhd yellow"></div>
<div id="id2" data-id="2" class="nbhd green"></div>
<div id="id3" data-id="3" class="nbhd blue"></div>



function setHeights() {
        $('.nbhd').css('height', window.innerHeight);
    }

    function scrollSections() {

        if ($('body, html').is(':animated')) {
            return;
        }

        var currentSectionId = $('.nbhd.scrolledto').data('id');

        currentSectionId++;

        $('.scrolledto').removeClass('scrolledto');

        var section = $('#id'+currentSectionId);

        section.addClass('scrolledto');

        var pos = section.offset().top;

        $('body, html').animate({scrollTop: pos}, 1000, function() {
            console.log('animated');
        });

    }

    setHeights();



    $( window ).on( "scroll", function() {
        scrollSections();
    });

fiddle: https://jsfiddle.net/2d47k6af/ 小提琴: https : //jsfiddle.net/2d47k6af/

I also get animated logged 6 times for some reason, I expected 3. 由于某些原因,我还记录了6次animated记录,我期望3。

I guess this is what you're going for: https://jsfiddle.net/2d47k6af/5/ 我想这就是你要去的地方: https : //jsfiddle.net/2d47k6af/5/

The problem was that you can't distinguish scroll events triggered by JavaScript and by user themself . 问题在于您无法区分JavaScript和用户自身触发的滚动事件 So when you run the animation it fired many $(window).on("scroll", ...); 因此,当您运行动画时,它会触发许多$(window).on("scroll", ...); events that were ignored thanks to $('body, html').is(':animated') that worked as expected. 由于$('body, html').is(':animated')工作正常,因此忽略了这些事件。

However the last scroll event occured probably after the animation has ended so function scrollSections() was called one more time which triggered another animation and so on. 但是,最后一个滚动事件可能发生在动画结束之后,因此又一次调用了函数scrollSections() ,触发了另一个动画,依此类推。 My solution would be to add a short timeout after the animation has ended and before it's ready to another user triggered scroll: 我的解决方案是在动画结束之后和准备好让另一个用户触发滚动之前添加短暂的超时:

$('body, html').animate({scrollTop: pos}, 1000, function() {
  console.log('animated');
  ignoreScrollEvents = true;

  setTimeout(function() {
    ignoreScrollEvents = false;
  }, 100);

});

You can to use variable to achieve, what you want: 您可以使用变量来实现您想要的:

 function setHeights() { $('.nbhd').css('height', window.innerHeight); } var isAnimated = false; // Initialize variable function scrollSections() { if (isAnimated) return; // Previous animation still in action isAnimated = true; // lock it var currentSectionId = $('.nbhd.scrolledto').data('id'); currentSectionId++; var section = $('#id'+currentSectionId); if (!section.length) { currentSectionId = 0; section = $('#id0'); } $('.scrolledto').removeClass('scrolledto'); section.addClass('scrolledto'); var pos = section.offset().top; $('body').animate({scrollTop: pos}, 1000, function() { setTimeout(function(){ isAnimated = false; // unlock it on next eventloop tick }) }); } setHeights(); $( window ).on( "scroll", function() { scrollSections(); }); 
 .div {width: 100%;} .red {background: red;} .yellow {background: yellow;} .green {background: green;} .blue {background: blue;} 
 <div id="id0" data-id="0" class="nbhd red scrolledto"></div> <div id="id1" data-id="1" class="nbhd yellow"></div> <div id="id2" data-id="2" class="nbhd green"></div> <div id="id3" data-id="3" class="nbhd blue"></div> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> 

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

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