每当用户滚动页面时,我希望将元素放置在页面底部。这类似于“固定定位”,但我不能使用“position: fixed” css,因为我的许多客户端浏览器无法支持它。
我注意到jquery可以获取当前视口的顶部位置,但是如何获取滚动视口的底部?
因此,我的问题是如何知道:$(window).scrollBottom()
我注意到jquery可以获取当前视口的顶部位置,但是如何获取滚动视口的底部?
因此,我的问题是如何知道:$(window).scrollBottom()
var scrollBottom = $(window).scrollTop() + $(window).height();
我认为,作为scrollTop的直接相反方式,scrollBottom应该是:
var scrollBottom = $(document).height() - $(window).height() - $(window).scrollTop();
这是一个对我有效的小丑陋测试:
// SCROLLTESTER START //
var showerEl = $('<h1 id="st" style="position: fixed; right: 25px; bottom: 25px;"></h1>')
showerEl.insertAfter('body');
$(window).scroll(function () {
var scrollTop = $(window).scrollTop();
var scrollBottom = $(document).height() - $(window).height() - scrollTop;
showerEl.html('scrollTop: ' + scrollTop + '<br>scrollBottom: ' + scrollBottom);
});
// SCROLLTESTER END //
将来,我已经将scrollBottom制作成一个jquery插件,可像scrollTop一样使用(即您可以设置一个数字,它将从页面底部滚动该数量并返回距页面底部的像素数,或者如果未提供数字,则返回距页面底部的像素数)。
$.fn.scrollBottom = function(scroll){
if(typeof scroll === 'number'){
window.scrollTo(0,$(document).height() - $(window).height() - scroll);
return $(document).height() - $(window).height() - scroll;
} else {
return $(document).height() - $(window).height() - $(window).scrollTop();
}
}
//Basic Usage
$(window).scrollBottom(500);
var scrollBottom =
$(document).height() - $(window).height() - $(window).scrollTop();
我认为最好是获取底部滚动。
这将滚动到页面顶部:
$(window).animate({scrollTop: 0});
这将滚动到底部:
$(window).animate({scrollTop: $(document).height() + $(window).height()});
如果需要的话,将“window”更改为您所需的容器ID或类名(用引号括起来)。
overflow-y: auto
的固定高度 div 中向底部进行动画,以便它实际上滚动到文本的底部(该文本超出了 div 的高度)? - user1063287scrollHeight
- 参见:https://dev59.com/AWs05IYBdhLWcg3wNPM3。 - user1063287 $(window).scrollTop( $('body').height() );
这里是最佳选项,滚动到底部以查看表格网格,它将滚动到表格网格的最后一行:
$('.add-row-btn').click(function () {
var tempheight = $('#PtsGrid > table').height();
$('#PtsGrid').animate({
scrollTop: tempheight
//scrollTop: $(".scroll-bottom").offset().top
}, 'slow');
});
在我的页面中的一个项目:
document.getElementById(elementId).scroll(0,
document.getElementById(elementId).scrollHeight);
function scrollBottum(elementId){
document.getElementById(elementId).scroll(0, document.getElementById(elementId).scrollHeight);
}
<html><div><button onclick="scrollBottum('myCart')">Click me to scroll</button></div>
<div id="myCart" style="height: 50px; overflow-y: scroll;">
<div>1: A First ...</div>
<div>2: B</div>
<div>3: C</div>
<div>4: D</div>
<div>5: E</div>
<div>6: F</div>
<div>7: LAST !!</div>
</div>
</html>
我尝试了一下,它运行得非常好
scrollrev(){
let x:any= document.getElementById('chat')
x.scrollTop = -9000;
}
我尝试了那段代码,它可以正常工作。
// scroll top
scroll(){
let x:any= document.getElementById('chat')
x.scrollTop = 9000;
}
// scroll buttom
scrollrev(){
let x:any= document.getElementById('chat')
x.scrollTop = -9000;
}