zoukankan      html  css  js  c++  java
  • vue+element项目里实时监听某个div宽度的变化,然后执行相应的事件

    背景:vue项目中用到echarts图表,页面上有侧边栏,侧边栏收缩图表不能自适应,想通过监听内容部分的宽度让图表resize,试过window带的resize,只能监听浏览器窗口大小变化,为了监听某元素区域的变化而使echarts的尺寸重置。

    可以看到收起时会图表不会发生变化(上面的折线图是已经调好的)

    解决方式-----

    • 一、自定义指令的方式
      1、局部自定义指令
    directives: {  // 使用局部注册指令的方式
      resize: { // 指令的名称
        bind(el, binding) { // el为绑定的元素,binding为绑定给指令的对象
          let width = '', height = '';
          function isReize() {
            const style = document.defaultView.getComputedStyle(el);
            if (width !== style.width || height !== style.height) {
              binding.value();  // 关键
            }
            width = style.width;
            height = style.height;
          }
          el.__vueSetInterval__ = setInterval(isReize, 300);
        },
        unbind(el) {
          clearInterval(el.__vueSetInterval__);
        }
      }
    }
    //然后在html中
    <div v-resize="resize"></div> // 绑定的resize是一个函数
    //在methods中
    resize() {  // 当宽高变化时就会执行
      //执行某些操作
    }
    

    2、如果是封装的全局自定义指令
    @/directives/index.js

    import Vue from "vue"
    
    
    Vue.directive("chartsWeith",{
        bind(el, binding) { // el为绑定的元素,binding为绑定给指令的对象
            let width = '', height = '';
            function isReize() {
                const style = document.defaultView.getComputedStyle(el);
                if (width !== style.width || height !== style.height) {
                binding.value();  // 关键
                }
                width = style.width;
                height = style.height;
            }
            el.__vueSetInterval__ = setInterval(isReize, 300);
        },
        unbind(el) {
            clearInterval(el.__vueSetInterval__);
        }
    })
    

    main.js中

    ...
    import "@/directives"//引入自定义指令
    

    图表页面中

    //然后在html中
    <div v-chartsWeith="resize"></div> // 绑定的resize是一个函数(v-chartsWeith表示全局指令的方法,resize表示执行的方法)
    //在methods中
    resize() {  // 当宽高变化时就会执行
      //执行某些操作
    }
    
    • 二、element提供的 element-resize-detector(没实际使用,仅看到网上有这个方法)
      第一步:在项目中安装 element-resize-detector
    npm install element-resize-detector
    

    第二步:在项目中使用

    html

    
    <div id="test">
        <div id="eChart">
    </div>
    

    (1)script引入

    
    <script src="node_modules/element-resize-detector/dist/element-resize-detector.min.js"></script>
     
     
    // With default options (will use the object-based approach).
    var erd = elementResizeDetectorMaker();
     
    // With the ultra fast scroll-based approach.
    // This is the recommended strategy.
    var erdUltraFast = elementResizeDetectorMaker({
      strategy: "scroll" //<- For ultra performance.
    });
     
     
     
    //监听元素size变化,触发响应事件
    erd.listenTo(document.getElementById("test"), function(element) {
      var width = element.offsetWidth;
      var height = element.offsetHeight;
      console.log("Size: " + width + "x" + height);
    });
    

    (2)require 引入使用,在cli项目中使用,需要用到的页面 ***.vue 引入

    var elementResizeDetectorMaker = require("element-resize-detector")
    

    在mounted中启用

     var erd = elementResizeDetectorMaker()
        erd.listenTo(document.getElementById("test"), function (element) {
          var width = element.offsetWidth
          var height = element.offsetHeight
          that.$nextTick(function () {
            console.log("Size: " + width + "x" + height)
            //使echarts尺寸重置
            that.$echarts.init(document.getElementById("eChart")).resize()
           
          })
        })
    

    转载地址:https://blog.csdn.net/csl125/article/details/89245267

  • 相关阅读:
    calendar.getTimeInMillis() 和 System.currentTimeMillis() 的区别
    微信小程序中使用 <web-view> 内嵌 H5 时,登录问题的处理方法
    小程序 TabBar 定制
    webpack 代码优化压缩方法
    react-router v4 按需加载的配置方法
    axios发送post请求,如何提交表单数据?
    react中键盘enter事件处理
    常用证件正则表达式
    react中input自动聚焦问题
    React Router v4 页面传值的三种方法
  • 原文地址:https://www.cnblogs.com/axingya/p/14758886.html
Copyright © 2011-2022 走看看