Vue 动态获取可视区高度,Vue监听窗口变化

小灰灰 2023-02-16 04:22 31阅读 0赞

链接:https://www.jianshu.com/p/37c73afb678b

  1. <div class="allposition_qw_content" ref="qwcontent" ></div>
  2. data()
  3. return{
  4. fullHeight: '',//fullHeight: document.documentElement.clientHeight 屏幕高度 默认值
  5. }
  6. },
  7. methods: {
  8. changeFixed(fullHeight){ //动态修改样式
  9. this.$refs.qwcontent.style.height =( 0.91*fullHeight - 200)+'px';
  10. }
  11. },
  12. mounted() {
  13. this.fullHeight= `${document.documentElement.clientHeight}`;//默认值
  14. const that = this
  15. window.onresize = () => {
  16. return (() => {
  17. window.fullHeight = document.documentElement.clientHeight
  18. that.fullHeight = window.fullHeight
  19. })()
  20. }
  21. },
  22. watch: {
  23. // 如果 `fullHeight ` 发生改变,这个函数就会运行
  24. fullHeight :function(val) {
  25. if(!this.timer) {
  26. this.fullHeight = val
  27. this.changeFixed(this.fullHeight)
  28. console.log(val)
  29. this.timer = true
  30. let that = this
  31. setTimeout(function (){//频繁触发 resize 函数,会导致页面很卡
  32. that.timer = false
  33. },100)
  34. }
  35. }
  36. }

发表评论

表情:
评论列表 (有 0 条评论,31人围观)

还没有评论,来说两句吧...

相关阅读