jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。通过 jQuery,你可以轻松地操作 DOM 元素、处理事件以及创建复杂的动画效果。
jQuery 主要有以下几种类型:
jQuery 广泛应用于各种 Web 开发场景,包括但不限于:
如果你想在页面刷新后自动回到顶部,可以使用以下方法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Page Title</h1>
<p>Some content...</p>
<script>
$(document).ready(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
html {
scroll-behavior: smooth;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Page Title</h1>
<p>Some content...</p>
<script>
$(document).ready(function() {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
</script>
</body>
</html>
原因:
解决方法:
$(document).ready(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
});
通过以上方法,你可以实现页面刷新后自动回到顶部的效果。
领取专属 10元无门槛券
手把手带您无忧上云