侧边栏固定,响应式布局,附带原生实现和Vue版本源码

效果如下

请添加图片描述

适用场景和介绍

该布局适用于后台管理,主页展示等。布局主要由三个部分组成:

  • 顶部的导航栏,它被设计为fixed,时刻停留在页面顶部;
  • 左侧的侧边栏,和右侧主体存在联动,当用户隐藏侧边栏时,右侧主体会填充整个页面。和导航栏一样,同样是fixed,确保用户时刻能看到侧边栏;
  • 右侧的主体;

源码

原生HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>布局</title>
  <style>
      * {
          margin: 0;
          padding: 0;
      }
      .header {
          height: 75px;
          width: 100vw;
          position: fixed;
          background-color: #000;
          color: white;
      }
      .body {
          display: flex;
      }
      .left {
          position: fixed;
          height: calc(100vh - 75px);
          width: 300px;
          background-color: rgba(255, 192, 203, 0.2);
          margin-left: 0;
          margin-top: 75px;
          transition: 1s;
          cursor: pointer;
          z-index: 0;
      }
      .right {
          flex: 1;
          display: flex;
          height: 200vh;
          margin-top: 75px;
      }
      .right .whitespace {
          width: 300px;
          margin-left: 0;
          transition: 1s;
      }
      .right .content {
          flex: 1;
          background: #ffffff;
          background: linear-gradient(225deg,#ffffff 0%, #000000 80%);
          background: -webkit-linear-gradient(225deg,#ffffff 0%, #000000 80%);
          background: -moz-linear-gradient(225deg,#ffffff 0%, #000000 80%);
      }
  </style>
</head>
<body>
  <div class="mainContainer">
    <div class="header">导航栏预留空间</div>
    <div class="body">
      <div class="left" onclick="changePosition()">侧边栏</div>
      <div class="right">
        <div class="whitespace"></div>
        <div class="content">正文内容</div>
      </div>
    </div>
  </div>

  <script type="text/javascript">
    function changePosition() {
      let left = document.querySelector('.left')
      let whitespace = document.querySelector('.whitespace')
      left.style.marginLeft = '-300px'
      whitespace.style.marginLeft = '-300px'

      // 可自行设计一个按钮用来控制侧边栏动作
      setTimeout(() => {
        left.style.marginLeft = 0;
        whitespace.style.marginLeft = 0;
      }, 1500)
    }
  </script>
</body>
</html>

Vue

<template>
  <div class="mainContainer">
    <div class="header">导航栏预留空间</div>
    <div class="body">
      <div class="left" @click="changePosition">侧边栏</div>
      <div class="right">
        <div class="whitespace"></div>
        <div class="content">正文内容</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "ZTest",
  methods: {
    changePosition() {
      let left = document.querySelector('.left')
      let whitespace = document.querySelector('.whitespace')
      left.style.marginLeft = '-300px'
      whitespace.style.marginLeft = '-300px'

      // 可自行设计一个按钮用来控制侧边栏动作
      setTimeout(() => {
        left.style.marginLeft = 0;
        whitespace.style.marginLeft = 0;
      }, 1500)
    }
  }
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.header {
  height: 75px;
  width: 100vw;
  position: fixed;
  background-color: #000;
  color: white;
}
.body {
  display: flex;
}
.left {
  position: fixed;
  height: calc(100vh - 75px);
  width: 300px;
  background-color: rgba(255, 192, 203, 0.2);
  margin-left: 0;
  margin-top: 75px;
  transition: 1s;
  cursor: pointer;
  z-index: 0;
}
.right {
  flex: 1;
  display: flex;
  height: 200vh;
  margin-top: 75px;
}
.right .whitespace {
  width: 300px;
  margin-left: 0;
  transition: 1s;
}
.right .content {
  flex: 1;
  background: #ffffff;
  background: linear-gradient(225deg,#ffffff 0%, #000000 80%);
  background: -webkit-linear-gradient(225deg,#ffffff 0%, #000000 80%);
  background: -moz-linear-gradient(225deg,#ffffff 0%, #000000 80%);
}
</style>

总结

有问题欢迎留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值