스크롤 상단 이동 버튼에... 자바스크립트 질문 드립니다. 채택완료
버튼에 페이지의 상단과 하단을 %로 표시되게 해놓은 상태입니다.
데스크탑에서는 마우스로 스크롤 하면 제대로 100%가 표시가 되는데...
모바일 기기에서 하단까지 스크롤을 내리면 100% 표시가 되지 않고 80~90% 쯤에서 멈추네요...
</strong></p>
<p>$(function() {</p>
<p> function updateScrollPercent() {</p>
<p> var docHeight = $(document).height();</p>
<p> var windowHeight = $(window).height();</p>
<p> var scrollPosition = $(window).scrollTop();</p>
<p> if (scrollPosition === 0) {</p>
<p> $('#top_btn').hide();</p>
<p> return;</p>
<p> }</p>
<p> </p>
<p> if (docHeight > windowHeight) {</p>
<p> var scrollPercent = (scrollPosition / (docHeight - windowHeight)) * 100;</p>
<p> var scrollPercentRounded = Math.round(scrollPercent);</p>
<p> $('#top_btn').text(scrollPercentRounded + '%').show();</p>
<p> $('#top_btn').css('background', 'rgba(48, 89, 199, ' + (scrollPercentRounded / 100) + ')');</p>
<p> } else {</p>
<p> $('#top_btn').hide();</p>
<p> }</p>
<p> }</p>
<p> </p>
<p> $(window).on('scroll', function() {</p>
<p> window.requestAnimationFrame(updateScrollPercent);</p>
<p> });</p>
<p> </p>
<p> updateScrollPercent();</p>
<p> </p>
<p> $("#top_btn").on("click", function() {</p>
<p> $("html, body").animate({ scrollTop: 0 }, '500');</p>
<p> return false;</p>
<p> });</p>
<p> </p>
<p> $("#top_btn").hover(</p>
<p> function() {</p>
<p> $(this).text('Top');</p>
<p> },</p>
<p> function() {</p>
<p> if ($(this).is(':visible')) {</p>
<p> updateScrollPercent();</p>
<p> }</p>
<p> }</p>
<p> );</p>
<p>});</p>
<p><strong>
답변 2개
</p>
<p> // var windowHeight = $(window).height();
var windowHeight = window.visualViewport.height;</p>
<p>
답변에 대한 댓글 1개
댓글을 작성하려면 로그인이 필요합니다.
</p>
<p>$(function () {
function updateScrollPercent() {
var docHeight = $(document).height();
var windowHeight = window.innerHeight;
var scrollPosition = window.scrollY;</p>
<p> if (scrollPosition === 0) {
$('#top_btn').hide();
return;
}</p>
<p> if (docHeight > windowHeight) {
var scrollPercent = (scrollPosition / (docHeight - windowHeight)) * 100;
var scrollPercentRounded = Math.round(scrollPercent);
$('#top_btn').text(scrollPercentRounded + '%').show();
$('#top_btn').css('background', 'rgba(48, 89, 199, ' + (scrollPercentRounded / 100) + ')');
} else {
$('#top_btn').hide();
}
}</p>
<p> window.onscroll = function () {
window.requestAnimationFrame(updateScrollPercent);
};</p>
<p> updateScrollPercent();</p>
<p> $("#top_btn").on("click", function () {
$("html, body").animate({ scrollTop: 0 }, '500');
return false;
});</p>
<p> $("#top_btn").hover(
function () {
$(this).text('Top');
},
function () {
if ($(this).is(':visible')) {
updateScrollPercent();
}
}
);
});
답변에 대한 댓글 1개
주신 코드 적용 해보니 하단을 두번? 스크롤을 내리니 %값이 조정되네요
답변 감사합니다.
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인