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>
相关推荐
普郎特3 分钟前
"不再迷惑!用'血缘关系'彻底搞懂JavaScript原型链机制"
前端·javascript
可观测性用观测云12 分钟前
前端错误可观测最佳实践
前端
恋猫de小郭13 分钟前
Android 将强制应用使用主题图标,你怎么看?
android·前端·flutter
小帆聊前端23 分钟前
Flex 布局实战指南:从踩坑到精通,解决 90% 布局难题
css
一枚前端小能手34 分钟前
「周更第3期」实用JS库推荐:Lodash
前端·javascript
艾小码34 分钟前
Vue组件到底怎么定义?全局注册和局部注册,我踩过的坑你别再踩了!
前端·javascript·vue.js
Cyan_RA941 分钟前
计算机网络面试题 — TCP连接如何确保可靠性?
前端·后端·面试
谢尔登41 分钟前
【CSS】层叠上下文和z-index
前端·css
鹏多多42 分钟前
前端复制功能的高效解决方案:copy-to-clipboard详解
前端·javascript
AryaNimbus44 分钟前
你不知道的 Cursor系列(三):再也不用死记硬背 Linux 命令,终端 Cmd+K 来帮你!
前端·ai编程·cursor