我在jQuery中編寫了一個圖像幻燈片,可以根據鼠標懸停在圖像上的位置來加快/縮小。jQuery圖像旋轉木馬 - 我如何重複圖像?
我想在幻燈片放映結束時讓圖像「重複」。因此,用戶滾動瀏覽幻燈片,它會到達圖像LI的末尾,然後從頭開始無縫重複。
下面是當前的代碼:
jQuery的
$(document).ready(function()
{
$('#products ul').css('width', ($('#products ul li').length * 185));
var $products = $('#products div');
var posLeft = 0;
// Add 5 DIV overlays to use as 'hover buttons' for the slideshow speed
for (i = 0; i <= 4; i++)
{
$('#products').append('<div class="hover-' + i + '" style="background: transparent; width: 100px; height: 167px; position: absolute; left: ' + posLeft + 'px; top: 15px;"></div>');
posLeft += 100;
}
// Function animate the scrolling DIV in the appropriate direction with the set speed
function plz2scroll(direction, scrollSpeed)
{
var scrollDir = (direction == 'left') ? '-=' : '+=';
var scrollOffset = (direction == 'right') ? '-10' : '+' + $products.css('width').substring(0, -2) + 10; // Fix the '+100px10' issue - Seems substring don't support negative offsets
$products.animate({scrollLeft: scrollDir + $products.css('width')}, scrollSpeed, 'linear', function()
{
$products.scrollLeft(scrollOffset);
plz2scroll(direction, scrollSpeed);
});
}
// Create the 'hover buttons'
$('div.hover-0').hover(function() { $products.stop(); plz2scroll('right', 2000); });
$('div.hover-1').hover(function() { $products.stop(); plz2scroll('right', 3500); });
$('div.hover-2').hover(function() { $products.stop(); });
$('div.hover-3').hover(function() { $products.stop(); plz2scroll('left', 3500); });
$('div.hover-4').hover(function() { $products.stop(); plz2scroll('left', 2000); });
});
HTML
<div id="products">
<div>
<ul>
<li><img src="images/1.jpg" /></li>
<li><img src="images/2.jpg" /></li>
<li><img src="images/3.jpg" /></li>
<li><img src="images/4.jpg" /></li>
<li><img src="images/5.jpg" /></li>
</ul>
</div>
</div>
滾輪工程和速度/方向與疊加DIV按鍵很好地工作。我的animate()回調重複是緩慢的,越野車,只是很糟糕:/
我過度使用.stop()也看起來像一個潛在的問題>。 <
任何想法?
我沒有對重複問題的答案,但我不認爲你過度使用stop(),看起來不錯。我想,'100px10'問題可以通過使用'innerWidth()'和'outerWidth()'來克服。 – 2010-02-24 04:23:17
我選擇了'(parseInt($ products.css('width')。replace('px',''))+ 10);' – dave 2010-02-24 04:44:46
看起來像這個答案在[[幫助自動旋轉無限jquery傳送帶。無法讓輪播無限循環而不是「倒帶」] [1]「可能有些用處。 [1]:http://stackoverflow.com/questions/3738640/help-with-auto-rotating-infinite-jquery-carousel-can-not-get-carousel-to-loop-in/ 3739493#3739493 – bengusty 2011-09-07 20:20:40