简体   繁体   English

jQuery函数可逆转onClick事件

[英]jQuery function to reverse onClick event

Can someone help me understand this better. 有人可以帮助我更好地理解这一点。 I'm simply fading out the box element in the DOM when the button is clicked by using the onclick. 当使用onclick单击按钮时,我只是在DOM中淡化box元素。

Is there a simple way to cause the box to fade back in when the same button is clicked again? 当再次单击同一按钮时,是否有一种简单的方法可以使框退回?

 function fade() { $("#box").fadeOut(); }; 
  <div id="box" style="height:150px; width:150px; background-color:orange; margin:25px; opacity: 1;"></div> <button id="button3" onclick="fade();">Fade</button> 

切换衰落功能:

$("#box").fadeToggle();

Change your function to use toggle : 更改功能以使用toggle:

function fade() {
    $("#box").fadeToggle();
}

fadeToggle() fadeToggle()

 function fade(){ $('#box').fadeToggle(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="box" style="height:150px; width:150px; background-color:orange; margin:25px; opacity: 1;"></div> <button id="button3" onclick="fade();">Fade</button> 

请在此处使用fadeToggle()而不是fadeOut()完整参考: http : fadeToggle()

You can possibly make use of the 'is(":visible")' to check display status on the FadeOut and do the FadeIn instead. 您可以使用'is(“:visible”)'来检查FadeOut上的显示状态并改为执行FadeIn。

function fade() {
    var mybox = $("#box");
    if (mybox.is(":visible"))
        mybox.fadeOut();
    else mybox.fadeIn();
};

Check out the jsfiddle here 这里查看jsfiddle

 $( function() { x=1; } ); function fade() { if(x==1){ $("#box").fadeOut(1000) && (x=0); } else $("#box").fadeIn(1000) && (x=1); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="box" style="height:150px; width:150px; background-color:orange; margin:25px; opacity: 1;"></div> <button id="button3" onclick="fade()">Fade</button> 

please refer the link: jQuery .fadeTo() -- Fade back in after fade out for another example related to this. 请参考链接: jQuery .fadeTo()-淡出后淡入,以获取与此相关的另一个示例。

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

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