我前一陣有同樣的問題,並把這個在一起,基本上它是一個無限滾動的解決方案:
編輯:這是我的代碼,適合您的用例:
// Assuming you also have/want a scroll up
jQuery(".up-arrow").mousedown(function() {
// You don't need to use jQuery instead of $ inside of jQuery defined functions
var offset = $("#technology_list").offset();
var topValue = offset.top;
if ((topValue * 2) < 1000) speed = topValue * 3;
else if ((topValue * 2) < 500) speed = topValue * 4;
else if ((topValue * 2) < 100) speed = topValue * 8;
else speed = topValue * 2;
$("#technology_list").animate({
top: 0
}, speed);
}).mouseup(function() {
$("#technology_list").stop();
});
jQuery(".down-arrow").mousedown(function() {
var offset = $("#technology_list").offset();
var topValue = offset.top;
var height = $("#technology_list").height();
if (((height - topValue) * 2) < 1000) speed = (height - topValue) * 2;
else if (((height - topValue) * 2) < 500) speed = (height - topValue) * 2;
else if (((height - topValue) * 2) < 100) speed = (height - topValue) * 2;
else speed = (height - topValue) * 2;
$("#technology_list").animate(function() {
top: $("#technology_list").height()
}, speed);
}).mouseup(function() {
$("#technology_list").stop();
});
編輯結束。
$(".scroll-left").hover(function() {
if (($(this).parent().scrollLeft() * 2) < 1000) speed = $(this).parent().scrollLeft() * 3;
// If it is less than 500 pixels from the edge, then it takes 3 times as long as the scrollLeft value in milliseconds. (Sorry about the if's being hard to read)
else if (($(this).parent().scrollLeft() * 2) < 500) speed = $(this).parent().scrollLeft() * 4;
// And if it is less than 250 pixels, it takes 4 times as long
else if (($(this).parent().scrollLeft() * 2) < 100) speed = $(this).parent().scrollLeft() * 8;
// Less than 50, it takes 8 times as long
else speed = $(this).parent().scrollLeft() * 2;
// And if it is more than 500 run it at 2 milliseconds per pixel
$(this).parent().animate({
scrollLeft: 0
}, speed);
// Scroll it to the beginning at the above set speed
}, function() {
$(this).parent().stop();
// On mouseOut stop the animation
});
$(".scroll-right").hover(function() {
parent = $(this).parent()[0];
parentWidth = $(this).parent()[0].scrollWidth;
// Cache parent and parentWidth (stops delay on hover)
if (((parentWidth - parent.scrollLeft) * 2) < 1000) speed = (parentWidth - parent.scrollLeft) * 2;
// Pretty much the same thing as before but this time it sort of makes a "scrollRight"
else if (((parentWidth - parent.scrollLeft) * 2) < 500) speed = (parentWidth - parent.scrollLeft) * 2;
else if (((parentWidth - parent.scrollLeft) * 2) < 100) speed = (parentWidth - parent.scrollLeft) * 2;
else speed = (parentWidth - parent.scrollLeft) * 2;
$(this).parent().animate({
scrollLeft: $(this).siblings(".row-scroll").width()
}, speed);
// Same thing as before, but this time we scroll all the way to the right side
}, function() {
$(this).parent().stop();
});
這是從我的代碼直接複製,但想法是健全的,當它接近邊緣的乘法減慢。
它不是或接近它,如果完美的你想要的東西更多的微調,那麼你應該嘗試Pause
謝謝你的工作完美。我從來沒有意識到線性選擇。 – chadpeppers 2012-07-25 14:51:36
讓我永遠認識到它也存在我自己!我總是假設沒有放棄寬鬆選項,但它完全沒有(或'線性')......這顯然不是這種情況。可笑的哈哈 – 2012-07-25 14:53:26