Trying to get my rollovers to change src on rollover. This is working ok, though there is a bug. After I have clicked on a thumbnail, the src can sometimes contain the wrong src (the rollover state remains even on mouseout). . To find the bug, click on a few thumbnails and mouseover a few, you should see the rollover src remain for ones that have been clicked already. Demo is no longer available, sorry!
The jQuery -
function image_gallery (){
if ($('ul.thumbs').length > 0) {
$('.gallery').each(function(){
$('ul.thumbs li img:gt(0)').addClass('unselected');
$('ul.thumbs li img:eq(0)').addClass('selected');
function mouse_overs () {
var unselected = $('li img.unselected');
unselected.hover(function(){
var thumb = $(this);
thumb.attr('src',thumb.attr('src')
.replace(/([^.]*\d)\.(.*)/, "$1r.$2"));
}, function(){
var thumb = $(this);
thumb.each(function(){
$(this).attr('src',$(this)
.attr('src').replace('r.jpg','.jpg'));
});
});
};
mouse_overs();
var img_main = $(this).find('img.main:first');
$(this).find('ul.thumbs img').each(function(){
$(this).click(function(){
var thumb = $(this);
var src = thumb.attr('src');
if ( src.indexOf('r.jpg') == -1) {
$(this).attr('src',thumb.attr('src')
.replace(/([^.]*)\.(.*)/, "$1r.$2"));
}
var selected = $('ul.thumbs li img.selected');
// previous img remove r.jpg
selected.attr('src',selected.attr('src')
.replace('r.jpg','.jpg'));
selected.removeClass('selected');
selected.addClass('unselected');
//current thumb add class "selected", remove "unselected"
thumb.addClass('selected');
thumb.removeClass('unselected');
mouse_overs();
var rel = $(this).parent('a').attr('rel');
img_main.fadeOut(500, function(){
img_main.attr('src', rel);
img_main.fadeIn('slow');
});
thumb.mouseout(function(){
var src = $(this).attr('src');
if ( src.indexOf('r.jpg') == -1) {
$(this).attr('src',thumb.attr('src')
.replace(/([^.]*)\.(.*)/, "$1r.$2"));
}
else return false;
});
});
});
});
}
}
The HTML:
<div class="gallery">
<img class="main" src="images/gallery/yes-campaign/NL1.jpg"/>
<ul class="thumbs">
<li><a rel="images/gallery/yes-campaign/NL1.jpg"><img src="images/thumbs/yes-campaign/NL-1r.jpg"/></a></li>
<li><a rel="images/gallery/yes-campaign/NL2.jpg"><img src="images/thumbs/yes-campaign/NL-2.jpg"/></a></li>
<li><a rel="images/gallery/yes-campaign/NL3.jpg"><img src="images/thumbs/yes-campaign/NL-3.jpg"/></a></li>
<li><a rel="images/gallery/yes-campaign/NL4.jpg"><img src="images/thumbs/yes-campaign/NL-4.jpg"/></a></li>
</ul>
</div>
This HTML is repeated various times throughout the page. The rollover states are NL1r.jpg, NL2r.jpg etc. The images are organized in folders, so all image filenames use the same naming convention.