使用Vue里面的ref 控制DOM元素的样式marginleft,marginLeft的只是变化了,但是视图没有变化,求解答

使用Vue里面的ref 控制DOM元素的样式marginleft,marginLeft的只是变化了,但是视图没有变化,求解答

<template>
  <div>
    <div class="roll_header">
      <div class="flex_item" v-for="(ti,index) in pagetitle" :key="ti" @click="changeClassify($event,index)" :class="{'changeclassify':index==temps}">{{ti}}</div>
    </div>
    <div class="roll_bar">
      <div class="bar_item" ref="baritem"></div>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      pagetitle:["登录","注册"],
      temps:0,
    }
  },
  methods:{
    changeClassify(event,index){
      // 改变temps的值,当前点击的控制的元素让他拥有changecolor样式
      this.temps=index;
      // 得到元素的宽度用来计算bar要移动多远
      let dis=event.target.offsetWidth;
      let distance=dis*index;
        // 注意拼接px
      this.$set(this.$refs.baritem.style,'marginleft',distance+'px')
      console.log(this.$refs.baritem.style)
    }
  }
};
</script>
<style>
.roll_header {
  width: 100%;
  height: 46px;
  display: flex;
  justify-content: center;
  line-height: 46px;
  color: rgb(121, 121, 121);
  font-size: 18px;
  cursor: pointer;
}
.flex_item {
  width: 60px;
  text-align: center;
  /* border: 1px solid rebeccapurple; */
}
.roll_bar{
  width: 180px;
  height:3px;
  border:1px solid rgb(223, 223, 223);
  background: white;
  margin: -4px auto;
  padding:0 30px;
  border-radius:2px;
}
.bar_item{
  width:60px;
  height:100%;
  background: coral;
  border-radius:4px;
}
.login{
  color: coral;
}
/* 选择拥有样式 */
.changeclassify{
  color: coral;
}

</style>

这是点击检测看赋值没问题,但是视图就是不变啊,头秃!!!!
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Vue3中的`@scroll`事件监听滚动事件,然后通过计算滚动距离来判断是否需要让DOM元素浮动。 下面是一个简单的实现示例: ```html <template> <div class="container" @scroll="handleScroll"> <div class="content"></div> <div class="float-element" :class="{ 'float': isFloating }"></div> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const isFloating = ref(false); const handleScroll = (event) => { const scrollTop = event.target.scrollTop; if (scrollTop > 100) { // 判断滚动距离是否大于100px isFloating.value = true; // 让DOM元素浮动 } else { isFloating.value = false; // 取消DOM元素浮动 } }; return { isFloating, handleScroll, }; }, }; </script> <style> .container { position: relative; height: 400px; overflow-y: scroll; } .content { height: 1000px; } .float-element { position: absolute; top: 120px; left: 50%; transform: translateX(-50%); width: 100px; height: 100px; background-color: #ccc; transition: all 0.3s ease-in-out; } .float { top: 20px; background-color: red; } </style> ``` 在上面的代码中,我们通过`@scroll`事件监听滚动事件,并在`handleScroll`函数中计算滚动距离,然后根据滚动距离的大小来决定是否让DOM元素浮动。 当滚动距离大于100px时,我们通过`isFloating`变量来控制DOM元素的`float`属性,实现让DOM元素浮动的效果。注意,这里使用Vue3中的`ref`函数来创建响应式变量`isFloating`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值