vue.jsvue.js·

vue 获取元素宽高

Publié à 2024-08-29 16:27:08Vu 36 fois
Article professionnel
Réimpression Veuillez indiquer la source

vue 获取元素宽高

vue 中获取组件宽高分两种情况:
1、获取普通组件宽高,直接使用 $refs 即可
2、获取子组件宽高,需使用 $refs.$el

子组件

<template>
  <div class="div_box">这是一个子组件</div>
</template>

<script>
export default {
  name: 'ChileHeight'
}
</script>

<style lang="scss" scoped>
.div_box {
  padding: 20px;
  border: 20px solid red;
}
</style>

父组件

<template>
  <div class="home">
    <div class="div_card" ref="attrRef">获取元素高度</div>
    <child-height ref="childRef"></child-height>
  </div>
</template>

<script>
import ChildHeight from './note/child-height.vue'
export default {
  name: 'Home',
  components: {
    ChildHeight
  },
  mounted() {
    // 获取元素宽高
    const w = this.$refs.attrRef.offsetWidth
    const h = this.$refs.attrRef.offsetHeight
    console.log('获取元素宽高', w, h)
    // 获取子组件元素宽高
    const w_child = this.$refs.childRef.$el.offsetWidth
    const h_child = this.$refs.childRef.$el.offsetHeight
    console.log('获取子组件元素宽高', w_child, h_child)
  }
}
</script>
<style scoped>
.div_card {
  padding: 20px;
  border: 10px solid yellow;
}
</style>


clientHeightoffsetHeight 区别

clientHeight:包括padding但不包括border、水平滚动条、margin的元素的高度

offsetHeight:包括padding、border、水平滚动条,但不包括margin的元素的高度

原文链接:https://blog.csdn.net/lgg1997/article/details/125618858

Section des commentaires

Pas encore de commentaire, ajoutez le premier.

弦圈热门内容

pyttsx3运行错误

接上文Python实现语音朗读,运行示例代码时import pyttsx3 engine = pyttsx3.init() engine.say('开车不规范,亲人两行泪,I love China') engine.runAndWait()弹出以下错误:经过检查,pywin32等库都已经安装好了。尝试使用win32com库替代pyttsx3,结果仍然报错,报错内容为win32 api。之后又尝试了几种办法,仍然都是跟win32有关的报错。因为之前pip安装总是SSL报错,刚开始以为是SSL报错导致安装出错。但是修复SSL报错问题后(见Python pip安装SSL证书错误),该问题仍然没解决。最后经过了解,可能是pywin32版本过高所导致。一般需要将pywin32版本控制在305以下,可以使用225或者226这样的低版本。于是使用pip下载对应版本pip install pypiwin32 pip install pywin32 == 225然而,下载时发现已经没有225版本可以下载。因此另寻办法。最终,发现是pywin32安装的版本有问题,导致包虽然有了,但是却无法识别,导致出现N ...

大学毕业转行后的一点想法

最近成功把以前写的PDF格式的数学文章,几乎完美复刻到HTML网页上面,文章中的数学公式使用JS插件Mathjax渲染。之后会陆续更新到网站上,希望以后能让更多人无需下载就能看到,这也算给大学四年一个结尾。链接如下👇👇👇Note on arithmetic algebraic geometry, An introduction to different branches of mathematics, Note on perfectoid spaces, 代数几何简介​然后我目前只会把我以前留下的notes、introduction之类的弄成HTML这样网页的形式。至于我写的论文存arXiv上面就好了,谷歌搜也能搜到我的论文。目前来看,距离我论文完成也过去一年半了,并没有太多人对于推广perfect这一概念感兴趣。但值得一提的是,目前来看,我的工作更加受到老外的欣赏和认可,没有一个中国的Phd给我写过信,说看过我的文章。虽然关于perfect这一系列的工作没有全部完成,还可以继续深入耕耘,说不定还能多产出几篇论文吧,算下来我本科完成了4篇论文,有5篇未完成,总页数超过100页。但这一切 ...