Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hover effect on div using jquery

Tags:

jquery

HTML:

<div id="container">
    <div class="section one"></div>
        <div class="inner"></div>
    <div class="section two"></div>
        <div class="inner"></div>
    <div class="section three"></div>
        <div class="inner"></div>
    <div class="section four"></div>
        <div class="inner"></div>
</div>

Explanation:

On hover each div ( section ) it should hide and the next div( inner ) should display. On mouseleave it show as old. For this I use moseenter and mouseleave events.

Problem

On mouseenter the div blinks (happends the both events together).

Jquery

$(document).ready(function () {
    $(".inner").hide();
    $(".section").mouseenter(function () {
        $(this).hide();
        $(this).next(".inner").show();
    });
    $(".section").mouseleave(function () {
        $(this).show();
        $(this).next(".inner").hide();
    });
});

Please check the fiddle given below for more details

DEMO

like image 528
user3847937 Avatar asked Dec 29 '25 17:12

user3847937


2 Answers

This is happening because of the .hide is also firing the mouse out function..

Here is your updated Fiddle: http://jsfiddle.net/hdehzec0/12/

Consider this structure for your HTML:

<div id="container">
    <div class="section one"><div class="inner"></div></div>

    <div class="section two"><div class="inner"></div></div>

    <div class="section three"><div class="inner"></div></div>

    <div class="section four"><div class="inner"></div></div>

</div>

and your JQuery :

$(document).ready(function () {
    $(".inner").hide();
    $(".section").hover(
     function () {
        $(this).find(".inner").show();
     }, function() {
        $(this).find(".inner").hide();
    });
});
like image 135
JF it Avatar answered Jan 01 '26 12:01

JF it


Fiddle : http://jsfiddle.net/hdehzec0/16/

When you enter .section it disappears therefore mouseleave function gets triggered. Instead of using .section on mouseleave use .inner since your mouse is

$(document).ready(function () {
        $(".inner").hide();
        $(".section").mouseenter(function () {
            $(this).hide();
            $(this).next(".inner").show();
        });
        $(".inner").mouseleave(function () {
            $(this).hide();
            $(this).prev(".section").show();
        }); 

});
like image 38
Dany Minassian Avatar answered Jan 01 '26 10:01

Dany Minassian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!