所以我对我的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});
});
});发布于 2011-01-30 14:31:45
这听起来像是你想让它重新循环并在鼠标移出时停止。
定义cycle_images()函数后,添加一个标志repeatCycle
cycle_images.repeatCycle = true;在cycle_images函数的末尾,添加一个带超时的递归调用
if (this.repeatCycle) {
var f = function() {
return cycle_images.apply(this, [$current_image]);
}
setTimeout(f,50);
}然后在鼠标输出中,
cycle_images.repeatCycle = false;https://stackoverflow.com/questions/4841475
复制相似问题