简体   繁体   English

显示最新评论并向上滚动以查看较旧的评论

[英]Show Comment from latest and scroll up to see older comments

i'm new to this but i want to know is there a way to show the last li which is inside div 我是新来的,但我想知道是否有办法显示div内的最后一个li

Ex: i have 20 comments inside a <div style="height:500px;overflow-y:scroll"></div> each <li> 's height will vary and it crosses height of <div> . 例如:我在<div style="height:500px;overflow-y:scroll"></div>有20条注释,每个<li>的高度都会变化,并且会超过<div>高度。 so we can scroll inside the div to first and last comment 因此我们可以在div中滚动到第一个和最后一个评论

what i want to do is when the page loads i want the last comment to be shown then if user wants he can scroll up to see the older comments. 我要做的是在页面加载时希望显示最后的评论,然后如果用户希望他可以向上滚动以查看较早的评论。

var commentDiv = $('#myDiv');
var distanceToScroll = commentDiv.innerHeight();

commentDiv.scrollTop(distanceToScroll);

It may so happen that a comment might be longer than the visible area and the top of the comment might not be visible with the above code. 可能发生这样的情况,即注释可能比可见区域长,并且注释的顶部在上面的代码中可能不可见。

Then, 然后,

var lastComment = commentDiv.find('.comments').last();
distanceToScroll -= (lastComment.outerHeight() - 10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM