2013-04-25 131 views
0

jQuery的幻燈片,我有這樣的代碼:暫停懸停功能

$(function() { 
var current = 0, 
$imgs = jQuery('#m_slider .ms1'); 
imgAmount = $imgs.length; 

$($imgs.css('position', 'absolute').hide().get(0)).show(); 


window.setInterval(swapImages, 5000); 

function swapImages() { 

var $currentImg = $('.ms1:visible'); 

var $nextImg = $('.ms1:hidden').eq(Math.floor(Math.random() *$('.ms1:hidden').length)); 
    speed = 500; 
// animation speed should be the same for both images so we have a smooth change 
$currentImg.fadeOut(speed); 
$nextImg.fadeIn(speed); 
} 
}); 

這種運作良好,我想我暫停懸停幻燈片。我在哪裏添加.hover()函數?

+0

#m_slider – runTarm 2013-04-25 06:05:49

回答

0

你可以試試這個(未測試):

$(function() { 
var current = 0, timer, 
    $imgs = jQuery('#m_slider .ms1'), 
    imgAmount = $imgs.length; 

$($imgs.css('position', 'absolute').hide().get(0)).show(); 

timer = setInterval(swapImages, 5000); 

jQuery('#m_slider').mouseenter(function() { 
    clearInterval(timer); 
}).mouseleave(function() { 
    timer = setInterval(swapImages, 5000); 
}); 

function swapImages() { 

    var $currentImg = $('.ms1:visible'); 

    var $nextImg = $('.ms1:hidden').eq(Math.floor(Math.random() *$('.ms1:hidden').length)); 
    var speed = 500; 
    // animation speed should be the same for both images so we have a smooth change 
    $currentImg.fadeOut(speed); 
    $nextImg.fadeIn(speed); 
} 
}); 
在容器上
+0

感謝,做工精良... – shfkktm 2013-04-25 07:36:53