Daniel
Daniel

Reputation: 1466

FadeIn & FadeOut Conflicting Jquery

I was trying to show the paragraph on click to the h1 tag. When the user clicks h1 the paragraphs shows, but it's not working properly when I click h1 the paragraph shows and when I click again it hides, but when I do the same thing again the fade in and fade out effects works at the same time.

Here is my JSFIDDLE example: https://jsfiddle.net/9pLnkqz8/

Here is my HTML:

    <h1>REVEAL</h1>
            <p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
                proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>

Here is my JQUERY:

 (function(){

        $('p').hide();

        var object = {

            revealContent:$('p'),

            showReveal:function(){

                $('p').fadeIn();
                $('h1').on('click', object.hideReveal);

            },

            hideReveal:function(){

                $('p').fadeOut();

            }

        };

        $('h1').on('click', object.showReveal);

    })();

Upvotes: 1

Views: 182

Answers (4)

boszlo
boszlo

Reputation: 1286

Use the fadeToggle()

$('p').hide();


$('h1').click(function() {
  $('p').stop().fadeToggle()( "slow", function() {
  // Animation complete.
  });
});`

https://jsfiddle.net/9pLnkqz8/7/

Upvotes: 0

Mouneer
Mouneer

Reputation: 13489

edit your jquery to just toggle p element

(function(){

    $('p').hide();

    var object = {

        revealContent:$('p'),

        toggleReveal:function(){
            if($('p').is(":visible"))
                $('p').fadeOut();
            else
                $('p').fadeIn();                
        }

    };

    $('h1').on('click', object.toggleReveal);

})();

Edit:
we can make use of Jquery "fadeToggle" function and thanks for TrueBlueAussie we need to call stop to prevent previous animations

(function(){
    $('p').hide();
    $('h1').on('click', function(){
         $('p').stop().fadeToggle();
     });
})();

Upvotes: 0

Bhavin Solanki
Bhavin Solanki

Reputation: 4828

you can use this code :

  (function(){

        $('p').hide();


        $('h1').on('click', function(){
            $('p').fadeToggle('slow');
        });

    })();

Demo : https://jsfiddle.net/9pLnkqz8/3/

Upvotes: 0

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93611

You are creating a chain of event handlers by connecting click again and again (inside showReveal). fadeToggle() the state instead and use stop() to prevent previous animations from completing first.

$(function () {

    $('p').hide();

    $('h1').on('click', function(){
        $('p').stop().fadeToggle();
    });

});

JSFiddle: https://jsfiddle.net/TrueBlueAussie/9pLnkqz8/2/

Note, you are also using an IIFE instead of a DOM ready hander in the example so I changed that too as it is unneeded in the new shorter version. $(function(){ is just a shortcut for $(document).ready(function(){.

Upvotes: 6

Related Questions