我手里有以下两个:
#elementA {
position: absolute;
width: 200%;
height: 100%;
background: linear-gradient(to right, rgba(100,0,0,0.3), rgba(0,0,250,0.3));
z-index: 250;
}
#containerofA {
position: fixed;
width: 100%;
height: 100%;
z-index: 240;
padding-bottom: 100px; // to hide the horizontal scrollbar
overflow-x: scroll;
}
作为 div
的 elementA
再次作为 div
出现在 containerofA
中。 elementA
的宽度是其容器的两倍,可通过支持触摸的设备滚动。
这些事情已经发生了。我所追求的是让 elementA
最初滚动到中间。我希望它最初位于 -50%
,或者在加载时向右滚动 50%
。
在 JavaScript、CSS 或 HTML 中:怎么做?
window.scroll(offset-x, offset-y);
如果可以的话,变体会滚动整个窗口。这真的应该是一件容易的事,但我看不出如何......
最佳答案
使用scrollLeft dom元素的属性。
document.getElementById('containerofA').scrollLeft = 250;
关于javascript - 滚动 HTML 元素的内容,而不是整个窗口对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23565515/