# css实现手风琴效果

# 快速导航

# 示例效果

  • 图片1

  • 图片2

  • 图片3

  • 图片4

  • 图片5

    <ul class="accordion-container">
      <li class="accordion-item" style="background-image: url(bg1.jpg);">
        <p class="accordion-title">背景1</p>
      </li>
      ···
    </ul>
1
2
3
4
5
6
  .accordion-container {
      overflow: hidden;
      display: flex;
      justify-content: flex-start;
      align-items: flex-start;
      .accordion-item {
        list-style: none;
        height: 500px;
        transition: width 0.5s;
        background-repeat: no-repeat;
        background-size: cover;
        background-position: center center;
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;
        &:not(:hover) {
          width: 20%;
        }
        &:hover {
          width: 800px;
          &::after {
            opacity: 1;
          }
        }
        &::after{
          content:'';
          width: 100%;
          height: 100%;
          position: absolute;
          left: 0;
          top: 0;
          background-color: rgba(0, 0, 0, .4);
          z-index: 1;
          opacity: 0;
          transition: opacity 0.5s;
        }
        .accordion-title {
          color: #fff;
          font-size: 18px;
          z-index: 2;
        }
      }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
手风琴完整组件代码
<style scoped lang="scss">
  .accordion-container {
    padding: 0;
    overflow: hidden;
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
    .accordion-item {
      list-style: none;
      height: 500px;
      transition: width 0.5s;
      background-repeat: no-repeat;
      background-size: 500px;
      background-position: center center;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      &:not(:hover) {
        width: 20%;
      }
      &:hover {
        width: 500px;
        &::after {
          opacity: 1;
        }
      }
      &::after {
        content: "";
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        background-color: rgba(0, 0, 0, 0.4);
        z-index: 1;
        opacity: 0;
        transition: opacity 0.5s;
      }
      .accordion-title {
        color: #fff;
        font-size: 18px;
        z-index: 2;
      }
    }
  }
</style>
<template>
  <div>
    <ul class="accordion-container">
      <li class="accordion-item"
        v-for="(item,index) in items"
        :key="index"
        :style="{'backgroundImage':`url(${item.src})`,width:100/item.length+'%'}">
        <p class="accordion-title">{{item.title}}</p>
      </li>
    </ul>

  </div>
</template>

<script>
  export default {
    name: "accordion",
    data() {
      return {
        items: [
          {
            title: `图片1`,
            src:"https://cdn.ai-art.com.cn/static/upload/midjourney/mj/static/upload/other/20230625/2587.png?imageView2/0/format/webp/q/75"
          },
          {
            title: `图片2`,
            src: "https://cdn.ai-art.com.cn/static/upload/midjourney/mj/static/upload/other/20230625/2545.png?imageView2/0/format/webp/q/75"
          },
          {
            title: `图片3`,
            src: "https://cdn.ai-art.com.cn/static/upload/midjourney/mj/static/upload/other/20230604/1004.png?imageView2/0/format/webp/q/75"
          },
          {
            title: `图片4`,
            src: "https://cdn.ai-art.com.cn/static/upload/midjourney/mj/static/upload/other/20230604/994.png?imageView2/0/format/webp/q/75"
          },
          {
            title: `图片5`,
            src: "https://cdn.ai-art.com.cn/static/upload/midjourney/mj/static/upload/other/20230527/375.png?imageView2/0/format/webp/q/75"
          }
        ]
      };
    }
  };
</script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
白色

关注公众号

一个走心,有温度的号,同千万同行一起交流学习

加作者微信

扫二维码 备注 【加群】

扫码易购

福利推荐