4 回答

TA贡献1772条经验 获得超8个赞
我认为你应该这样做,而不是仅仅滚动html, body.
您还应该滚动 ,.scrollDiv因为这是内容较长的内容。
$(function() {
if (window.performance) {
$('html, body').animate({
scrollTop: document.body.scrollHeight
}, 'slow')
$('.scrollDiv').animate({
scrollTop: $('.scrollDiv')[0].scrollHeight
}, 'slow')
};
});
.scrollDiv {
height: 300px;
overflow: auto;
}
.content {
height: 1500px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div class="contianer">
<div class="scrollDiv">
<div class="content"></div>
<p>This content should be appear on page refresh</p>
</div>
<div class="bottom"></div>
</div>

TA贡献1827条经验 获得超9个赞
如果预计内容会增加,您可以max-height一起使用。height
$(function() {
if (window.performance) {
$('html, body').animate({
scrollTop: $('.bottom').offset().top
}, 'slow')
};
});
.scrollDiv {
height: 300px;
max-height:1500px;
}
.content {
height:300px;
max-height: 1500px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div class="contianer">
<div class="scrollDiv">
<div class="content"></div>
<p>This content should be appear on page refresh</p>
</div>
<div class="bottom"></div>
</div>

TA贡献1895条经验 获得超7个赞
You are trying to scroll to the bottom of DIV.scrollDiv?
If yes, use this.
var objDiv = document.getElementById("scrollToBottom");
objDiv.scrollTop = objDiv.scrollHeight;
var objDiv = document.getElementById("scrollToBottom");
objDiv.scrollTop = objDiv.scrollHeight;
$(function() {
if (window.performance) {
$('html, body').animate({
scrollTop: $('.bottom').offset().top
}, 'slow')
};
});
.scrollDiv {
height: 300px;
overflow: auto;
}
.content {
height: 1500px;
}
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js">
</script>
<div class="contianer">
<div class="scrollDiv" id="scrollToBottom">
<div class="content"></div>
<p>This content should be appear on page refresh</p>
</div>
<div class="bottom"></div>
</div>

TA贡献1871条经验 获得超13个赞
您的 JavaScript 代码是正确的。
它不起作用的原因是因为你将scrollDiv
高度设置为 300,但它content
是 1500;
将您的值更改scrollDiv
为 1500,您会看到它有效。
- 4 回答
- 0 关注
- 159 浏览
添加回答
举报