繁体   English   中英

滚动HTML元素而不是整个window对象的内容

[英]Scrolling the content of an HTML element, rather than the whole window object

我手中有以下两个:

#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;
}

elementA作为一个div出现内部containerofA作为一个div一次。 elementA的宽度是其容器的两倍,并且将使用具有触摸功能的设备进行滚动。

这些事情已经发生。 我要做的是让elementA最初滚动到中间。 我希望它要么最初位于-50%位置,要么在加载时向右滚动50%

在JavaScript,CSS或HTML中:该怎么做?

window.scroll(offset-x, offset-y); 如果可能的话,变体会滚动整个窗口。 这确实是一件容易的事,但是我看不到如何...

使用dom元素的scrollLeft属性。

http://jsfiddle.net/hCN7n/

document.getElementById('containerofA').scrollLeft = 250;

尝试这样的事情:

 $(window).load(function() {
    $(element).scrollLeft(offset-left);
});

您可以使用scrollTopscrollLeft属性。

为什么不这样做:

.Container{
position:fixed;
width:100%;
height:100%;
overflow-x:scroll;
overflow-y:hidden;
}

.Elementcontained{
position:absolute;
margin:auto;
left:0;right:0;bottom:0;top:0;
width:200%;
height:100%;
}

那应该工作吗?

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM