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>
相关推荐
遇乐的果园2 分钟前
前端学习笔记-vue列表处理
前端·vue.js·学习
倾颜8 分钟前
AI 聊天刷新后记录全丢?用浏览器本地存储给 AI Chat 加一层"离线记忆"
前端·aigc
梦想的旅途214 分钟前
企业微信API 实现企业微信消息的自动发送?
java·服务器·前端
何时梦醒16 分钟前
🤖 RAG 文档分割器(Splitter)深度学习笔记 —— 从零搭建知识库文档处理流水线
前端·javascript·人工智能
ss27324 分钟前
Vue 完整版本发展史时间线(2013–2026)|版本迭代里程碑+核心特性对照表
前端·javascript·vue.js
用户9385156350725 分钟前
知识库预处理实战:从URL加载到语义分块的全链路解析
javascript·人工智能·全栈
亿元程序员35 分钟前
为什么大家都那么喜欢用Shader?
前端
Codebee37 分钟前
OODER Studio设计工具导入体系深度解析
前端
weedsfly40 分钟前
前端开发中的命令模式——从点餐退单到异步任务调度
前端·javascript·面试