jQuery-使页面回到顶部

时间:2022-02-11 13:11:00
 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>back-top</title>
<style type="text/css">
#back-top{
width: 40px;
height: 40px;
position: fixed;
bottom: 40px;
right: 20px;
border: solid gray;
text-align: center;
font-size: 14px;
cursor: pointer;
} .contain {
margin: 0 auto;
width: 1000px;
height: 2000px;
background: red;
font-size: 400px;
}
</style>
<script src="jquery-3.4.1.js"></script>
<script>
$(function(){
$('#back-top').click(function(){
// html,body取并级,处理浏览器兼容
$("html,body").animate({
scrollTop: 0,
screenLeft: 0,
}, 400);
});
});
</script>
</head>
<body>
<div id="back-top">回到顶部</div>
<div class="contain">这里是内容</div>
</body>
</html>

结果

jQuery-使页面回到顶部