Javascript 平滑滚动无锚只jQuery
我有一个jQuery脚本,它允许我水平滚动,但这是一个生锈的旧滚动,我想让它平滑,而不改变整个代码 我想更改Javascript 平滑滚动无锚只jQuery,javascript,jquery,smooth-scrolling,Javascript,Jquery,Smooth Scrolling,我有一个jQuery脚本,它允许我水平滚动,但这是一个生锈的旧滚动,我想让它平滑,而不改变整个代码 我想更改this.scrollLeft-=(delta*90),但我是jQuery的初学者 以下是我目前的代码: $(document).ready(function() { $('html, body, eall, *').mousewheel(function(e, delta) { this.scrollLeft -= (delta * 90); e
this.scrollLeft-=(delta*90)使用.animate
编写代码>,但我是jQuery的初学者
以下是我目前的代码:
$(document).ready(function() {
$('html, body, eall, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 90);
e.preventDefault();
});
});
您可以这样做:
$('html, body').animate({
scrollLeft : 0 // or whatever value you need here
}, 750);
首先只选择必须调用事件回调的元素,而不是$('html,body,eall,*')
,就像你的问题所显示的那样
例如,将.horizontalScroll
类添加到所需的元素,然后添加$('.horizontalScroll
)
然后,您可以执行以下操作以使滚动更平滑:
var scroll = $(this).scrollLeft() - (delta * 90);
$(this).stop(true).animate({
scrollLeft: scroll
}, 200);
e.preventDefault();
(在div#scroll
、tweak(delta*90)
和animate
持续时间参数200
内滚动以获得所需的平滑度。什么是'eall'
应该选择的?然后仍然为什么要使用“*”来匹配所有元素?谢谢,但我需要在你的代码中更改一些内容$(this).stop(true)而不是$(this).stop(true,true)Ty@AlexBonta你们做得很好。跳到终点会导致丑陋的行为。我更新了答案和演示