Vue——vue2监听元素style变化

简介: Vue——vue2监听元素style变化

前言

纯粹是为了偷懒,不想再安装swiper来渲染,直接改造下element-ui的走马灯,实现类似的效果,最主要的是后续会迭代到vue3,所以这里临时的实现下即可;

内容

元素绑定ref

给需要监听的元素添加ref,这里是ref="carouse"

<div
                    v-for="(nodes, index) in form.bricks"
                    :key="index"
                    :class="activeIndex === index? 'active' : ''"
                  >
                    <component
                      :is="nodes.els.el"
                      :type="nodes.els.type"
                      :autoplay="!!nodes.auto_play"
                      :src="nodes.els.src"
                      arrow="never"
                      :height="computedHeight(nodes)"
                      @click.native="handleClickNode({...nodes, ...{index: index}})"
                    >
                      <el-carousel-item
                        v-for="(item, i) in nodes.group_content"
                        :key="i"
                        ref="carouse"
                      >
                        <el-image
                          :src="item.src"
                          :class="['carousel'].includes(nodes.group_type)? 'margin-right-xxl' : ''"
                        />
                      </el-carousel-item>
                    </component>
                  </div>

监听元素

leftNav 为左边侧边栏数据,当点击添加的时候会塞入相应的数据,监听该数据变化即可;

主要还是基于MutationObserver来实现监听;

watch: {
    leftNav: {
      handler(val) {
        if (val.group_type !== 'carousel') return ''
        this.$nextTick(() => {
          const elements = this.$refs.carouse
          elements.forEach((element, index) => {
            const observer = new MutationObserver((mutationsList) => {
              for (const mutation of mutationsList) {
                if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
                  if (element.translate > 0) element.translate = 350
                }
              }
            })
            const config = { attributes: true, attributeFilter: ['style'] }
            observer.observe(element.$el, config)
          })
        })
      },
      deep: true
    }
  },

学无止境,谦卑而行.

目录
相关文章
|
2月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
300 2
|
1月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
280 137
|
4月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
403 1
|
4月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
259 0
|
前端开发 JavaScript
Vue.js中class与style的增强绑定
Vue.js中class与style的增强绑定
186 0
|
Web App开发 前端开发 JavaScript
|
前端开发 JavaScript
vue中class与style的绑定
数据绑定一个常见需求是操作元素的 class 列表和它的内联样式。因为它们都是属性 ,我们可以用v-bind 处理它们:只需要计算出表达式最终的字符串。不过,字符串拼接麻烦又易错。因此,在 v-bind 用于 class 和 style 时, Vue.js 专门增强了它。
2021 0
|
JavaScript
Vue.js之Class 与 Style 绑定
数组语法: 我们可以把一个数组传给 v-bind:class ,以应用一个 class 列表: wo data: { activeClass: 'active', err...
842 0
|
5月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
769 0
|
5月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能