如何在加载时使div滚动到底部 [英] How to make div scroll to bottom on load
问题描述
您好我想将div滚动到底部。我试过这样的东西,但它不起作用。
function scrollToBottom(){
var objDiv = document.getElementById(message);
objDiv.scrollTop = objDiv.scrollHeight;
}
这是div的代码。
< div id =messageönload=scrollToBottom()>
< ul>
@foreach(db.Query中的var行(SELECT * FROM Messages WHERE(FromUserID = @ 0 AND ToUserID = @ 1)OR(FromUserID = @ 1 AND ToUserID = @ 0),id,WebSecurity.CurrentUserId ))
{
if(row.FromUserID == id)
{
var a = db.QuerySingle(SELECT * FROM UserProfile WHERE UserId = @ 0,row。 FromUserId);
< li>
< span id =messageToMe> @ a.User< / span>
< br> @ row.Message
< / li>
}
其他
{
< li>
< span id =messageFromMe> @ WebSecurity.CurrentUserName< / span> <峰; br>
@ row.Message
< / li>
}
}
< / ul>
< / div>
和CSS
#message
{
float:right;
溢出:auto;
头寸:相对;
border-bottom:1px纯黑色;
身高:280px;
宽度:450px;
}
参见在页面加载(jQuery)上滚动到Div的底部 [ ^ ]
*注意:此代码使用jQuery
如果不使用jQuery,你可以试试这个:
window .onload = function (){
var objDiv = document .getElementById( MyDivElement跨度>);
objDiv.scrollTop = objDiv.scrollHeight;
}
干杯,
Edo
document.getElementById('message') .scrollTop = 9999999;
Quote:scrollTop属性指定从区域顶部开始的滚动偏移(以像素为单位)。将它设置为一个非常大的值将强制它到底。 blockquote>
更多信息检查: http://stackoverflow.com/questions/4606560/div-overflow-scroll-to-bottom-is-it-possible [ ^ ]
我希望这会对你有所帮助。
Hi I want to scroll div to bottom onload. I tried something like this, but it doesn't work.
function scrollToBottom() { var objDiv = document.getElementById("message"); objDiv.scrollTop = objDiv.scrollHeight; }
This is code of div.
<div id="message" önload="scrollToBottom()" > <ul> @foreach(var row in db.Query("SELECT * FROM Messages WHERE (FromUserID= @0 AND ToUserID= @1) OR (FromUserID= @1 AND ToUserID =@0)",id,WebSecurity.CurrentUserId)) { if(row.FromUserID==id) { var a= db.QuerySingle("SELECT * FROM UserProfile WHERE UserId=@0",row.FromUserId); <li > <span id="messageToMe">@a.User</span> <br>@row.Message </li> } else { <li > <span id="messageFromMe">@WebSecurity.CurrentUserName</span> <br> @row.Message </li> } } </ul> </div>
and CSS#message { float: right; overflow: auto; position: relative; border-bottom: 1px solid black; height: 280px; width: 450px; }解决方案See Scroll to bottom of Div on page load (jQuery)[^]
* NOTE: this code is using jQuery
Without using jQuery, you can try this:
window.onload=function () { var objDiv = document.getElementById("MyDivElement"); objDiv.scrollTop = objDiv.scrollHeight; }
Cheers,
Edo
document.getElementById('message').scrollTop = 9999999;
Quote:The scrollTop property specifies the scrolling offset in pixels from the top of the region. Setting it to a very large value will force it to the bottom.
More info check this : http://stackoverflow.com/questions/4606560/div-overflow-scroll-to-bottom-is-it-possible[^]
I hope this will help to you.
这篇关于如何在加载时使div滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!