How do I use jQuery to create hovering elements?

user23524697

I'm new to jQuery and learning how to make it interact with my actual code so the pages are more interactive but I'm a bit confused with all the functions. I understand the principles of jQuery which I find quite easy but, I just don't really know how to call functions. Right now I'm working on a test page to practice and get used to it.

I'm trying to accomplish 2 things I have seen on a shopping website.

First thing: Make words appear on mouseover an image like this example

  • Like you can see, when cursor hover the image, "quick view" appears. That's the first thing I'm trying to do.

Second thing: Make a box appear with more details and information when image is clicked and make appeared box disappear when "closed" is selected (please use previous link example to see the effect I'm trying to accomplish).

So to test my skills, I have started this test code to try and accomplish what I want.

< script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" > < /script>
<script>

$( img ).mouseenter( handlerIn ).mouseleave( handlerOut );


</script >
.img {
  background-color: #f6f6f6;
  width: 241px;
  height: 341px;
}
<body>
  <div class="img">an image
    <div>
</body>

I know it has something to do with this $( selector ).mouseenter( handlerIn ).mouseleave( handlerOut ); for the first thing I'm trying to do.

For the second thing I know it has something to do with the .toggle()

Be nice with me guys, I'm a rookie and just starting to figure out how to start using jQuery.

Thijs Riezebeek

You could use the hover function: $( selector ).hover( handlerIn, handlerOut ) see: http://api.jquery.com/hover/

  // You want this code to run after the document has been loaded
  jQuery(document).ready(function() {
        // Use .img to target all elements with the class 'img'
        $('.img').hover(function() {
            // This function is called when the mouse hovers over the element
            // Because this is function is an eventListener that is attached
            // to an element, you can call 'this' to target the element
            // that is being hovered over
            var image = jQuery(this);

            // Use the .append() function to insert content into an element
            image.append("<div class='quick-view'>Quick view</div>")
        },
        function() {
            // This function is called when the mouse leaves the element
            // In here we want to remove the quick-view element, so
            // first we have to find it. Again, use 'this' to
            // target the element that is hovered over.
            var image = jQuery(this);

            // Use the .find() function to look for the quick-view element
            // inside the element with the .img class:
            var quick-view = image.find('.quick-view');

            // Now to remove the quick-view element, use .remove()
            quick-view.remove();
        });
    });

The code below will attach an onClick listener to the quick-view element, so that you can tell your script what to do when the quick-view element is clicked.
Note that we are not using

 jQuery('.quick-view').click(function() {});

This is because this will attach the click function to all elements with the 'quick-view' class, after the document is loaded. But after the document is loaded, there are no elements with the 'quick-view' class. These elements are created dynamically. Therefore we have to use a special jQuery function, the .on() function, which we will attach to the body element. See http://api.jquery.com/on/

jQuery('body').on('click', '.quick-view', function() {
    // Even though the listerner seems to be attached to the body
    // you can still call 'this' to target the quick-view element.
    // In here you can add the box with more details to the body
    jQuery('body').append("<div class='more-info'>more info</div>")
});

The functionality for the close element can be implemented in much the same way (always use .on() for elements that aren't created on load, but are created dynamically).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I use jquery to create an overlay contact form

Jquery hovering multiple elements

Javascript: How do i target selected elements in a slideshow when hovering over one element?

How do I stop my <li> elements from moving while hovering over another <li>?

How do I style two elements into different styles by hovering over one of them? Without JavaScript

How do I create new elements in html?

How to use <i> element for hovering container div

How do I add HTML elements in this jQuery?

How do I trigger a click when hovering over a element for x seconds, using JQuery?

How do I make this infinite jquery animation end when the user is no longer hovering?

How do I create a percentage counter in jQuery?

How do I create a textarea countdown with jQuery?

How do I create a jquery function with callbacks

How do i create a table using Jquery

How do I use tkinter to create a menubar?

How do I create and use a Task in Verilog

How do I use a "for loop" to create an object?

How do I use jquery ui with requirejs

How do I use a header location in jQuery?

How do I use jQuery in Svelte

How do I use for loop with jQuery?

How do I use jquery in an angular directive

How do i use the jquery on my site?

How do I create a list with elements from 1 to N in prolog

How do I create a javascript function that add html elements dynamically?

How do I create concave shaped borders around <div> elements?

In JavaScript how do I create a list of differences of array elements elegantly?

How do I create another column with value of different elements in a column?

How do i create an input that adds elements to an array?