vue 监听屏幕变化

偏执的太偏执、 2021-10-19 07:54 575阅读 0赞

1、防抖

  1. npm install lodash.debounce -save-dev

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

  1. const windowResize = function() {
  2. console.log("resize");
  3. var html = document.getElementsByTagName('html')[0];
  4. /*取到屏幕的宽度*/
  5. var width = window.innerWidth;
  6. /* 640 100 320 50 */
  7. var fontSize = 100 / 750 * width;
  8. /*设置fontsize*/
  9. html.style.fontSize = fontSize + 'px';
  10. window.onresize = function() {
  11. var html = document.getElementsByTagName('html')[0];
  12. /*取到屏幕的宽度*/
  13. var width = window.innerWidth;
  14. /* 640 100 320 50 */
  15. var fontSize = 100 / 750 * width;
  16. /*设置fontsize*/
  17. html.style.fontSize = fontSize + 'px';
  18. }
  19. };
  20. windowResize();
  21. window.onresize = debounce(windowResize, 200);

发表评论

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

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

相关阅读