사용자가 페이지를 스크롤 할 때마다 페이지 하단에 요소를 배치하고 싶습니다. "고정 된 위치"와 같지만 많은 클라이언트의 브라우저에서 지원할 수없는 "위치 : 고정"CSS를 사용할 수 없습니다.
jquery가 현재 뷰포트의 상단 위치를 얻을 수 있다는 것을 알았지 만 어떻게 스크롤 뷰포트의 하단을 얻을 수 있습니까?
그래서 아는 방법을 묻습니다 : $ (window) .scrollBottom ()
답변:
var scrollBottom = $(window).scrollTop() + $(window).height();
Top은 문서 Top에서 보이는 창 까지의 수직 픽셀 수 Top입니다. 스크롤에 정반대로 Top, 스크롤은 Bottom문서에서 수직 픽셀의 # 측정해야 Bottom보이는 창 Bottom(아래 예 알프레드의 답변을). 어떤 이 제공하는 문서의 수직 픽셀의 #입니다 _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 //
앞으로 scrollBottom을 scrollTop과 같은 방식으로 사용할 수있는 jquery 플러그인으로 만들었습니다 (즉, 숫자를 설정하면 페이지 하단에서 해당 양을 스크롤하고 하단에서 픽셀 수를 반환합니다). 페이지의 수 또는 숫자가 제공되지 않은 경우 페이지 하단에서 픽셀 수를 반환)
$.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의 높이를 넘어 확장 됨)?
// 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.body.scrollTop = 100000;