반응형
브라우저 페이지 맨 위로 이동하는 방법
모달 팝업을 작성 중이며 열린 모달 버튼을 누를 때 브라우저가 화면 상단으로 이동해야합니다. jQuery를 사용하여 브라우저를 맨 위로 스크롤하는 방법이 있습니까?
다음 scrollTop
과 같이를 설정할 수 있습니다 .
$('html,body').scrollTop(0);
또는 상단에 스냅하는 대신 작은 애니메이션을 원할 경우 :
$('html, body').animate({ scrollTop: 0 }, 'fast');
애니메이션이 없으면 일반 JS를 사용할 수 있습니다.
scroll(0,0)
애니메이션으로 Nick의 답변을 확인하십시오 .
jQuery UI 대화 상자를 사용하는 경우 창에서 고정 된 위치로 표시되도록 모달의 스타일을 지정하면 팝업되지 않고 스크롤 할 필요가 없습니다. 그렇지 않으면,
var scrollTop = function() {
window.scrollTo(0, 0);
};
트릭을해야합니다.
당신은 자바 스크립트없이 할 수 있고 단순히 앵커 태그를 사용합니까? 그런 다음 무료로 해당 js에 액세스 할 수 있습니다.
모달을 사용하는 동안 js free에 신경 쓰지 않는다고 가정합니다. ;)
나는 이것이 오래된 것을 알고 있지만 Edge에 문제가있는 사람들을 위해 :
일반 JS : window.scrollTop=0;
불행하게도, scroll()
및 scrollTo()
에지 오류를 던져.
jQuery UI 대화 상자를 사용하는 경우 창에서 고정 된 위치로 표시되도록 모달의 스타일을 지정하면 팝업되지 않아 스크롤 할 필요가 없습니다. 다른
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById("myBtn").style.display = "block";
} else {
document.getElementById("myBtn").style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
$('html, body').animate({scrollTop:0}, 'slow');
}
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
}
#myBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
font-size: 18px;
border: none;
outline: none;
background-color: red;
color: white;
cursor: pointer;
padding: 15px;
border-radius: 4px;
}
#myBtn:hover {
background-color: #555;
}
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<div style="background-color:black;color:white;padding:30px">Scroll Down</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px">This example demonstrates how to create a "scroll to top" button that becomes visible when the user starts to scroll the page.</div>
참고 URL : https://stackoverflow.com/questions/4147112/how-to-jump-to-top-of-browser-page
반응형
'Programming' 카테고리의 다른 글
pthreads의 조건 변수 함수에 뮤텍스가 필요한 이유는 무엇입니까? (0) | 2020.05.23 |
---|---|
Linq와 XML 후손 및 요소의 차이점 (0) | 2020.05.23 |
Laravel에 대한 파일 권한을 설정하는 방법은 무엇입니까? (0) | 2020.05.21 |
인터넷에 액세스 할 수없는 Android 에뮬레이터 (0) | 2020.05.21 |
각도 포트를 4200에서 다른 포트로 변경하는 방법 (0) | 2020.05.21 |