【发布时间】:2011-01-30 06:13:40
【问题描述】:
所以我对我的 JS 有点生疏,但这是我的代码...... 基本上我有一个图像,在鼠标悬停时,它会在一个充满其他图像的隐藏 div 中循环...淡出,替换 src,然后淡入。效果很好。但是一旦它通过所有图像,我希望它重新开始并继续循环它们直到 mouseout 事件停止它。
我以为我可以从函数cycle_images($(current_image)); 中再次调用该函数,但这会导致浏览器崩溃,这是可以理解的。有什么好的方法可以做到这一点?
$.fn.image_cycler = function(options){
// default configuration properties
var defaults = {
fade_delay: 150,
image_duration: 1500,
repeatCycle: true,
};
var options = $.extend(defaults, options);
this.each(function(){
var product = $(this);
var image = $('div.image>a.product_link>img', product);
var description = $('div.image>div.description', product);
var all_images = $('div.all_images', product);
var next_image = ($(all_images).find('img[src="' + $(image).attr('src') + '"]').next('img').attr('src')) ? $(all_images).find('img[src="' + $(image).attr('src') + '"]').next('img') : $(all_images).children('img').first();;
// mouseover
image.fadeOut(options.fade_delay, function(){
image.attr('src', next_image.attr('src'));
image.fadeIn(options.fade_delay);
});
if (options.repeatCycle){
var loop = function() {
product.image_cycler();
}
setTimeout(loop,options.image_duration);
}
});
};
$(document).ready(function() {
$('div.product').hover(function(){
$(this).image_cycler();
}, function(){
$(this).image_cycler({repeatCycle: false});
});
});
【问题讨论】:
-
我建议不要使用 .each 中的所有图像,而是使用
var img = $('div.image div.all_images img:first')并继续使用var img = $(this).next() || $('div.image div.all_images img:first');(或类似的东西)。
标签: javascript jquery infinite-loop