我有一个容器,里面有一个列表。列表项可以被拖动,跟随鼠标移动。
该容器具有滚动功能:
overflow-y: scroll;
通过设置这个属性,Chrome会自动将
overflow-x
属性设置为'auto'。如果我设置overflow-x: visible
,Chrome会忽略它。如果我设置overflow-x: hidden
,那么显然项目会被裁剪。当我将列表项拖出容器的左侧或上侧时,它会被裁剪到容器的边缘。如果我将其从右侧或底部边缘拖出,则容器会滚动以适应它。我希望该项能够在不被裁剪的情况下被拖出容器,并且不触发滚动。
考虑到容器必须设置为
overflow-y: scroll
,这反过来又强制Chrome设置overflow-x: auto
,我是否可以实现这一点,还是不可能呢?Codepen:http://codepen.io/Pedr/pen/azLWeY 注:我知道我可以通过使用填充来偏移容器(使容器的限制实际上超出其可见边缘),但在我的情况下,这不是一个选择。
$(function() {
$('.Wrapper').mousemove(function(event){
$('.Item').offset({left: event.pageX, top: event.pageY});
});
})
html,
body {
height: 100%;
}
.Wrapper {
width: 100%;
height: 100%;
position: absolute;
}
.Container {
background: grey;
position: absolute;
width: 50%;
left: 25%;
min-height: 100%;
overflow-y: scroll;
overflow-x: hidden; // Clips Item
// If left at auto it will clip the item on the top and left edge and scroll if the item overlaps the bottom or right edge.
}
.Item {
padding: 20px;
background: red;
position: absolute;
width: 600px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Wrapper">
<div class="Container">
<div class="Item">ITEM</div>
</div>
</div>