vue结合js做一个响应式的侧边栏

<template>
  <div class="onetestwrapper" :style="{'--onetestwidth':width,'--onetestheight':height,'--onetestheightterm':onetestheightterm}">
    <div class="one">
      <div class="onetermchild">
            <i class="el-icon-office-building iconitem"></i>
            酒店
      </div>
    </div>
    <div class="two">
      <div class="onetermchild">

            <i class="el-icon-add-location iconitem"></i>
            风景

      </div>
    </div>
    <div class="three">
     <div class="onetermchild">

           <i class="el-icon-truck iconitem"></i>
           火车

     </div>
    </div>
    <div class="four ">
      <div class="onetermchild">

            <i class="el-icon-food iconitem"></i>
            订餐

      </div>
    </div>
    <div class="five ">
      <div class="onetermchild">

            <i class="el-icon-pear iconitem"></i>
            旅游

      </div>
    </div>
    <div class="six ">
     <div class="onetermchild">

           <i class="el-icon-tickets iconitem"></i>
           门票

     </div>
    </div>
  </div>

</template>

<script>
export default {
  name: "oneTest",
  data(){
    return{
      width:'',
      height:'',
      //每一行的高度
      onetestheightterm:''
    }
  },
  mounted() {
    console.log("我是oneTest中的mounted")
    this.gethomewidth()
    window.addEventListener('resize', this.gethomewidth); // 在窗口大小改变时重新获取窗口宽度
    window.addEventListener('resize', this.gethomeheight);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.gethomewidth); // 组件销毁时移除resize事件监听器
  },
  methods:{
    gethomewidth(){
      // 获取窗口宽度
      var windowwidth = window.innerWidth
      windowwidth = windowwidth-60
      this.width = windowwidth
      console.log("获得浏览器宽",windowwidth)
      this.width = parseInt(this.width/8)
      this.width = this.width+'px'
      console.log('第一部分的宽度为',this.width)
    },
    // 获得窗口height度
    gethomeheight(){

      var windowheight = window.innerHeight
      windowheight = windowheight-160
      this.height = windowheight
      console.log("获得浏览器高度",windowheight)
      this.height = parseInt(this.height)
      // 计算每一行的高度
      this.onetestheightterm = parseInt(this.height/13)+'px'
      console.log("每一行的高度",this.onetestheightterm)
      this.height = this.height+'px'
      console.log('第一部分的高度为',this.height)

    }
  },
}
</script>

<style scoped>
:root{
  --onetestwidth:'';
  --onetestheight:'';
/*  每一行的高度*/
  --onetestheightterm:''
}
.onetestwrapper {
  margin: 25px;
  /* 声明一个容器 */
  display: grid;
  /*  声明列的宽度  */
  grid-template-columns: repeat(1,var(--onetestwidth));
  /*  声明行间距和列间距  */
  grid-gap: 20px;
  /*  声明行的高度  */
  height: var(--onetestheight);
  grid-auto-rows: var(--onetestheightterm);
}
.one {

  background: #19CAAD;
}
.two {
  background: #8CC7B5;
}
.three {
  background: #D1BA74;
}
.four {
  background: #BEE7E9;
}
.five {
  background: #E6CEAC;
}
.six {
  background: #ECAD9E;
}
.iconitem {
  color: rgb(0, 131, 246);
}
.onetermchild{
  position: relative;
  height: 30px;
  width: 60px;
  left: 40px;
  top: 14px;
}
.onetermchildfont{

  left: 30px;
}
</style>

效果
【vue结合js做一个响应式的侧边栏的布局】 https://www.bilibili.com/video/BV1xL411B7KG/?share_source=copy_web&vd_source=58607fc67d165b8caff1ed8bfcefd35b

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是梦磊OL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值