简体   繁体   English

如何使用检测键功能转到 Javascript 中的下一部分?

[英]How to I use the detect key function to go to the next section in Javascript?

So I'm playing with detecting keystrokes in JavaScript and I thought it would be cool to add a function to my site that would go to the next section when the down arrow is pressed and go to the previous section when the up arrow is pressed, I'm not sure how I should go about this, the source code is a bit too long to put here so my site is just https://www.hukari.tech/ , if any of you have ideas, please let me know.所以我正在玩检测 JavaScript 中的击键,我认为向我的网站添加一个函数会很酷,当按下向下箭头时会转到下一部分,当按下向上箭头时会转到上一部分,我不知道我应该怎么做,源代码有点长,不能放在这里,所以我的网站只是https://www.hukari.tech/ ,如果你们有任何想法,请告诉我. Thanks so much :)非常感谢 :)

This is a good start, but depending on how you have your site structured you will have to handle navigation differently.这是一个好的开始,但根据您的网站结构,您将不得不以不同的方式处理导航。 You can use cookies or local storage to track which page was last visited to send the client to the previous page.您可以使用 cookie 或本地存储来跟踪上次访问的页面,以将客户端发送到上一个页面。 You can use window.history.back() to simulate the back button being pressed.您可以使用window.history.back()来模拟被按下的后退按钮。 If it is a single page site, you can scroll to different tags on the page, keeping track of where the client is with a global variable the keydown function checks.如果它是单页站点,您可以滚动到页面上的不同标签,使用 keydown 函数检查的全局变量跟踪客户端的位置。

 const body = document.querySelector("body"); const loadNextPage = () => body.innerText = "Next"; const loadPreviousPage = () => body.innerText = "Previous"; window.addEventListener("keydown", e => { if(e.key == "ArrowUp") { e.preventDefault(); //to prevent it from doing native scroll loadPreviousPage(); } if(e.key == "ArrowDown") { e.preventDefault(); //to prevent it from doing native scroll loadNextPage(); } });
 * { background: lightgray; }

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

相关问题 如何使用JavaScript检测iOS键盘的“下一个”键 - How to detect iOS keyboard 'next' key with javascript 如何等待 function 执行然后 go 下一个 javascript - how to wait for function to execute and then go next javascript 如果我转到下一个滑块,请滚动到某个部分的顶部? - Scroll to top of a section if i go to the next slider? 如何在纯 javascript function 中将 i18next 与命名空间一起使用? - How to use i18next with namespace in pure javascript function? 在JavaScript中,如何使用函数参数作为对象的键? - In JavaScript how can I use a function parameter as the key to an object? 如何在不手动调用 javascript 中的 function 的情况下使用 ajax 自动刷新页面的一部分? - How do I use ajax to auto refresh a section of page without manually invoking a function within javascript? 如何用我自己的 JavaScript function 替换 Tab 键移动到下一个 HTML 文本框的默认操作 - How do I replace the default action of Tab Key to move to next HTML textbox with my own JavaScript function 如何在标头部分中将标头功能与javascript一起使用? - How to use header function together with javascript in head section? 用JavaScript检测当前部分 - Detect current Section with javascript 如何在javascript部分中使用django模板变量? - how can I use django templates variable in javascript section?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM