javascript - FadeIn & FadeOut Conflicting Jquery -


i trying show paragraph on click h1 tag. when user clicks h1 paragraphs shows, it's not working when click h1 paragraph shows , when click again hides, when same thing again fade in , fade out effects works @ same time.

here jsfiddle example: https://jsfiddle.net/9plnkqz8/

here html:

    <h1>reveal</h1>             <p>                 lorem ipsum dolor sit amet, consectetur adipisicing elit, sed 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 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);      })(); 

you creating chain of event handlers connecting click again , again (inside showreveal). fadetoggle() state instead , use stop() prevent previous animations completing first.

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

jsfiddle: https://jsfiddle.net/trueblueaussie/9plnkqz8/2/

note, using iife instead of dom ready hander in example changed unneeded in new shorter version. $(function(){ shortcut $(document).ready(function(){.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -