鼠标悬停时改变拇指

时间:2013-09-07 01:51:55

标签: jquery rotation thumbnails

此脚本有问题,无法在最后一张图片后继续旋转拇指

var cdn_url     = 'http://cdn.domain.com/';
var timers  = new Array;
var images  = new Array;
function changeThumb( id, cdn_url )
{
        document.getElementById(id).src = cdn_url;
}

$(document).ready(function() {
    $("img[id*='tmb_']").mouseover(function(){
        var image_id    = $(this).attr("id");
        var id_split    = image_id.split('_');
        var one    = id_split[1];
        var two    = id_split[2];
        thumbs = 10;

        for ( var i=0; i<=thumbs; i++   ) {
            var image_url = cdn_url + one + '/' + two + '/' + i + '.jpg';
            images[i]     = new Image();
            images[i].src = image_url;
        }
        for ( var i=0; i<=thumbs; i++ ) {
            timers[i] = setTimeout("changeThumb('" + image_id + "','" + cdn_url + one + '/' + two + '/' + i + '.jpg' + "')", i*200);
        }
    }).mouseout(function(){
        var image_id    = $(this).attr("id");
        var id_split    = image_id.split('_');
        var one    = id_split[1];
        var two    = id_split[2];
        var init = id_split[3];
        thumbs = 10;

        for ( var i=0; i<=thumbs; i++ ) {
            if ( typeof timers[i] == "number" ) {
                clearTimeout(timers[i]);
            }
        }
        $(this).attr('src', cdn_url + one + '/' + two + '/' + init + '.jpg');
    });
});

HTML

<img id="tmb_0613_2913374229_4" class="img"  src="http://cdn.domain.com/thumbs/0613/2913374229/4.jpg" />

拇指在悬停时旋转但最后(10.jpg)不会再从0.jpg开始

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

改进包括 - 在加载时编译图像数组一次,使用间隔而不是超时,for循环中图像数组的引用长度,从最后一次停止开始的图像循环(可选,连续=真)

我运行它以确保没有控制台错误,但我没有检查所有的HTML ...所以请告诉我它是否适合您。

更新了JS:

var cdn_url = 'http://telus-nezavisnost.com/videos/';
var thumbs  = 5;
var images = [];
var iterator = [];
var continuous = false;
var image_interval;
var init_thumb;

//global functions
var changeThumb = function(id, cdn_url){
    document.getElementById(id).src = cdn_url;
}

var initThumbs = function(selector){
    for ( var i=0; i<thumbs; i++  ) {
        var image_id    = selector.attr("id");
        var id_split    = image_id.split('_');
        var one    = id_split[1];
        var two    = id_split[2];
        var image_url = cdn_url + one + '/' + two + '/' + i + '.jpg';
        var image = new Image();
        image.src = image_url;
        if( images[image_id] === undefined ){
         images[image_id]        = [];
        }
        images[image_id][i]     = image;
    }
}

$(function() {
    $("img[id*='tmb_']").mouseover(function(){
        init_thumb = $(this).attr('src');
        initThumbs($(this));
        var image_id    = $(this).attr("id");
        var image_count = images[image_id].length;

        if( !(iterator.hasOwnProperty(image_id)) || !continuous ){
            iterator[image_id] = 1;
        }

        image_interval = setInterval(function(){
            changeThumb(image_id, images[image_id][iterator[image_id]].src);
            if( iterator[image_id] < ( image_count - 1 ) ){
                iterator[image_id]++;
            }else{
                iterator[image_id] = 1;   
            }
        }, 200);
    }).mouseout(function(){
        var image_id    = $(this).attr("id");
        clearInterval(image_interval);
        if( !continuous ){
            changeThumb(image_id, init_thumb);
        }
    });
});