jQuery 手风琴(Accordion)是一种常见的网页交互效果,它允许用户通过点击标题来展开或折叠内容区域。这种效果通常用于展示一组相关的内容,用户可以逐个查看而不必一次性展开所有内容。
以下是一个简单的 jQuery 手风琴示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Accordion</title>
<style>
.accordion {
width: 300px;
}
.accordion h3 {
cursor: pointer;
padding: 10px;
background-color: #f1f1f1;
}
.accordion .content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #fff;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="accordion">
<h3>Section 1</h3>
<div class="content">
<p>Content for section 1.</p>
</div>
<h3>Section 2</h3>
<div class="content">
<p>Content for section 2.</p>
</div>
<h3>Section 3</h3>
<div class="content">
<p>Content for section 3.</p>
</div>
</div>
<script>
$(document).ready(function() {
$('.accordion h3').click(function() {
$(this).next('.content').slideToggle('slow');
$(this).siblings('h3').next('.content').slideUp('slow');
});
});
</script>
</body>
</html>
通过以上示例代码和常见问题解答,你应该能够实现一个基本的 jQuery 手风琴效果,并解决一些常见的问题。