zoukankan      html  css  js  c++  java
  • Vue获取DOM元素样式 && 样式更改

    在 vue 中用 document 获取 dom 节点进行节点样式更改的时候有可能会出现 'style' is not definde的错误,这时候可以在 mounted 里用 $refs 来获取样式,并进行更改:

    <template>
      <div style="display: block;" ref="abc">
        <!-- ... -->
      </div>
    </template>
    <script>
      export default {
        mounted () {
          console.log(this.$refs.abc.style.cssText)
        }
      }
    </script>
    
    结果是 display: block;
    如果我们给一个div设定全屏背景图,就需要获取屏幕高度进行赋值:
    <template>
      <div ref="nana">
        <!-- ... -->
      </div>
    </template>
    
    <script>
    export default {
      mounted () {
       let w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
       let h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    
        this.$refs.nana.style.height = h +'px';
    
      }
    
    }
    
    </script>

     备注:有必要时需要在updated方法中设置

        updated () {
          let height = this.$refs.luckDraw.offsetHeight;
          this.$refs.luckDrawContainer.style.height= height+'px';
        },
  • 相关阅读:
    构建之法阅读笔记02
    学习进度条
    构建之法阅读笔记01
    c++ 与C的区别
    c++ 菜单动态效果
    c++ 方框中绘制菜单代码
    c++ 绘制方框
    c++ 条件编译
    c++ 预处理和多重替换
    c++ 文件共享打开
  • 原文地址:https://www.cnblogs.com/sophie_wang/p/7903905.html
Copyright © 2011-2022 走看看