Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery simplify mouseenter mouseleave code
    text
    copied!<p>I have 3 banners in which I want to have some <code>mouseenter</code> and <code>mouseleave</code> behaviors for each one.<br> I made it work, but by repeating the jQuery and using ids for each div. I want to know how to do it just using class as the animation is same for each.</p> <p>The HTML:</p> <pre><code>&lt;div id="first-banner"&gt; &lt;a href="&lt;?php echo home_url( '/' ); ?&gt;?page_id=51" class="banner-trigger" id="banner-trigger-1"&gt; &lt;img src="&lt;?php bloginfo('stylesheet_directory'); ?&gt;/images/home-slider/banners/ico1.png" /&gt; &lt;div class="banner-tag" id="banner-tag-1"&gt; &lt;img src="&lt;?php bloginfo('stylesheet_directory'); ?&gt;/images/home-slider/banners/ico1link.png" /&gt; &lt;/div&gt; &lt;/a&gt; &lt;/div&gt; &lt;div id="second-banner"&gt; &lt;a href="&lt;?php echo home_url( '/' ); ?&gt;?page_id=47" class="banner-trigger" id="banner-trigger-2"&gt; &lt;img src="&lt;?php bloginfo('stylesheet_directory'); ?&gt;/images/home-slider/banners/ico2.png" /&gt; &lt;div class="banner-tag" id="banner-tag-2"&gt; &lt;img src="&lt;?php bloginfo('stylesheet_directory'); ?&gt;/images/home-slider/banners/ico2link.png" /&gt; &lt;/div&gt; &lt;/a&gt; &lt;/div&gt; &lt;div id="third-banner"&gt; &lt;a href="#" class="banner-trigger trigger" id="banner-trigger-3"&gt; &lt;img src="&lt;?php bloginfo('stylesheet_directory'); ?&gt;/images/home-slider/banners/ico3.png" /&gt; &lt;div class="banner-tag" id="banner-tag-3"&gt; &lt;img src="&lt;?php bloginfo('stylesheet_directory'); ?&gt;/images/home-slider/banners/ico3link.png" /&gt; &lt;/div&gt; &lt;/a&gt; &lt;/div&gt; </code></pre> <p><br> The script:</p> <pre><code> // First $("#banner-trigger-1").mouseenter(function(e){ $("#banner-trigger-1").parent("#first-banner").stop().animate({top: "-50px"}, 300 ); $("#banner-trigger-1").children("#banner-tag-1").stop().animate({top: "-50px"}, 300 ); }); $("#banner-trigger-1").mouseleave(function(e){ $("#banner-trigger-1").parent("#first-banner").stop().animate({top: "0px"}, 500 ); $("#banner-trigger-1").children("#banner-tag-1").stop().animate({top: "-90px"}, 300 ); }); // Second $("#banner-trigger-2").mouseenter(function(e){ $("#banner-trigger-2").parent("#second-banner").stop().animate({top: "-50px"}, 300 ); $("#banner-trigger-2").children("#banner-tag-2").stop().animate({top: "-50px"}, 300 ); }); $("#banner-trigger-2").mouseleave(function(e){ $("#banner-trigger-2").parent("#second-banner").stop().animate({top: "0px"}, 500 ); $("#banner-trigger-2").children("#banner-tag-2").stop().animate({top: "-90px"}, 300 ); }); // Third $("#banner-trigger-3").mouseenter(function(e){ $("#banner-trigger-3").parent("#third-banner").stop().animate({top: "-50px"}, 300 ); $("#banner-trigger-3").children("#banner-tag-3").stop().animate({top: "-50px"}, 300 ); }); $("#banner-trigger-3").mouseleave(function(e){ $("#banner-trigger-3").parent("#third-banner").stop().animate({top: "0px"}, 500 ); $("#banner-trigger-3").children("#banner-tag-3").stop().animate({top: "-90px"}, 300 ); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload