简体   繁体   English

Javascript用鼠标缓慢滚动到页面上的下一个锚点

[英]Javascript scroll to next anchor on page with mouse slowly

I have a single page website with multiple anchors.我有一个带有多个锚点的单页网站。 I am referencing this fiddle example http://jsfiddle.net/kamikazefish/t6LLybx8/201/ for advancing the page to the next anchor when the mouse scrollwheel is used.我正在引用这个小提琴示例http://jsfiddle.net/kamikazefish/t6LLybx8/201/用于在使用鼠标滚轮时将页面推进到下一个锚点。 Is there a way I can control the speed if someone could help would be much appreciated.如果有人可以提供帮助,我是否可以控制速度,将不胜感激。 keep in mind I am very new to code and would love it if it was wrote out not just tell me to add a function or something thanks.请记住,我对代码非常陌生,如果它被写出来,我会喜欢它,而不仅仅是告诉我添加一个函数或其他东西,谢谢。 Here is the Javascript.这是Javascript。

(function() {
    var delay = false;

    $(document).on('mousewheel DOMMouseScroll', function(event) {
        event.preventDefault();
        if(delay) return;

        delay = true;
        setTimeout(function(){delay = false}, 200)

        var wd = event.originalEvent.wheelDelta || -event.originalEvent.detail;

        var a= document.getElementsByTagName('a');
        if(wd < 0) {
            for(var i = 0 ; i < a.length ; i++) {
                var t = a[i].getClientRects()[0].top;
                if(t >= 40) break;
            }
        }
        else {
            for(var i = a.length-1 ; i >= 0 ; i--) {
                var t = a[i].getClientRects()[0].top;
                if(t < -20) break;
            }
        }
        $('html,body').animate  ({
            scrollTop: a[i].offsetTop
        });
    });
})();

You can just add the time during which you want the animation to run like so:您可以添加您希望动画运行的时间,如下所示:

$('html,body').animate({
    scrollTop: a[i].offsetTop
}, 1000);

Where 1000 is the time in ms.其中1000是以毫秒为单位的时间。 If you want it more slow increase this number.如果您希望它更慢,请增加此数字。

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

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