简体   繁体   English

FadeIn和FadeOut冲突Jquery

[英]FadeIn & FadeOut Conflicting Jquery

I was trying to show the paragraph on click to the h1 tag. 我试图显示单击到h1标签的段落。 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. 当用户单击h1时,段落将显示,但是当我单击h1时,段落将显示不正常,当我再次单击时,段落将隐藏,但是当我再次执行相同的操作时,淡入和淡出效果将同时起作用。

Here is my JSFIDDLE example: https://jsfiddle.net/9pLnkqz8/ 这是我的JSFIDDLE示例: https ://jsfiddle.net/9pLnkqz8/

Here is my HTML: 这是我的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: 这是我的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 are creating a chain of event handlers by connecting click again and again (inside showReveal ). 通过一次又一次地单击click(在showReveal内部)来创建事件处理程序链。 fadeToggle() the state instead and use stop() to prevent previous animations from completing first. fadeToggle()改为使用状态,并使用stop()防止先前的动画首先完成。

$(function () {

    $('p').hide();

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

});

JSFiddle: https://jsfiddle.net/TrueBlueAussie/9pLnkqz8/2/ 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. 注意,在示例中,您还使用IIFE而不是DOM ready处理程序,因此我也进行了更改,因为新的较短版本不需要它。 $(function(){ is just a shortcut for $(document).ready(function(){ . $(function(){只是$(document).ready(function(){的快捷方式$(document).ready(function(){

you can use this code : 您可以使用以下代码:

  (function(){

        $('p').hide();


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

    })();

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

edit your jquery to just toggle p element 编辑您的jquery以仅切换p元素

(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 我们可以使用Jquery的“ fadeToggle”功能,感谢TrueBlueAussie,我们需要调用stop来防止以前的动画

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

Use the fadeToggle() 使用fadeToggle()

$('p').hide();


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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM