gpt4 book ai didi

javascript - 悬停时连续/无限水平滚动

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:45 25 4
gpt4 key购买 nike

我有一个架子,当您将鼠标悬停在左侧或右侧按钮时,它会左右滚动。然而,目前这只会滚动一个定义的数量..并且这个架子将通过延迟加载来填充,所以没有真正的方法知道架子有多宽。

当然,我可以将数字设置为 99999999 并将动画速度设置为同样高的数字,但肯定有更聪明的方法来做到这一点吗?没有插件!

谢谢你的帮助..

Fiddle

$('.scroll-arrow').each(function(){
var modifier = ($(this).hasClass('right')) ? 1 : -1;
var sib = ('.shelf-slide');
var sl = 0;

$(this).hover(function() {
sl = $(this).siblings(sib).scrollLeft();
$(this).siblings(sib).stop();
$(this).siblings(sib).animate({scrollLeft: sl + (modifier * 1000)}, 5000, 'linear');
}, function() {
$(this).siblings(sib).stop();
});
});

最佳答案

你可以创建一个函数,当你像这样悬停箭头时保持动画

function animatecontent(ele,modifier){//function to scroll
var sl = ele.scrollLeft();
//120 should be the width of the ".content" and 500 the time to scroll 1 ".content"
ele.animate({scrollLeft: sl + (modifier * 120)}, 500, 'linear',function(){
if(hover){//on callback if it is still hover call the same function
animatecontent(ele,modifier);
}
});
};
var hover=false;
$('.scroll-arrow').each(function(){
var modifier = ($(this).hasClass('right')) ? 1 : -1;
var sib = ('.shelf-slide');
$(this).hover(function() {
hover=true;
$(this).siblings(sib).stop();
animatecontent($(this).siblings(sib),modifier);//pass the element to animate and the modifier
}, function() {
hover=false;
$(this).siblings(sib).stop();
});
});

http://jsfiddle.net/A3mPw/7/

关于javascript - 悬停时连续/无限水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19711634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com