Yas
Yas

Reputation: 353

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>

This is example html code. I need to move view (Scroll) of div to right-top on event (For example on button click).

Upvotes: 1

Views: 2219

Answers (3)

Pugazh
Pugazh

Reputation: 9561

Try jQuery .scrollLeft(). Here is the working code.

$(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>

Upvotes: 3

Simon Kraus
Simon Kraus

Reputation: 736

scrollLeft and scrollTop is the correct way but you can add animation and dynamic values for a better flexibility: https://jsfiddle.net/twey5d7u/

Upvotes: 2

Yas
Yas

Reputation: 353

I found a solution:

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

Upvotes: 1

Related Questions