
本文将指导你如何使用 jQuery 和 CSS 创建一个带有图片的动态手风琴菜单。我们将使用图片作为菜单标题,并在点击时展开/折叠相应的内容区域,实现一个美观且交互性强的导航组件。
首先,我们需要定义 HTML 结构。手风琴菜单由多个 accordion-section 组成,每个 section 包含一个标题 accordion-section-title 和一个内容区域 accordion-section-content。标题部分我们将使用图片作为链接内容。
<div class="accordion">
<div class="accordion-section">
<a class="accordion-section-title" href="#accordion-1">
<img src="https://provact.altervista.org/newct/colonna_sx/home_off.png" id="pic">
</a>
<div id="accordion-1" class="accordion-section-content">
<p>This is first accordion section</p>
</div>
</div>
<div class="accordion-section">
<a class="accordion-section-title" href="#accordion-2">
<img src="https://provact.altervista.org/newct/colonna_sx/scheda_off.png" id="pic">
</a>
<div id="accordion-2" class="accordion-section-content">
<p> this is second accordian section</p>
</div>
</div>
<div class="accordion-section">
<a class="accordion-section-title" href="#accordion-3">
<img src="https://provact.altervista.org/newct/colonna_sx/aggiorna_off.png" id="pic">
</a>
<div id="accordion-3" class="accordion-section-content">
<p> this is third accordian section</p>
</div>
</div>
</div>每个 accordion-section-title 的 href 属性指向对应 accordion-section-content 的 id。
接下来,我们添加 CSS 样式来控制手风琴菜单的布局和外观。
立即学习“前端免费学习笔记(深入)”;
.accordion {
overflow: hidden;
border-radius: 4px;
background: transparent;
}
.accordion-section-title {
width: 100%;
padding: 15px;
}
.accordion-section-title {
width: 100%;
padding: 15px;
display: inline-block;
background: transparent;
border-bottom: 1px solid #1a1a1a;
font-size: 1.2em;
color: #fff;
transition: all linear 0.5s;
text-decoration: none;
}
.accordion-section-title.active {
background-color: #4c4c4c;
text-decoration: none;
}
.accordion-section-title:hover {
background-color: grey;
text-decoration: none;
}
.accordion-section:last-child .accordion-section-title {
border-bottom: none;
}
.accordion-section-content {
padding: 15px;
display: none;
color: white;
}
.accordion-section {
background-image: url('https://i.pinimg.com/originals/16/51/a7/1651a7e049cf443edc1cffe560600e0f.jpg');
}这里设置了基本样式,包括背景、边框、内边距、文字颜色等。.accordion-section-content 初始设置为 display: none;,使其默认隐藏。.active 类用于高亮当前展开的标题。
最后,我们使用 jQuery 来实现点击标题展开/折叠内容区域的交互效果。
$(document).ready(function() {
$('.accordion-section-title').click(function(e) {
var currentAttrvalue = $(this).attr('href');
if ($(e.target).is('.active')) {
$(this).removeClass('active');
$('.accordion-section-content:visible').slideUp(300);
} else {
$('.accordion-section-title').removeClass('active').filter(this).addClass('active');
$('.accordion-section-content').slideUp(300).filter(currentAttrvalue).slideDown(300);
}
e.preventDefault(); // 阻止默认链接行为
});
});这段代码首先监听 .accordion-section-title 的点击事件。当点击发生时,获取当前点击标题的 href 属性值,然后判断当前标题是否已经处于激活状态 (.active)。
e.preventDefault() 阻止了链接的默认行为,防止页面跳转。
引入 jQuery 库:
确保在 HTML 文件中引入 jQuery 库。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
通过以上步骤,我们就成功创建了一个带有图片的 CSS 手风琴菜单。
注意事项:
这个教程提供了一个基本的手风琴菜单实现,你可以根据自己的需求进行定制和扩展。
以上就是使用 jQuery 创建带有图片的 CSS 手风琴菜单的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号