以下是JSFIDDLE的链接。我正在尝试使用向上和向下键在对话框中滚动。我可以通过使用鼠标滚动,但知道如何使用键上下滚动。这是我到目前为止所尝试的。如何让div上下滚动使用jquery
var element = document.getElementById("scroll-to-here");
element.scrollIntoView({block: "end", behavior: "smooth"});
其实我试图滚动15px;或向上或向下按下20px。
以下是JSFIDDLE的链接。我正在尝试使用向上和向下键在对话框中滚动。我可以通过使用鼠标滚动,但知道如何使用键上下滚动。这是我到目前为止所尝试的。如何让div上下滚动使用jquery
var element = document.getElementById("scroll-to-here");
element.scrollIntoView({block: "end", behavior: "smooth"});
其实我试图滚动15px;或向上或向下按下20px。
使用scrollTop
功能:
$("#yourElement").scrollTop(10);
你的情况
,增加或减少被按压滚轮顶视按钮。
var $el = $("#yourElement");
$el.scrollTop($el.scrollTop() - 10); //move the scrollbar upwards
看看这个例子:
试试这个:
$("#dialog-message").dialog({
modal: true,
draggable: false,
resizable: false,
position: ['center', 'top'],
show: 'blind',
hide: 'blind',
width: 400,
dialogClass: 'ui-dialog-osx',
buttons: {
"I've read and understand this": function() {
$(this).dialog("close");
}
}
});
$(document).keydown(function(e) {
switch(e.which) {
case 38: // up
var y = $('#to-scroll').scrollTop(); //your current y position on the page
$('#to-scroll').scrollTop(y-150);
break;
case 39: // right
break;
case 40: // down
var y = $('#to-scroll').scrollTop(); //your current y position on the page
$('#to-scroll').scrollTop(y+150);
break;
default: return; // exit this handler for other keys
}
//e.preventDefault(); // prevent the default action (scroll/move caret)
});
看看这个例子:
弹出窗口的高度将根据ajax内容增加,在这种情况下的元素也是未知的。 –
scrollTop的为u ndefined。另外我不想滚动到元素。我想在每个向上和向下滚动某个px的情况下,在向下键的情况下向上滚动某个px。 –
你有没有安装jQuery?另外,在加载页面时附加事件处理程序。在我的答案中看到现场示例。 – dpp
已安装,但我使用引导模型窗口弹出。在弹出窗口中滚动不起作用。 –