Javascript 按键滚动(回车)
我试图让我的代码在按键(输入键13)上滚动到一个部分。我甚至更希望当用户键入“launch”(有或没有按回车键)时,它会滚动到下一部分。目前,它只有在按两次enter键后才会滚动Javascript 按键滚动(回车),javascript,scroll,onkeypress,Javascript,Scroll,Onkeypress,我试图让我的代码在按键(输入键13)上滚动到一个部分。我甚至更希望当用户键入“launch”(有或没有按回车键)时,它会滚动到下一部分。目前,它只有在按两次enter键后才会滚动 (function ($) { document.getElementById("launch").addEventListener("keypress", enterKey); function enterKey(e) { if (e.keyCode == 13) {
(function ($) {
document.getElementById("launch").addEventListener("keypress", enterKey);
function enterKey(e) {
if (e.keyCode == 13) {
getInfo();
}
} false;
function getInfo() {
window.smoothScroll = function (target) {
var scrollContainer = target;
do { //find scroll container
scrollContainer = scrollContainer.parentNode;
if (!scrollContainer) return;
scrollContainer.scrollTop += 1;
} while (scrollContainer.scrollTop == 0);
var targetY = 0;
do { //find the top of target relatively to the container
if (target == scrollContainer) break;
targetY += target.offsetTop;
} while (target = target.offsetParent);
scroll = function (c, a, b, i) {
i++; if (i > 30) return;
c.scrollTop = a + (b - a) / 30 * i;
setTimeout(function () { scroll(c, a, b, i); }, 5);
}
// start scrolling
scroll(scrollContainer, scrollContainer.scrollTop, targetY, 0);
}
}