jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。垂直滚动是指页面或元素在垂直方向上的滚动行为。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>固定高度滚动</title>
<style>
.scroll-container {
height: 200px;
overflow-y: scroll;
border: 1px solid #ccc;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="scroll-container">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...</p>
<!-- 更多内容 -->
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>无限滚动</title>
<style>
.content {
height: 200px;
overflow-y: auto;
border: 1px solid #ccc;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="content" id="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...</p>
<!-- 更多内容 -->
</div>
<script>
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
// 加载更多内容
$('#content').append('<p>New content loaded...</p>');
}
});
</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>平滑滚动</title>
<style>
.scroll-link {
display: block;
text-align: center;
margin-top: 50px;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<a href="#section1" class="scroll-link">Go to Section 1</a>
<div id="section1" style="height: 1000px; background-color: #eee;">Section 1</div>
<script>
$('.scroll-link').click(function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $(this.hash).offset().top
}, 1000);
});
</script>
</body>
</html>
overflow-y
属性设置为 scroll
或 auto
。overflow-y
属性设置为 scroll
或 auto
。通过以上示例和解决方法,您可以更好地理解和实现 jQuery 垂直滚动功能。
领取专属 10元无门槛券
手把手带您无忧上云