0

Good morning,

I would like to now if I can create a hover effect with JQuery. Is it possible if the user puts the mouse over an image and then it displays a toggle effect with some text? How can I do that?

At the moment, the toggle effect is working when the user "click" on the image, but I would like to create the same effect with a simple hover.

This is my code:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
        <script>
        $(document).ready(function(){
            $("#flip1").click(function(){
                $("#panel1").slideToggle("500");
            });
            $("#flip2").click(function(){
                $("#panel2").slideToggle("500");
            });
            $("#flip3").click(function(){
                $("#panel3").slideToggle("500");
            });
        });
        </script>

Is it possible with JQuery?

Thanks,

Regards,

2
  • why not!! change the click to hover $("#flip1").hover( Commented Apr 8, 2014 at 10:58
  • 1
    Instead of repeating the same set of code... if you can share the html we can help you to write it better Commented Apr 8, 2014 at 10:58

3 Answers 3

1

Try this,

$(document).ready(function(){
   $("#flip1").hover(function(){
      $("#panel1").stop(true).slideToggle("500");
   });
   $("#flip2").hover(function(){
      $("#panel2").stop(true).slideToggle("500");
   });
   $("#flip3").hover(function(){
      $("#panel3").stop(true).slideToggle("500");
   });
});

Read hover()

You can use data-panel on image like

<img id="flip1" data-panel="#panel1" src="..." />

Script

$(document).ready(function(){
   $("#flip1, #flip2, #flip3").hover(function(){
      $($(this).data('panel')).stop(true).slideToggle("500");
   });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the mouseenter and mouseleave events... .hover() provides a shortcut to do it

$("#flip1").hover(function () {
    $("#panel1").stop(true).slideToggle("500");
});

or to register all handlers at once

$("#flip1, #flip2, #flip3").hover(function () {
    $("#panel" + this.id.replace('flip', '')).stop(true).slideToggle("500");
});

Comments

0

You can use the hover event and register one handler using a start with selector, then gets its progressive and show the relative panel accordingly:

$("[id^=flip]").hover(function () {
    $("#panel" + this.id.replace('flip', '')).stop(true).slideToggle("500");
});

Demo: http://jsfiddle.net/IrvinDominin/C3kRp/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.