<div style=width:200px;height:200px;border-style:solid;overflow:scroll;>
<div style=width:500px;height:500px;>
Text, something
</div>
</div>
這是例如HTML代碼。我需要將div的視圖(Scroll)移動到事件的右上角(例如點擊按鈕時)。
<div style=width:200px;height:200px;border-style:solid;overflow:scroll;>
<div style=width:500px;height:500px;>
Text, something
</div>
</div>
這是例如HTML代碼。我需要將div的視圖(Scroll)移動到事件的右上角(例如點擊按鈕時)。
嘗試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:200px; border-style:solid; overflow:scroll;">
<div class="scrollthis" style="width:500px; height:500px;">
Text, something
</div>
</div>
<button id="scroll">Scroll</button>
我找到了一個解決方案:
$(MainDiv).scrollLeft(DivInsideWidth);
$(MainDiv).scrollTop(0);
scrollLeft
和scrollTop
是正確的做法,但你可以爲更好的靈活性添加動畫和動態值:https://jsfiddle.net/twey5d7u/
這一個不滾動到右上角。它只是滾動到右邊緣。嘗試滾動到左下角並點擊滾動按鈕。 –