Am'm scris un pop-up modal și am nevoie de browser-ul pentru a sări la partea de sus a ecranului atunci când deschideți modal este apăsat butonul. Există o modalitate de a defila pe browser-ul în partea de sus folosind jQuery?
Puteți seta scrollTop
, astfel:
$('html,body').scrollTop(0);
Sau, dacă doriți un pic de animație în loc de a o fixa la partea de sus:
$('html, body').animate({ scrollTop: 0 }, 'fast');
Daca're folosind jQuery UI de dialog, ai putea doar stil modal să apară cu poziție fixă în fereastră, așa că nu't pop-up de vedere, anulând nevoie pentru a defila. În caz contrar,
var scrollTop = function() {
window.scrollTo(0, 0);
};
ar trebui să facă truc.
// 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>