Reputation: 85
I have some really large div
s with code samples and I'd like to add a button so, when the user clicks on it, the page will scroll (with some ease) to the bottom of that div
.
body {
background-color: #ecf0f1;
}
.story {
background-color: #fff;
border-radius: 5px;
height: 500px;
margin: 20px auto;
padding: 10px;
width: 70%;
}
<div class="story">
<button class="scrollToBottom">Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button class="scrollToBottom">Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button class="scrollToBottom">Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
<div class="story">
<button>Scroll to bottom</button>
</div>
Upvotes: 0
Views: 62
Reputation: 490647
You can simply set scrollTop
to scrollHeight
.
elem.scrollTop = elem.scrollHeight;
If you have an element you want to scroll to, call scrollIntoView()
on a reference to it.
Upvotes: 2