css3 实现html样式蛇形布局

文章目录

  • [1. 实现效果](#1. 实现效果)
  • [2. 实现代码](#2. 实现代码)

1. 实现效果

2. 实现代码

html 复制代码
<template>
  <div class="body">
    <div class="title">CSS3实现蛇形布局</div>
    <div class="list">
      <div class="item" v-for="(item, index) in 20" :style="`--i: ${index}`">
        <span>{{ index + 1 }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {};
</script>

<style lang="scss" scoped>
.body {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #de3730;
  .title {
    font-size: 25px;
    font-weight: bold;
    color: #fff;
  }
  .list {
    padding: 20px;
    display: flex;
    width: 100vw;
    gap: 40px;
    flex-wrap: wrap;
    .item {
      width: calc((100% - 120px) / 4);
      background: #00c297;
      color: #fff;
      font-size: 30px;
      border-radius: 5px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      order: var(--i);
      padding: 10px;
      position: relative;
      span {
        text-align: center;
        font-size: 40px;
        font-weight: bold;
      }
      &::after {
        position: absolute;
        content: "=>";
        width: 18px;
        display: inline-block;
        font-size: 12px;
        top: 50%;
        transform: translateY(-50%);
        right: -30px;
        z-index: 1;
        color: #fff;
        font-weight: bold;
        font-size: 18px;
      }
      &:nth-child(8n + 5) {
        order: calc(var(--i) + 3);
      }
      &:nth-child(8n + 6) {
        order: calc(var(--i) + 1);
      }
      &:nth-child(8n + 7) {
        order: calc(var(--i) - 1);
      }
      &:nth-child(8n + 8) {
        order: calc(var(--i) - 3);
      }
      &:nth-child(8n + 4),
      &:nth-child(8n + 8) {
        &::after {
          position: absolute;
          left: 50%;
          top: 110%;
          font-weight: bold;
          transform: translateX(-50%) rotate(90deg);
        }
      }
      &:nth-child(8n + 5),
      &:nth-child(8n + 6),
      &:nth-child(8n + 7) {
        &::after {
          left: -32px;
          top: 50%;
          transform: rotate(180deg) translateY(50%);
        }
      }
      &:last-child {
        &::after {
          display: none;
        }
      }
    }
  }
}
</style>
相关推荐
kite01215 小时前
浏览器工作原理06 [#]渲染流程(下):HTML、CSS和JavaScript是如何变成页面的
javascript·css·html
крон5 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
coding随想8 小时前
JavaScript ES6 解构:优雅提取数据的艺术
前端·javascript·es6
年老体衰按不动键盘8 小时前
快速部署和启动Vue3项目
java·javascript·vue
小小小小宇8 小时前
一个小小的柯里化函数
前端
灵感__idea8 小时前
JavaScript高级程序设计(第5版):无处不在的集合
前端·javascript·程序员
小小小小宇8 小时前
前端双Token机制无感刷新
前端
小小小小宇8 小时前
重提React闭包陷阱
前端
小小小小宇8 小时前
前端XSS和CSRF以及CSP
前端
UFIT8 小时前
NoSQL之redis哨兵
java·前端·算法