javascript - Create an effect to make element fadeIn and fadeOut without using fadeToggle() -
javascript - Create an effect to make element fadeIn and fadeOut without using fadeToggle() -
you see, utilize if else
accomplish goal, not works. think might create mistakes. these codes w3schools, , changed look.
class="snippet-code-js lang-js prettyprint-override">$(document).ready(function() { if ($("button").text() == "click fade in boxes") $("button").click(function() { $("#div1").fadein(); $("#div2").fadein(); $("#div3").fadein(); $("button").text("click fade out boxes"); }); else $("button").click(function() { $("#div1").fadeout(); $("#div2").fadeout(); $("#div3").fadeout(); $("button").text("click fade in boxes"); }); });
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <p>demonstrate fadein() different parameters.</p> <button>click fade in boxes</button> <br> <br> <div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div> <br> <div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div> <br> <div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>
you should set if/else
within click
event. because, when set outside of click
(the way did), 1 of click
event handlers attached. see:
$(document).ready(function() { // in document.load, status true if ($("button").text() == "click fade in boxes") // have function attached $("button").click(function() { // stuffs }); // 1 false else // line, never executed $("button").click(function() { // stuffs }); });
as can see, first grouping of code (fadein()
stuffs) executed. so, alter code to:
class="snippet-code-js lang-js prettyprint-override"> $(document).ready(function() { $("button").click(function() { if ($("button").text() == "click fade in boxes") { $("#div1").fadein(); $("#div2").fadein(); $("#div3").fadein(); $("button").text("click fade out boxes"); } else { $("#div1").fadeout(); $("#div2").fadeout(); $("#div3").fadeout(); $("button").text("click fade in boxes"); } }); });
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <p>demonstrate fadein() different parameters.</p> <button>click fade in boxes</button> <br> <br> <div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div> <br> <div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div> <br> <div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>
and if looking shorter , cleaner code (as other users mentioned), can (without huge alter in code):
class="snippet-code-js lang-js prettyprint-override"> $(document).ready(function() { $("button").click(function() { if ($("button").text() == "click fade in boxes") { $("#div1, #div2, #div3").fadein(); // impact div1, div2, , div3 $("button").text("click fade out boxes"); } else { $("#div1, #div2, #div3").fadeout(); // 1 too. $("button").text("click fade in boxes"); } }); });
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <p>demonstrate fadein() different parameters.</p> <button>click fade in boxes</button> <br> <br> <div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div> <br> <div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div> <br> <div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>
javascript jquery html web
Comments
Post a Comment