JavaScript/jQuery code optimization -
i'm learning javascript , jquery , i'm dealing following code:
$("#hrefblur0").hover(function() { $("#imgblur0").toggleclass("blur frame"); }); $("#hrefblur1").hover(function() { $("#imgblur1").toggleclass("blur frame"); }); $("#hrefblur2").hover(function() { $("#imgblur2").toggleclass("blur frame"); }); $("#hrefblur3").hover(function() { $("#imgblur3").toggleclass("blur frame"); }); $("#hrefblur4").hover(function() { $("#imgblur4").toggleclass("blur frame"); }); $("#hrefblur5").hover(function() { $("#imgblur5").toggleclass("blur frame"); }); $("#hrefblur6").hover(function() { $("#imgblur6").toggleclass("blur frame"); }); $("#hrefblur7").hover(function() { $("#imgblur7").toggleclass("blur frame"); });
the code supposed remove blur effect image while hoover cursor on href link on website. i'm wondering if can faster, fewer lines of code. tried:
(var = 0; < 8; i++) { $("#hrefblur" + i).hover(function() { $("#imgblur" + i).toggleclass("blur frame"); }); }
but code doesn't work.
here's js fiddle: link
you can set class elements , select class, example let's want use "blurmecontainer" container, can this:
$(".blurmecontainer").hover(function(el){ $(this).find("img").toggleclass("blur frame"); });
the trick must aware jquery applies events element, inside events function, "this" accessor element involved in event, can use $ function in selector in order have corrispective jquery element, , can use "find" method find img tag inside jquery element. work if have single image in container, if need identify 1 image in set of images inside single container, assign class image (ie: "blurme") , change code in way:
$(".blurmecontainer").hover(function(el){ $(this).find(".blurme").toggleclass("blur frame"); });
Comments
Post a Comment