【发布时间】:2011-06-07 00:25:00
【问题描述】:
每当用户滚动页面时,我想在页面底部放置一个元素。这就像“固定位置”,但我不能使用“位置:固定”css,因为我的许多客户的浏览器都不支持。
我注意到 jquery 可以获取当前视口的顶部位置,但是如何获取滚动视口的底部?
所以我问怎么知道:$(window).scrollBottom()
【问题讨论】:
标签: javascript jquery
每当用户滚动页面时,我想在页面底部放置一个元素。这就像“固定位置”,但我不能使用“位置:固定”css,因为我的许多客户的浏览器都不支持。
我注意到 jquery 可以获取当前视口的顶部位置,但是如何获取滚动视口的底部?
所以我问怎么知道:$(window).scrollBottom()
【问题讨论】:
标签: javascript jquery
var scrollBottom = $(window).scrollTop() + $(window).height();
【讨论】:
Top 是从文档Top 到可见窗口Top 的垂直像素数。与滚动Top 完全相反,滚动Bottom 应该测量从文档Bottom 到可见窗口Bottom 的垂直像素数(即Alfred 在下面的回答)。 this 给出的是从文档_top_ 到可见窗口Bottom 的垂直像素数。它肯定很有用,但不能将其称为 ScrollBottom 的反面(我知道这是一个明显的答案,但对于像我这样的未来读者)
我会说与 scrollTop 直接相反的 scrollBottom 应该是:
var scrollBottom = $(document).height() - $(window).height() - $(window).scrollTop();
这是一个适合我的小丑测试:
// SCROLLTESTER START //
$('<h1 id="st" style="position: fixed; right: 25px; bottom: 25px;"></h1>').insertAfter('body');
$(window).scroll(function () {
var st = $(window).scrollTop();
var scrollBottom = $(document).height() - $(window).height() - $(window).scrollTop();
$('#st').replaceWith('<h1 id="st" style="position: fixed; right: 25px; bottom: 25px;">scrollTop: ' + st + '<br>scrollBottom: ' + scrollBottom + '</h1>');
});
// SCROLLTESTER END //
【讨论】:
var scrollBottom =
$(document).height() - $(window).height() - $(window).scrollTop();
我认为最好是底部滚动。
【讨论】:
对于未来,我已经将 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);
【讨论】:
这将滚动到最顶部:
$(window).animate({scrollTop: 0});
这将滚动到最底部:
$(window).animate({scrollTop: $(document).height() + $(window).height()});
.. 如有必要,将窗口更改为所需的容器 ID 或类(用引号括起来)。
【讨论】:
overflow-y: auto 动画到固定高度div 的底部,使其真正滚动到文本底部(超出div 的高度)?
scrollHeight - 请参阅:stackoverflow.com/q/7381817
这是表格网格滚动到底部的最佳选项,它将滚动到表格网格的最后一行:
$('.add-row-btn').click(function () {
var tempheight = $('#PtsGrid > table').height();
$('#PtsGrid').animate({
scrollTop: tempheight
//scrollTop: $(".scroll-bottom").offset().top
}, 'slow');
});
【讨论】:
这是一个快速的技巧:只需将滚动值分配给一个非常大的数字。这将确保页面滚动到底部。 使用纯 JavaScript:
document.body.scrollTop = 100000;
【讨论】:
// Back to bottom button
$(window).scroll(function () {
var scrollBottom = $(this).scrollTop() + $(this).height();
var scrollTop = $(this).scrollTop();
var pageHeight = $('html, body').height();//Fixed
if ($(this).scrollTop() > pageHeight - 700) {
$('.back-to-bottom').fadeOut('slow');
} else {
if ($(this).scrollTop() < 100) {
$('.back-to-bottom').fadeOut('slow');
}
else {
$('.back-to-bottom').fadeIn('slow');
}
}
});
$('.back-to-bottom').click(function () {
var pageHeight = $('html, body').height();//Fixed
$('html, body').animate({ scrollTop: pageHeight }, 1500, 'easeInOutExpo');
return false;
});
【讨论】:
尝试:
$(window).scrollTop( $('body').height() );
【讨论】:
var scrolltobottom = document.documentElement.scrollHeight - $(this).outerHeight() - $(this).scrollTop();
【讨论】:
对于我页面中的项目:
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;
}
【讨论】: