我有两个问题:
1)我尝试使用下面的jquery鼠标悬停时旋转相册中的照片,但鼠标输出时循环无法停止。
2)我无法弄清楚如何使用fadeIn / fadeOut函数进行旋转/幻灯片放映?
var photo_timers = new Array;
var photo = new Array;
var base_url = 'http://www.example.com';
function changeThumb( id, url )
{
document.getElementById(id).src = url;
}
$("img[id*='album_slideshow_']").live('mouseover', function() {
var image_id = $(this).attr("id");
var id_split = image_id.split('_');
var album_id = id_split[2];
$.post(base_url + '/ajax_rotate_album_photos.php', { album_id: album_id },
function(response) {
if(response.count > 0) {
for(var i=0; i < response.pid.length; i++) {
var photo_src = base_url + '/images/picturethumbs/' + response.pid[i].id + '-1.jpg';
photo[i] = new Image();
photo[i].src = photo_src;
}
for(var i=0; i < response.pid.length; i++) {
var photo_src = base_url + '/images/picturethumbs/' + response.pid[i].id + '-1.jpg';
photo_timers[i] = setTimeout("changeThumb('" + image_id + "','" + photo_src + "')", i*100*10);
}
}
}, "json");
}).live('mouseout', function() {
var image_id = $(this).attr("id");
var id_split = image_id.split('_');
var album_id = id_split[2];
$.post(base_url + '/ajax_rotate_album_photos.php', { album_id: album_id },
function(response) {
if(response.count > 0) {
for(var i=0; i < response.pid.length; i++) {
if( typeof photo_timers[i] == "number" ) {
clearTimeout(photo_timers[i]);
}
}
}
}, "json");
$(this).attr('src', base_url + '/images/albums/' + album_id + '.jpg');
});
答案 0 :(得分:1)
在setTimeout
中设置整个for循环var loopTimer; //global variable
if(response.count > 0) {
loopTimer = setTimeout((function(){
for(var i=0; i < response.pid.length; i++) {
var photo_src = base_url + '/images/picturethumbs/' + response.pid[i].id + '-1.jpg';
photo[i] = new Image();
photo[i].src = photo_src;
}
for(var i=0; i < response.pid.length; i++) {
var photo_src = base_url + '/images/picturethumbs/' + response.pid[i].id + '-1.jpg';
photo_timers[i] = setTimeout("changeThumb('" + image_id + "','" + photo_src + "')", i*100*10);
}}),1000);
}
并且在鼠标输出时只清除循环计时器
if(response.count > 0) {
clearTimeout(loopTimer);
for(var i=0; i < response.pid.length; i++) {
if( typeof photo_timers[i] == "number" ) {
clearTimeout(photo_timers[i]);
}
}
}