【问题标题】:auto-scrolling will pause when user moves mouse over screen / graphs. And auto-scrolling will resume again if mouse movement is stopped当用户将鼠标移到屏幕/图形上时,自动滚动将暂停。如果鼠标移动停止,自动滚动将再次恢复
【发布时间】:2016-04-28 05:04:56
【问题描述】:

您好,我有用于自动滚动页面的工作代码。我需要对此进行一些修改。当用户在页面上移动鼠标时需要暂停自动滚动,当没有鼠标移动时,自动滚动将恢复。

      <script>
    $("html, body").animate({ scrollTop: $(document).height() }, 400000);
        setTimeout(function() {
           $('html, body').animate({scrollTop:0}, 400000); 
        },400000);
        setInterval(function(){
             //  it will take 40 secound in total from the top of the page to the bottom
        $("html, body").animate({ scrollTop: $(document).height() }, 400000);
        setTimeout(function() {
           $('html, body').animate({scrollTop:0}, 400000); 
        },400000);

        },8000);
    </script>

【问题讨论】:

标签: javascript jquery


【解决方案1】:

希望这就是你要找的东西

  var x = 10,
    y = true,
    z = 1,
    maxscroll = 40,
    mixscroll = 10;

setInterval(function() {
    $('html, body').mousemove(function() {
        z = 0;
    });
    if (z === 0) {
        setTimeout(function() {
            z = x;
        }, 1000);
    } else {
        z = x;
        if (y) {
            $('html, body').animate({ scrollTop: ($(window).scrollTop() + z) + 'px' }, 300);
            x++;
        } else {
            $('html, body').animate({ scrollTop: ($(window).scrollTop() + -(z)) + 'px' }, 300);
            x--;
        }
    }

    if (maxscroll < x && y) {
        y = false;
    } else if (x < mixscroll) {
        y = true;
    }
}, 500);

https://jsfiddle.net/donS/9xdz86yu/

【讨论】:

  • 是这样的。但是您的页面在达到底部后不会再次上升。检查这个 jsfiddle.net/QUCWe/1605
  • 我没有花太多钱来完善它只是概念请检查更新希望对您有帮助
【解决方案2】:

您可以在 jQuery 中使用 .stop() 函数,例如 ..

$("html, body").mouseover(function(){ 
           $(this).stop();
});

试试这个FIDDLE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    相关资源
    最近更新 更多