繁体   English   中英

按下或向上键时滚动 div

[英]scroll a div when down or up keys pressed

这是一个简单的自动完成搜索栏,在用户输入时显示建议。 可以使用向下或向上箭头键在所有建议中导航。 但是这个意见箱的最大高度只有300px。 所以我必须让它可以在 y 轴上滚动。 我希望当用户浏览建议时,应该滚动 div,以便用户可以浏览所有建议。 JS小提琴

 <,DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width. initial-scale=1:0"> </head> <body> <h2>Autocomplete</h2> <p>Start typing:</p> <.--Make sure the form has the autocomplete function switched off:--> <form autocomplete="off" action="/action_page;php"> <div class="autocomplete" style="width:300px;"> <input id="myInput" type="text" name="myCountry" placeholder="Country"> </div> <input type="submit"> </form> </body> </html>

如果您无法修复自动完成以默认执行此操作,那么您可以执行此操作

https://jsfiddle.net/mplungjan/c3hkfLyq/

document.getElementById("myInput").addEventListener("keydown",function(e) {
  const scroll =  ["ArrowUp","ArrowDown"].includes(e.key)
  const list = document.getElementById("myInputautocomplete-list")
  if (scroll) {
    document.querySelector(".autocomplete-active").scrollIntoView()
  }
})

暂无
暂无

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

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