Amr*_*rhy 134 html javascript scroll
我有一个滚动div
,我希望有一个链接,当我点击它将强制它div
滚动查看里面的元素.我写了这样的JavasSript:
document.getElementById(chr).scrollIntoView(true);
Run Code Online (Sandbox Code Playgroud)
但这会在滚动div
自己的同时滚动所有页面.如何解决?
我想这样说
MyContainerDiv.getElementById(chr).scrollIntoView(true);
Run Code Online (Sandbox Code Playgroud)
小智 299
您需要获取要滚动到视图中的元素的顶部偏移量,相对于其父元素(滚动div容器):
var myElement = document.getElementById('element_within_div');
var topPos = myElement.offsetTop;
Run Code Online (Sandbox Code Playgroud)
变量topPos现在设置为滚动div的顶部与您希望可见的元素之间的距离(以像素为单位).
现在我们告诉div使用scrollTop
以下方式滚动到该位置:
document.getElementById('scrolling_div').scrollTop = topPos;
Run Code Online (Sandbox Code Playgroud)
如果您正在使用原型JS框架,那么您将执行以下相同的操作:
var posArray = $('element_within_div').positionedOffset();
$('scrolling_div').scrollTop = posArray[1];
Run Code Online (Sandbox Code Playgroud)
同样,这将滚动div,以便您希望看到的元素正好位于顶部(或者如果不可能,则尽可能向下滚动以使其可见).
Gle*_*lar 64
您必须在要滚动到的DIV中找到元素的位置,并设置scrollTop属性.
divElem.scrollTop = 0;
Run Code Online (Sandbox Code Playgroud)
更新:
向上或向下移动的示例代码
function move_up() {
document.getElementById('divElem').scrollTop += 10;
}
function move_down() {
document.getElementById('divElem').scrollTop -= 10;
}
Run Code Online (Sandbox Code Playgroud)
vsy*_*ync 30
var box = document.querySelector('.box'),
targetElm = document.querySelector('.boxChild'); // <-- Scroll to here within ".box"
document.querySelector('button').addEventListener('click', function(){
scrollToElm( box, targetElm , 600 );
});
/////////////
function scrollToElm(container, elm, duration){
var pos = getRelativePos(elm);
scrollTo( container, pos.top , 2); // duration in seconds
}
function getRelativePos(elm){
var pPos = elm.parentNode.getBoundingClientRect(), // parent pos
cPos = elm.getBoundingClientRect(), // target pos
pos = {};
pos.top = cPos.top - pPos.top + elm.parentNode.scrollTop,
pos.right = cPos.right - pPos.right,
pos.bottom = cPos.bottom - pPos.bottom,
pos.left = cPos.left - pPos.left;
return pos;
}
function scrollTo(element, to, duration, onDone) {
var start = element.scrollTop,
change = to - start,
startTime = performance.now(),
val, now, elapsed, t;
function animateScroll(){
now = performance.now();
elapsed = (now - startTime)/1000;
t = (elapsed/duration);
element.scrollTop = start + change * easeInOutQuad(t);
if( t < 1 )
window.requestAnimationFrame(animateScroll);
else
onDone && onDone();
};
animateScroll();
}
function easeInOutQuad(t){ return t<.5 ? 2*t*t : -1+(4-2*t)*t };
Run Code Online (Sandbox Code Playgroud)
.box{ width:80%; border:2px dashed; height:180px; overflow:auto; }
.boxChild{
margin:600px 0 300px;
width: 40px;
height:40px;
background:green;
}
Run Code Online (Sandbox Code Playgroud)
<button>Scroll to element</button>
<div class='box'>
<div class='boxChild'></div>
</div>
Run Code Online (Sandbox Code Playgroud)
请注意,浏览器支持不适合这个
var targetElm = document.querySelector('.boxChild'), // reference to scroll target
button = document.querySelector('button'); // button that triggers the scroll
// bind "click" event to a button
button.addEventListener('click', function(){
targetElm.scrollIntoView()
})
Run Code Online (Sandbox Code Playgroud)
.box {
width: 80%;
border: 2px dashed;
height: 180px;
overflow: auto;
scroll-behavior: smooth; /* <-- for smooth scroll */
}
.boxChild {
margin: 600px 0 300px;
width: 40px;
height: 40px;
background: green;
}
Run Code Online (Sandbox Code Playgroud)
<button>Scroll to element</button>
<div class='box'>
<div class='boxChild'></div>
</div>
Run Code Online (Sandbox Code Playgroud)
.box {
width: 80%;
border: 2px dashed;
height: 180px;
overflow-y: scroll;
scroll-behavior: smooth; /* <--- */
}
#boxChild {
margin: 600px 0 300px;
width: 40px;
height: 40px;
background: green;
}
Run Code Online (Sandbox Code Playgroud)
<a href='#boxChild'>Scroll to element</a>
<div class='box'>
<div id='boxChild'></div>
</div>
Run Code Online (Sandbox Code Playgroud)
Nik*_*hak 18
设置ScrollTop
确实给出了所需的结果,但滚动非常突然。使用jquery
平滑滚动不是一种选择。所以这里有一种支持所有主要浏览器的本地方式来完成工作。参考 - caniuse
// get the "Div" inside which you wish to scroll (i.e. the container element)
const El = document.getElementById('xyz');
// Lets say you wish to scroll by 100px,
El.scrollTo({top: 100, behavior: 'smooth'});
// If you wish to scroll until the end of the container
El.scrollTo({top: El.scrollHeight, behavior: 'smooth'});
Run Code Online (Sandbox Code Playgroud)
就是这样!
这是一个值得怀疑的工作片段 -
// get the "Div" inside which you wish to scroll (i.e. the container element)
const El = document.getElementById('xyz');
// Lets say you wish to scroll by 100px,
El.scrollTo({top: 100, behavior: 'smooth'});
// If you wish to scroll until the end of the container
El.scrollTo({top: El.scrollHeight, behavior: 'smooth'});
Run Code Online (Sandbox Code Playgroud)
document.getElementById('btn').addEventListener('click', e => {
e.preventDefault();
// smooth scroll
document.getElementById('container').scrollTo({top: 175, behavior: 'smooth'});
});
Run Code Online (Sandbox Code Playgroud)
/* just some styling for you to ignore */
.scrollContainer {
overflow-y: auto;
max-height: 100px;
position: relative;
border: 1px solid red;
width: 120px;
}
body {
padding: 10px;
}
.box {
margin: 5px;
background-color: yellow;
height: 25px;
display: flex;
align-items: center;
justify-content: center;
}
#goose {
background-color: lime;
}
Run Code Online (Sandbox Code Playgroud)
更新:正如您在评论Element.scrollTo()
中所见,IE11似乎不支持。所以如果你不关心 IE11(你真的不应该),请随意在你的所有项目中使用它。请注意,存在对 Edge 的支持!所以你并没有真正把你的 Edge/Windows 用户抛在后面;)
spi*_*ero 12
我们可以在不使用 JQuery 和其他库的情况下解决这个问题。
为此我编写了以下代码:
你有类似的结构->
<div class="parent">
<div class="child-one">
</div>
<div class="child-two">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
scrollToElement() {
var parentElement = document.querySelector('.parent');
var childElement = document.querySelector('.child-two');
parentElement.scrollTop = childElement.offsetTop - parentElement.offsetTop;
}
Run Code Online (Sandbox Code Playgroud)
我们可以轻松地重写此方法,将父级和子级作为参数传递
小智 10
代码应该是:
var divElem = document.getElementById('scrolling_div');
var chElem = document.getElementById('element_within_div');
var topPos = divElem.offsetTop;
divElem.scrollTop = topPos - chElem.offsetTop;
Run Code Online (Sandbox Code Playgroud)
您想要滚动子顶部位置和div顶部位置之间的差异.
使用以下方式访问子元素:
var divElem = document.getElementById('scrolling_div');
var numChildren = divElem.childNodes.length;
Run Code Online (Sandbox Code Playgroud)
等等....
要将元素滚动到div的视图中,只有在需要时,才能使用此scrollIfNeeded
函数:
function scrollIfNeeded(element, container) {
if (element.offsetTop < container.scrollTop) {
container.scrollTop = element.offsetTop;
} else {
const offsetBottom = element.offsetTop + element.offsetHeight;
const scrollBottom = container.scrollTop + container.offsetHeight;
if (offsetBottom > scrollBottom) {
container.scrollTop = offsetBottom - container.offsetHeight;
}
}
}
document.getElementById('btn').addEventListener('click', ev => {
ev.preventDefault();
scrollIfNeeded(document.getElementById('goose'), document.getElementById('container'));
});
Run Code Online (Sandbox Code Playgroud)
.scrollContainer {
overflow-y: auto;
max-height: 100px;
position: relative;
border: 1px solid red;
width: 120px;
}
body {
padding: 10px;
}
.box {
margin: 5px;
background-color: yellow;
height: 25px;
display: flex;
align-items: center;
justify-content: center;
}
#goose {
background-color: lime;
}
Run Code Online (Sandbox Code Playgroud)
<div id="container" class="scrollContainer">
<div class="box">duck</div>
<div class="box">duck</div>
<div class="box">duck</div>
<div class="box">duck</div>
<div class="box">duck</div>
<div class="box">duck</div>
<div class="box">duck</div>
<div class="box">duck</div>
<div id="goose" class="box">goose</div>
<div class="box">duck</div>
<div class="box">duck</div>
<div class="box">duck</div>
<div class="box">duck</div>
</div>
<button id="btn">scroll to goose</button>
Run Code Online (Sandbox Code Playgroud)
其他答案都没有解决我的问题。
我玩弄了scrollIntoView参数并设法找到了解决方案。设置inline
tostart
和block
tonearest
可以防止父元素(或整个页面)滚动:
document.getElementById(chr).scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'start'
});
Run Code Online (Sandbox Code Playgroud)
如果使用的是jQuery,则可以使用以下内容滚动动画:
$(MyContainerDiv).animate({scrollTop: $(MyContainerDiv).scrollTop() + ($('element_within_div').offset().top - $(MyContainerDiv).offset().top)});
Run Code Online (Sandbox Code Playgroud)
动画是可选的:您还可以采用上面计算的scrollTop值,并将其直接放在容器的scrollTop属性中。
小智 5
使用 jQuery 和 animate 的另一个示例。
var container = $('#container');
var element = $('#element');
container.animate({
scrollTop: container.scrollTop = container.scrollTop() + element.offset().top - container.offset().top
}, {
duration: 1000,
specialEasing: {
width: 'linear',
height: 'easeOutBounce'
},
complete: function (e) {
console.log("animation completed");
}
});
Run Code Online (Sandbox Code Playgroud)