2016-07-20 35 views
1

功能移動滾動到DIV的右上角jQuery的

<div style=width:200px;height:200px;border-style:solid;overflow:scroll;> 
 
    <div style=width:500px;height:500px;> 
 
    Text, something 
 
    </div> 
 
</div>

這是例如HTML代碼。我需要將div的視圖(Scroll)移動到事件的右上角(例如點擊按鈕時)。

回答

1

嘗試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>

+0

這一個不滾動到右上角。它只是滾動到右邊緣。嘗試滾動到左下角並點擊滾動按鈕。 –

1

我找到了一個解決方案:

$(MainDiv).scrollLeft(DivInsideWidth); 
$(MainDiv).scrollTop(0);