javascript - jQuery fade out HTML text when replacing it with new text instead of just appearing -
i have following little script...
$(document).ready(function() { $('#search').bind('keyup', function(event) { if($('#search').val().length == '0') { $("#products").html("all products"); } else { $("#products").html("searching products"); } }); });
when #search
has in it, shows 'searching products', , when empty, shows 'all products'. problem changes no fade effect, looks bit choppy. how have fade 1 other, no matter direction (from empty typed or typed empty) it's going?
try
$(document).ready(function() { var search = $("#search") , products = $("#products"); search.bind('keyup', function(event) { if (search.val().length === 0 || search.val().split(/\s/).every(function(val) { return val === "" })) { if (products.html() === "all products") { return }; products .stop(true, false) .html("all products") .fadeout(-1000) .fadein(1000); } else { if (search.val().length > 0 && products.html() === "searching products") { return }; products .stop(true, false) .html("searching products") .fadeout(-1000) .fadein(1000); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <input type="text" id="search" /> <br /> <div id="products"></div>
Comments
Post a Comment