<div style=width:200px;height:200px;border-style:solid;overflow:scroll;>
<div style=width:500px;height:500px;>
Text, something
</div>
</div>
这是示例html代码。我需要将div的视图(滚动)移动到事件的右上角(例如单击按钮)。
试试jQuery.scrollLeft()。下面是工作代码。
$(function() {
$('#scroll').click(function() {
var $scroll = $('.scrollthis');
$scroll.parent().scrollLeft($scroll.width());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width:200px; height:150px; border-style:solid; overflow:scroll;">
<div class="scrollthis" style="width:500px; height:500px;">
Text, something
</div>
</div>
<button id="scroll">Scroll</button>
scrollLeft
和scrollTop
是正确的方法,但您可以添加动画和动态值以获得更好的灵活性:https://jsfiddle.net/twey5d7u/
我找到了一个解决方案:
$(MainDiv).scrollLeft(DivInsideWidth);
$(MainDiv).scrollTop(0);