首页 > 其他分享 >使用css实现手风琴的效果

使用css实现手风琴的效果

时间:2024-12-07 09:32:47浏览次数:2  
标签:效果 item accordion height header background active 手风琴 css

<!DOCTYPE html>
<html>
<head>
<title>Accordion Example</title>
<style>
.accordion {
  border: 1px solid #ddd;
  width: 100%;
}

.accordion-item {
  border-bottom: 1px solid #ddd;
}

.accordion-header {
  padding: 10px 15px;
  cursor: pointer;
  background-color: #f5f5f5;
  user-select: none; /* Prevent text selection */
}

.accordion-header:hover {
  background-color: #e9e9e9;
}

.accordion-content {
  padding: 0 15px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease-out;
  background-color: white;
}

.accordion-item.active .accordion-content {
  max-height: 200px; /* Adjust as needed */
}

.accordion-item.active .accordion-header {
  background-color: #ddd;
}
</style>
</head>
<body>

<h2>Accordion</h2>

<div class="accordion">
  <div class="accordion-item">
    <div class="accordion-header">Item 1</div>
    <div class="accordion-content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor.</p>
    </div>
  </div>
  <div class="accordion-item">
    <div class="accordion-header">Item 2</div>
    <div class="accordion-content">
      <p>Nulla mauris. Ut id nisl quis enim dignissim fringilla.</p>
    </div>
  </div>
  <div class="accordion-item">
    <div class="accordion-header">Item 3</div>
    <div class="accordion-content">
      <p>Donec ullamcorper nulla non metus auctor fringilla.</p>
    </div>
  </div>
</div>

<script>
const accordionHeaders = document.querySelectorAll('.accordion-header');

accordionHeaders.forEach(header => {
  header.addEventListener('click', () => {
    const accordionItem = header.parentNode;
    accordionItem.classList.toggle('active');

    // Close other accordion items if needed (optional single-open behavior)
    if (accordionItem.classList.contains('active')) {
      accordionHeaders.forEach(otherHeader => {
        if (otherHeader !== header && otherHeader.parentNode.classList.contains('active')) {
          otherHeader.parentNode.classList.remove('active');
        }
      });
    }
  });
});
</script>

</body>
</html>

Explanation and Key Improvements:

  • HTML Structure: Uses a clear and semantic structure with div.accordion, div.accordion-item, div.accordion-header, and div.accordion-content.
  • CSS Styling: Provides basic styling for borders, padding, hover effects, and background colors. The crucial part is max-height: 0 and overflow: hidden for the closed state, and the transition for a smooth animation.
  • JavaScript Functionality:
    • Uses querySelectorAll to get all accordion headers.
    • Attaches a click event listener to each header.
    • Toggles the active class on the clicked item's parent (.accordion-item).
    • Optional Single-Open Behavior: The included JavaScript code now has a section that, when an accordion item is opened, closes any other open accordion items. This creates the more typical single-open accordion behavior. Remove this section if you want multiple items open at the same time.
  • Accessibility Considerations: Added user-select: none to the header to prevent accidental text selection while clicking.
  • Clearer Comments: Added comments to explain the code's purpose.
  • Adjustable Content Height: The max-height in the CSS is set to 200px, but you can adjust this value as needed to fit your content.

This improved example provides a functional and more robust accordion implementation. You can further customize the styling and behavior to match your specific design requirements. Remember to test this code in different browsers for compatibility.

标签:效果,item,accordion,height,header,background,active,手风琴,css
From: https://www.cnblogs.com/ai888/p/18591765

相关文章

  • 使用css实现蒙版的效果
    CSS提供了几种方法实现蒙版效果,以下是几种常见的方式以及它们的优缺点:1.使用::before或::after伪元素:这是最常用的方法之一,它通过在元素的前面或后面创建一个伪元素,并设置其背景颜色和透明度来实现蒙版效果。.masked-element{position:relative;/*必须设置相对定......
  • 在响应式布局中,如何使用纯css使得块元素等比缩放?
    在响应式布局中,使用纯CSS使块元素等比缩放,主要依靠padding的百分比值特性,以及aspect-ratio属性(现代浏览器支持)。以下几种方法可以实现:1.使用padding-top或padding-bottom:这是最常用的方法,利用了padding百分比值是相对于父元素宽度计算的特性。.container{......
  • 登录页毛玻璃效果
    在现代的前端开发中,毛玻璃效果(FrostedGlassEffect)常常用于页面设计中,以增加页面的层次感和美观度,特别是在登录页面等界面中,可以使页面显得更加现代和时尚。毛玻璃效果通过将背景图模糊化并让前景元素保持清晰,模拟出一种“磨砂玻璃”的效果。在本文中,我们将讨论如何在登录页......
  • CSS 一些个人不常用属性总结
    1.滚动捕获在元素中滚动不会在中间停止一定会停在元素前方后或后方-需要在父元素中设置 scroll-snap-type:none|[x|y|block|inline|both][mandatory|proximity]  eg:  scroll-snap-type:xmandatory; 即时在X轴开启强制捕获......
  • 基于CPLD/FPGA的呼吸灯效果实现(附全部verilog源码)
    一、功能介绍此设计可以让你的FPGA板子上那颗LED具有呼吸效果,像智能手机上的呼吸灯一样。以下源码已上板验证通过,大家可直接使用。二、呼吸灯Verilog源码ps1.带★号处可根据需要进行修改.ps2.有需要的话可自行添加rst复位信号. /**************************************......
  • canvas实现场景移动效果
    实现步骤1.中心圆形与随机圆形在Canvas中心绘制一个固定圆形。在画布其他地方随机生成5个圆形,记录每个圆形的初始位置。2.鼠标点击事件获取鼠标点击坐标,并计算与中心圆形之间的距离和角度。3.圆形反向移动5个随机圆形根据中心点为参考,向反方向移动。使用Math.at......
  • 【css】基础(一)
    本专栏内容为:前端专栏记录学习前端,分为若干个子专栏,htmljscssvue等......
  • CSS3——transform属性
    一、前言transform属性是CSS3中新增的一种非常强大的属性,它允许你旋转、缩放、倾斜或平移给定元素。这是通过修改CSS视觉格式化模型的坐标空间实现的。接下来就让我们一起学习transform属性二、transfrom:translate;位移,可以改变元素的位置,translateX让元素沿X轴......
  • 软文营销新攻略!掌握这5个小妙招,投放效果翻倍!媒介盒子分享
    在当今信息爆炸的时代,软文营销作为一种温和而有效的品牌推广手段,越来越受到企业的青睐。它不仅能够潜移默化地传递品牌信息,还能增强消费者的信任感与好感度。然而,如何在众多信息中脱颖而出,让软文真正达到预期的营销效果,成为了每个营销人员面临的挑战。以下是5个小妙招,助你掌握......
  • moviepy字符动态效果
    moviepy==1.0.2抄的别人的importmoviepy.editorasmpeimportnumpyasnprotMatrix=lambdaa:np.array([[np.cos(a),np.sin(a)],[-np.sin(a),np.cos(a)]])defvortex(screenpos,i,nletters):d=lambdat:1.0/(0.3+t**8)#dampinga=i*np.......