2.1.1 获取html元素尺寸

2.1.2 获取vue组件元素尺寸

2.1.3 将data值传递给scss类

  1. <template>
  2. <div>
  3. <!-- 2.1.1 -->
  4. <div ref="myDiv">
  5. <p>内容1</p>
  6. <p>内容2</p>
  7. <p>内容3</p>
  8. <p>内容4</p>
  9. </div>
  10. <!-- 2.1.2 -->
  11. <el-card ref="myVue">
  12. <p>内容1</p>
  13. <p>内容2</p>
  14. <p>内容3</p>
  15. <p>内容4</p>
  16. </el-card>
  17. <!-- 2.1.3 -->
  18. <div class="calc-color" :style = "{'--color': myColor}">
  19. <p>内容1</p>
  20. <p>内容2</p>
  21. <p>内容3</p>
  22. <p>内容4</p>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. // 2.1.3
  31. color: '#ff0000'
  32. }
  33. },
  34. watch: {
  35. },
  36. created() {
  37. },
  38. mounted() {
  39. this.calcHtml()
  40. },
  41. methods: {
  42. // 2.1.1 获取普通html元素尺寸
  43. calcHtml(){
  44. this.$nextTick.then(()=>{
  45. let el = this.$refs.myDiv
  46. let elStyle = window.getComputedStyle(el)
  47. console.log(elStyle.height) // 高度 输出带px 105px
  48. console.log(elStyle.width) // 宽度 输出带px 1200px
  49. })
  50. },
  51. // 2.1.2 获取vue组件元素尺寸 方式一
  52. calcVue1(){
  53. this.$nextTick.then(()=>{
  54. let vueEl = this.$refs.myVue
  55. console.log(vueEl.$el.clientHeight) // 高度 直接输出 105
  56. console.log(vueEl.$el.clientWidth) // 宽度 直接输出 1200
  57. })
  58. },
  59. // 2.1.2 获取vue组件元素尺寸 方式二
  60. calcVue2(){
  61. this.$nextTick.then(()=>{
  62. let vueEl = this.$refs.myVue
  63. let elStyle = window.getComputedStyle(vueEl.$el)
  64. console.log(elStyle.height) // 高度 输出带px 105px
  65. console.log(elStyle.width) // 宽度 输出带px 1200px
  66. })
  67. }
  68. }
  69. }
  70. </script>
  71. <style lang="scss" scoped>
  72. // 2.1.3
  73. .calc-color {
  74. color: var(--color);
  75. }
  76. </style>