根据屏幕高度判断列表是否滚动

需要实现的效果就是根据屏幕的高度来判断这个列表是否可以滚动

列表

这个列表我开始只是用css给他加了一个可以滚动的效果
当在全屏时,列表数据有很多已经超出屏幕,这个css添加的滚动效果就出现了,有固定高度的情况下完全可以正常使用
css滚动代码
但是将整个浏览器高度变小时,再去滚动列表,它已经滚动不了了,所以我们需要把他的高度变成一个动态的值

1、在列表上添加一个id
在这里插入图片描述

2、在data里面定义屏幕高度的变量

data() {
    
    
    return {
    
    
      fullHeight: document.documentElement.clientHeight,
      timer:false//节流
    };
  },

3、在watch里监听fullHeight

watch: {
    
    
    fullHeight(val) {
    
    
      if (!this.timer) {
    
    
        this.fullHeight = val;
        this.timer = true;
        let that = this;
        setTimeout(function() {
    
    
          that.timer = false;
        }, 400);
      }
    }
  },

4、获取id,动态添加高度

mounted() {
    
    
    window.onresize = () => {
    
    
      return (() => {
    
    
        var that = this;
        window.fullHeight = document.documentElement.clientHeight;
        that.fullHeight = window.fullHeight;
        var menuHeight = document.getElementById("menu");
        menuHeight.style.height = that.fullHeight - 100 + "px";
      })();
    };
  },

欧克,这样屏幕高度不管怎样变化,列表都可以滚动了哈哈哈哈啊啊哈哈

猜你喜欢

转载自blog.csdn.net/yangsi0706/article/details/115413597