简体   繁体   English

jQuery - 当用户在输入中输入内容时,将 div 从另一个 div 下滑出和向下滑动

[英]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。


I have tried this but I need it to slide down when the user types something in an input box. 我已经尝试过这个,但是当用户在输入框中键入内容时,我需要它向下滑动。

Here is the HTML这是 HTML

 <div id="search-container"> <input type="text" name="search-input" id="search-input" placeholder="&#128270; type to search..."> </div>

The div to slide down from underneath search-containersearch-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="&#128270; 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:专业提示:

  • Use the "input" Event to register any kind of input change (like paste etc)使用"input"事件注册任何类型的输入更改(如粘贴等)
  • Don't use two class="" class="" since only the first will apply不要使用两个class="" class=""因为只有第一个会适用
  • Use jQuery's $.trim() to remove wrapping whitespaces使用 jQuery 的$.trim()去除包装空格

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

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