How to match the word in jquery
I have the image and i am fade in and fade out the image on click. But i want that if the same is present then it should not fadein/fadeout.
My jquery code is
switch(current_id)
{
case 'l1':
$("#left_img img").fadeOut(500, function() {
$(this).attr("src","rrr.gif").load( function() {
$(t开发者_如何学Chis).fadeIn(500);
});
});
break;
case 'l3':
$("#left_img img").fadeOut(500, function() {
$(this).attr("src","cc1.gif").load( function() {
$(this).fadeIn(500);
});
});
break;
If i click on link 1 then image A appears with fade effect. but if i click again link 1 agan , then again i see fade effect . Is there any way i can avoid that if same link is clicked again
try adding another variable to check...
var last_id;
if (last_id != current_id) {
last_id = current_id;
switch(current_id){
case 'l1':
$("#left_img img").fadeOut(500, function() {
$(this).attr("src","rrr.gif").load( function() {
$(this).fadeIn(500);
});
});
break;
case 'l3':
$("#left_img img").fadeOut(500, function() {
$(this).attr("src","cc1.gif").load( function() {
$(this).fadeIn(500);
});
});
break;
}
}
Do a check on the src
attribute of the image - if it wasn't already change do the effect and load it in.
switch(current_id)
{
case 'l1':
if($(left_img img").attr("src") != "rrr.gif") {
$("#left_img img").fadeOut(500, function() {
$(this).attr("src","rrr.gif").load( function() {
$(this).fadeIn(500);
});
});
}
break;
case 'l3':
if($("#left_img img").attr('src') != "cc1.gif") {
$("#left_img img").fadeOut(500, function() {
$(this).attr("src","cc1.gif").load( function() {
$(this).fadeIn(500);
});
});
}
break;
精彩评论