javascript - 如何在按键上的一行代码中一个接一个地滚动查看节点列表中的每个项目?

标签 javascript

我有一个页面,我想用一行代码将每个元素滚动到 View 中。

我目前的代码:

window.addEventListener("keyup", function (e) { e.keyCode==13 ?document.querySelectorAll("ins").scrollIntoView():void})

我以为我可以让它与单个元素一起工作,但我没有得到关键事件(没关系,它不是本地文件上的脚本):

window.addEventListener("keyup", function (e) { console.log("test") })

最佳答案

var current=0;
function scrollIntoView(index){
    let node=document.querySelectorAll("ins")[index];
    if(node){
         node.scrollIntoView();
     }
}
window.addEventListener("keyup", function (e) { 
      e.keyCode==13 ? scrollIntoView(current++) :void
});

在一行中:

var current=0;function scrollIntoView(index){let node=document.querySelectorAll("ins")[index];if(node){node.scrollIntoView();         }}window.addEventListener("keyup", function (e) { e.keyCode==13 ? scrollIntoView(current++) :void })

var current=0;

function scrollIntoView(index){
   let node=document.querySelectorAll("ins")[index];
   if(node){
       node.scrollIntoView();
   }
};

window.addEventListener("keyup", function (e) {
   current>=document.querySelectorAll("ins").length ? current = 0 : null;
   e.keyCode==13 && document.querySelectorAll("ins").length && scrollIntoView(current++);
});
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br>
<br>
<ins>first index</ins><br>
<br><br>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.<br><br><br>
<ins>second index</ins><br><br><br>
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br><br><br>
<ins>third index</ins><br><br><br>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.<br><br><br>

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.<br><br><br>

There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text.

关于javascript - 如何在按键上的一行代码中一个接一个地滚动查看节点列表中的每个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55553536/

相关文章:

javascript - 当我在 Angular js 的 Post 请求中传递 session ID 时出现 404 错误

javascript - jQuery setAttribute 中的图像没有改变

javascript - Meteor:Router.route() 内部的 Session.set 在 'client' 页面中使用时未定义

javascript - 在运行该函数之前等待一分钟?

考虑Request.UserLanguages[0]的javascript日期

javascript - 具有多个返回值的 Jasmine spyOn

javascript - 找不到模块 : Error: Can't resolve './home.html' in '/Users/username/Documents/Privat/ionic3-barcodev3/src/app/home'

javascript - 日期的正则表达式模式匹配

Javascript 语法 []() 和 ()()

javascript - 用于提交的输入类型范围数组值和 ID