[英]jQuery - Slide div out and down from under another div when user enters something into an input
I want to slide a div down from underneath another div when a user types something into a div.当用户在 div 中键入内容时,我想从另一个 div 下方向下滑动一个 div。
Here is the HTML这是 HTML
<div id="search-container"> <input type="text" name="search-input" id="search-input" placeholder="🔎 type to search..."> </div>
The div to slide down from underneath search-container
从
search-container
下方向下滑动的 div
<div class="container" id="search-result-container" class="hidestuff" >
Some JS in the div search-result-container
that might be usefull div
search-result-container
中的一些 JS 可能有用
<script> $('#search-result-container').hide(); $('#search-input') .on('keyup', function(e) { var input = $(this).val(); input.length ? $('#search-result-container').show() : $('#search-result-container').hide(); }) </script>
Any Ideas on how this could be achieved?关于如何实现这一目标的任何想法? Many Thanks
非常感谢
The .slide<Down/Up>()
method could be useful here .slide<Down/Up>()
方法在这里很有用
$('#search-input').on('input', function(e) { var input = $.trim( this.value ); if ( input.length > 0 ) $('#search-result-container').stop().slideDown(); else $('#search-result-container').stop().slideUp(); });
.hidestuff { display: none; background: #ddd; padding: 20px; }
<script src="//code.jquery.com/jquery-3.1.0.js"></script> <div id="search-container"> <input type="text" name="search-input" id="search-input" placeholder="🔎 type to search..."> </div> <div class="container hidestuff" id="search-result-container"> Some JS in the div search-result-container that might be usefull </div>
Pro tips:专业提示:
"input"
Event to register any kind of input change (like paste etc)"input"
事件注册任何类型的输入更改(如粘贴等)class="" class=""
since only the first will applyclass="" class=""
因为只有第一个会适用$.trim()
to remove wrapping whitespaces$.trim()
去除包装空格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.