Milan Milosevic
Milan Milosevic

Reputation: 433

Change thumbs on mouse hover

Have problem with this script, cant make it to continue rotate thumbs after last image

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" />

Thumbs is rotating on hover but after last (10.jpg) wont start again from 0.jpg

Upvotes: 0

Views: 184

Answers (1)

Eric Uldall
Eric Uldall

Reputation: 2391

Try something like this:

Improvments include - compile images array once at load time, use interval instead of timeout, reference length of images array in for loop, start image loop where it last left off (optionally, continuous = true)

I ran it to make sure there are not console errors, but I don't have all of your html to check against...so let me know if it works for you.

Updated 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);
        }
    });
});

Upvotes: 1

Related Questions