当前位置: 代码迷 >> 综合 >> vue 监听屏幕变化
  详细解决方案

vue 监听屏幕变化

热度:96   发布时间:2023-09-18 23:39:55.0

1、防抖

npm install lodash.debounce -save-dev

2、在main.js写上如下代码

const windowResize = function() {console.log("resize");var html = document.getElementsByTagName('html')[0];/*取到屏幕的宽度*/var width = window.innerWidth;/* 640 100  320 50 */var fontSize = 100 / 750 * width;/*设置fontsize*/html.style.fontSize = fontSize + 'px';window.onresize = function() {var html = document.getElementsByTagName('html')[0];/*取到屏幕的宽度*/var width = window.innerWidth;/* 640 100  320 50 */var fontSize = 100 / 750 * width;/*设置fontsize*/html.style.fontSize = fontSize + 'px';}
};windowResize();
window.onresize = debounce(windowResize, 200);

 

  相关解决方案