Javascript 使用 jQuery 滚动到包含大量文本的 div 底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3742346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use jQuery to scroll to the bottom of a div with lots of text
提问by BrokeMyLegBiking
I have a div with a scrollbar on the right when there is a lot of text in it. I tried to use this code to scroll to the bottom of a div when the page loads, but I am not having much luck. How can it be achieved?
当里面有很多文本时,我有一个带有滚动条的div。我尝试使用此代码在页面加载时滚动到 div 的底部,但我运气不佳。如何实现?
Style:
风格:
div.messageScrollArea{
width: 100%;
max-height: 300px;
overflow: auto;
}
JavaScript code:
JavaScript 代码:
$(document).ready(function () {
var objDiv = $('.messageScrollArea');
if (objDiv.length > 0){
objDiv[0].scrollTop = objDiv[0].scrollHeight;
}
});
回答by
please refer : .scrollTop()
, You can give a try to the solution here : http://jsfiddle.net/y430ovjt/
请参考:.scrollTop()
,您可以在这里尝试解决方案:http: //jsfiddle.net/y430ovjt/
$(function() {
var wtf = $('#scroll');
var height = wtf[0].scrollHeight;
wtf.scrollTop(height);
});
#scroll {
width: 200px;
height: 300px;
overflow-y: scroll;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="scroll">
<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/> <center><b>Voila!! You have already reached the bottom :)<b></center>
</div>
Edit: for the people who are looking for a nice little animation with scrolling : http://jsfiddle.net/o98zbx8j
编辑:对于正在寻找带有滚动功能的漂亮小动画的人:http: //jsfiddle.net/o98zbx8j
$(function() {
var height = 0;
function scroll(height, ele) {
this.stop().animate({
scrollTop: height
}, 1000, function() {
var dir = height ? "top" : "bottom";
$(ele).html("scroll to " + dir).attr({
id: dir
});
});
};
var wtf = $('#scroll');
$("#bottom, #top").click(function() {
height = height < wtf[0].scrollHeight ? wtf[0].scrollHeight : 0;
scroll.call(wtf, height, this);
});
});
#scroll {
width: 300px;
height: 200px;
overflow-y: scroll;
}
#bottom,
#top {
font-size: 12px;
cursor: pointer;
min-width: 50px;
padding: 5px;
border: 2px solid #0099f9;
border-radius: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="bottom">scroll to bottom</span>
<br />
<br />
<br />
<div id="scroll">
<center><b>There's Plenty of Room at the Top, seriously?</b>
</center>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam at enim dignissim, eleifend eros at, lobortis sem. Mauris vel erat felis. Proin quis convallis neque, quis molestie augue. Vestibulum aliquam elit sit amet venenatis eleifend. Donec dapibus
mauris ac lorem mattis pulvinar. Curabitur cursus elit commodo tellus bibendum, ut euismod nisi luctus. Pellentesque id magna nunc. Nam luctus nisi sapien, ac porta sem ultrices vitae. Suspendisse aliquet eleifend nunc, in mattis tellus dapibus rutrum.
Nullam a sem venenatis, suscipit lorem eu, facilisis leo. Nunc eget eleifend magna. Curabitur dictum dui in massa vestibulum sagittis. Mauris sodales neque at tincidunt feugiat. Curabitur iaculis purus nec tortor elementum pulvinar. Donec non mattis
augue.</p>
<p>Integer sit amet iaculis nulla. Cras vehicula nunc eu leo aliquet, et convallis erat aliquet. Aenean tempor faucibus justo, porta blandit felis semper at. Maecenas auctor nibh sit amet tellus consectetur, et varius velit iaculis. Phasellus convallis
lacinia dapibus. Praesent tempus nunc elit, id volutpat tellus sagittis commodo. Vestibulum ultrices quam vel congue viverra. Integer varius diam quis tempor consequat. Integer pulvinar neque lorem, eu lobortis augue pharetra vel. Praesent ornare
lacus quis nisi fermentum dignissim. Curabitur hendrerit augue eu interdum interdum.</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam a ex non dolor rutrum suscipit vitae sit amet nibh. Donec pulvinar odio non ultrices dignissim. Quisque in risus lobortis, accumsan ante et, pellentesque
erat. Quisque ultricies tortor sed tortor venenatis posuere. Integer convallis nunc ut tellus sagittis, et imperdiet erat volutpat. Sed non maximus augue. Sed mattis ipsum sed sem rutrum, at mollis ligula facilisis. Etiam fringilla hendrerit mi eu
molestie. Etiam semper feugiat nunc, eu pellentesque metus porta pretium. Duis tempor neque ut libero scelerisque euismod. Vivamus molestie, quam a mattis scelerisque, dolor turpis accumsan quam, a cursus sem quam at ex. Suspendisse congue elit quis
sem scelerisque commodo.</p>
<p>Ut eu odio at urna hendrerit ullamcorper. Nulla ut turpis molestie diam aliquet luctus. Curabitur dignissim tellus ut porta sagittis. Vivamus ut erat ut neque consequat interdum. Duis vitae risus eget arcu pulvinar venenatis. Maecenas erat arcu, hendrerit
id tortor ut, viverra elementum nibh. Nam quis metus sit amet lacus rhoncus porttitor ac non ipsum. Nullam lacus dui, tempus sed elementum ut, venenatis eget ipsum. Quisque blandit maximus enim eu porta.</p>
<p>Donec mollis diam eros, eu ultrices magna sollicitudin vitae. Nullam quam sapien, elementum a metus nec, facilisis scelerisque nulla. Praesent lobortis nisi ac leo laoreet, quis molestie ipsum porta. Suspendisse aliquet in velit eu ullamcorper. Maecenas
auctor, mi ut viverra elementum, metus turpis commodo orci, eu commodo erat dolor malesuada arcu. Fusce condimentum augue</p>
<center><b>Voila!! You have reached the bottom, already! :)</b>
</center>
</div>
回答by Ruben Verborgh
No need to calculate the actual height of the contents; you can just scroll down a lot:
无需计算内容的实际高度;你可以向下滚动很多:
$(function () {
$('.messageScrollArea').scrollTop(1E10);
});
回答by Naveed
Scrolling with animation:
滚动动画:
Your DIV:
你的 504
<div class='messageScrollArea' style='height: 100px;overflow: auto;'>
Hello World! Hello World! Hello World! Hello World! Hello World! Hello
Hello World! Hello World! Hello World! Hello World! Hello World! Hello
Hello World! Hello World! Hello World! Hello World! Hello World! Hello
</div>
jQuery part:
jQuery 部分:
jQuery(document).ready(function(){
var $t = $('.messageScrollArea');
$t.animate({"scrollTop": $('.messageScrollArea')[0].scrollHeight}, "slow");
});
回答by mbokil
//note: use of stop function to prevent animation build-ups if called repeatedly
//subtracting container height brings scrollTo position to container bottom
scrollUp = function() {
$("#scroller").stop().animate({ scrollTop: 0 }, "slow");
}
scrollDown = function() {
var scroller = $('#scroller');
var height = scroller[0].scrollHeight - $(scroller).height();
$(scroller).stop().animate({ scrollTop: height }, "slow");
}
回答by Fifi
jQuery simple solution, one line, no external lib required :
jQuery简单解决方案,一行,无需外部库
$("#myDivID").animate({ scrollTop: $('#myDivID')[0].scrollHeight }, 1000);
Change 1000 to another value (this is the duration of the animation).
将 1000 更改为另一个值(这是动画的持续时间)。
回答by Thielicious
Make a jQuery function more flexible.
使 jQuery 函数更加灵活。
$.fn.scrollDown=function(){
let el=$(this)
el.scrollTop(el[0].scrollHeight)
}
$('div').scrollDown()
回答by vikash kumar pathak
$(function() {
var wtf = $('#scroll');
var height = wtf[0].scrollHeight;
wtf.scrollTop(height);
});
#scroll {
width: 200px;
height: 300px;
overflow-y: scroll;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="scroll">
<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/>blah<br/>halb<br/> <center><b>Voila!! You have already reached the bottom :)<b></center>
</div>
回答by Tudor
Here's one sample: http://jsfiddle.net/CUUfb/1/
这是一个示例:http: //jsfiddle.net/CUUfb/1/