簡體   English   中英

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

[英]Function to move scroll to the right-top corner of DIV in jQuery

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

這是示例html代碼。 我需要將div的視圖(滾動)移動到事件的右上角(例如,單擊按鈕時)。

scrollLeftscrollTop是正確的方法,但是您可以添加動畫和動態值以獲得更好的靈活性: https : //jsfiddle.net/twey5d7u/

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

我找到了解決方案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM