简体   繁体   English

平滑滚动没有锚仅jQuery

[英]Smooth scroll no anchor only jQuery

I have a jQuery script that allows me to scroll horizontally but it's a rusty old scroll, and I want to make it smooth without changing the entire code. 我有一个jQuery脚本,它允许我水平滚动,但它是生锈的旧滚动,并且我想使其平滑而不改变整个代码。

I want to change the this.scrollLeft -= (delta * 90); 我想更改this.scrollLeft -= (delta * 90); with an .animate but I'm a beginner with jQuery. .animate但我是jQuery的初学者。

Here's my code so far : 到目前为止,这是我的代码:

$(document).ready(function() {

    $('html, body, eall, *').mousewheel(function(e, delta) {
        this.scrollLeft -= (delta * 90);
        e.preventDefault();
    });
});

You can do something like this: 您可以执行以下操作:

$('html, body').animate({
    scrollLeft : 0 // or whatever value you need here
}, 750);

First only select the elements that must call your event callback and not $('html, body, eall, *') like your question shows you are doing. 首先仅选择必须调用事件回调的元素,而不要选择$('html, body, eall, *')就像您的问题表明您正在做的那样。

For example, add a .horizontalScroll class to the desired elements and then $('.horizontalScroll ) 例如,将.horizontalScroll类添加到所需的元素,然后添加$('.horizontalScroll

Then you can do something like this to make a smoother scroll: 然后,您可以执行以下操作以使滚动更流畅:

var scroll = $(this).scrollLeft() - (delta * 90);
$(this).stop(true).animate({
      scrollLeft: scroll
}, 200);
e.preventDefault();

See this working demo (scroll inside div#scroll , tweak (delta * 90) and animate duration argument 200 to get the desired smoothness. 参见此工作示例 (在div#scroll ,调整(delta * 90)animate时间参数200以获取所需的平滑度。

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

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