This is less of a specific problem or error but more of a implementation question.
First of I\'d like to say that I\'ve been through a lot of fading image tutorials
try to do it easy like this:
$('#img').hover(
function() {
$(this).stop().fadeIn(...).attr('src', 'image_o').fadeOut(...)
},
function() {
$(this).stop().fadeIn(...).attr('src', 'image').fadeOut(...)
});
You can get the src
attribute of an image and use .replace()
to replace the url on hover!
$('.fadein').each(function() {
var std = $(this).attr("src");
var hover = std.replace(".jpg", "_o.jpg");
$(this).clone().insertAfter(this).attr('src', hover).removeClass('fadein').siblings().css({
position:'absolute'
});
$(this).mouseenter(function() {
$(this).stop().fadeTo(600, 0);
}).mouseleave(function() {
$(this).stop().fadeTo(600, 1);
});
});
Or like:
$('.fadein').each(function() {
var std = $(this).attr("src");
var hover = std.replace(".jpg", "_o.jpg");
$(this).wrap('<div />').clone().insertAfter(this).attr('src', hover).removeClass('fadein').siblings().css({
position:'absolute'
});
$(this).mouseenter(function() {
$(this).stop().fadeTo(600, 0);
}).mouseleave(function() {
$(this).stop().fadeTo(600, 1);
});
});
What the script does:
var std = $(this).attr("src");
grab the SRC attributevar hover = std.replace(".jpg", "_o.jpg");
$(this).wrap('<div />')
src
and place it UNDERNEATH the first one .clone().insertAfter(this).attr('src', hover)
.removeClass('fadein')
.siblings().css({position:'absolute'});
Here's a nice pattern:
<img src="http://i.imgur.com/vdDQgb.jpg" hoverImg="http://i.imgur.com/Epl4db.jpg">
JS:
$('body').find('*[hoverImg]').each(function(index){
$this = $(this)
$this.wrap('<div>')
$this.parent().css('width',$this.width())
$this.parent().css('height',$this.width())
$this.parent().css('background-image',"url(" + $this.attr('hoverImg')+")")
$this.hover(function() {
$(this).stop()
$(this).fadeTo('',.01)
},function() {
$(this).stop()
$(this).fadeTo('',1)
})
});