js监控元素宽度变化

问题:监听元素的宽度变化(不一定是屏幕宽度才能导致元素变化,也肯能是其他元素的变化导致的)


// 指标控制 适用于 一些指标显示的地方 根据元素宽度 一行来显示不同的个数
<template>
	<div id="aaa"></div>
</template>
export default {
  data() {
    return {
      indexControlNum: 25,
      timerWidth: null,
      resizeObserver: null,
      observedElement: null
    }
  },
	mounted() {
    this.doIndexControl(`aaa`)
  },

  beforeDestroy() {
    // 在组件销毁前,取消对目标元素的观察
    this.resizeObserver.unobserve(this.observedElement)
    // 断开 ResizeObserver 实例与回调函数的连接,释放资源
    this.resizeObserver.disconnect()
  },

  methods: {
    doIndexControl(id) {
    // / 创建 ResizeObserver 实例
      this.resizeObserver = new ResizeObserver(entries => {
      // 当观察到元素大小变化时触发的回调函数
        for (let entry of entries) {
        // 获取元素的宽度
          const width = entry.contentRect.width
          // 在这里可以根据需要进行处理,例如更新数据或触发其他操作
          if (this.timerWidth) clearTimeout(this.timerWidth)
          this.timerWidth = setTimeout(() => {
          // 150宽度来算
            if (width < 450) {
            // 2个 300
              this.indexControlNum = 50
            } else if (width < 600) {
            // 3个 450
              this.indexControlNum = 33.3
            } else if (width < 750) {
            // 4个 600
              this.indexControlNum = 25
            } else if (width < 900) {
            // 5个 750
              this.indexControlNum = 20
            } else if (width < 1050) {
            // 6个
              this.indexControlNum = 16.6
            } else if (width < 1200) {
            // 7个
              this.indexControlNum = 14.2
            } else if (width < 1350) {
            // 8个
              this.indexControlNum = 12.5
            } else if (width < 1500) {
            // 9个
              this.indexControlNum = 11.1
            } else if (width < 1650) {
            // 10个
              this.indexControlNum = 10
            } else if (width < 1800) {
            // 11个
              this.indexControlNum = 9.09
            } else if (width < 1950) {
            // 12个
              this.indexControlNum = 8.33
            } else if (width < 2100) {
            // 13个
              this.indexControlNum = 7.69
            } else if (width < 2250) {
            // 14个
              this.indexControlNum = 7.14
            } else if (width < 2400) {
            // 15个
              this.indexControlNum = 6.66
            } else if (width < 2550) {
            // 16个
              this.indexControlNum = 6.25
            }
            clearTimeout(this.timerWidth)
            this.timerWidth = null
          }, 300)
        }
      })
      let timer = setTimeout(() => {
        this.observedElement = document.getElementById(id)
        this.resizeObserver.observe(this.observedElement)
        clearTimeout(timer)
      }, 500)
    }
  }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要监视元素宽度变化,可以使用JavaScript来实现。主要的方法是使用MutationObserver(变动观察器)来监视元素的属性变化。 MutationObserver是一种内置的JavaScript对象,它可以异步观察DOM树的变动,并且可以接收通知来执行相应的操作。 首先,需要创建一个MutationObserver对象,并将它绑定到需要监视的目标元素上。可以使用document.querySelector()方法来选择要监视的元素。 然后,使用MutationObserver对象的observe()方法来开始观察元素变化。观察的属性类型可以是attributes、childList或者characterData。在这种情况下,我们要监视宽度变化,所以属性类型应该是attributes。 最后,在观察器的回调函数中,可以编写代码来处理元素宽度变化的情况。回调函数的参数是一个数组,包含所有变化的MutationRecord对象。可以通过遍历这个数组来获取具体的变化信息。 下面是一个示例代码: ```javascript // 选择要监视的元素 const targetElement = document.querySelector('.element'); // 创建MutationObserver对象 const observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.attributeName === 'style') { // 处理宽度变化的情况 const width = targetElement.offsetWidth; console.log('宽度变化为:' + width); } }); }); // 开始观察元素宽度变化 observer.observe(targetElement, { attributes: true }); ``` 通过以上代码,可以实现对元素宽度变化的监视,并在控制台输出变化后的宽度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值