1、meta viewport

  1. const descriptionMeta: any = document.querySelector('meta[name="viewport"]');
  2. descriptionMeta.setAttribute(
  3. 'content',
  4. 'width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no',
  5. );

2、模拟100vh

  1. const vhCheck = () => {
  2. // 模拟 vh
  3. const vh = window.innerHeight * 0.01;
  4. // 设置 css 自定义属性
  5. document.documentElement.style.setProperty('--vh', `${vh}px`);
  6. };

3、rem

  1. function setRootFontSize() {
  2. document.documentElement.style.fontSize = document.documentElement.clientWidth / 3.75 + 'px';
  3. }

4、scroll chaining

  1. // 解决滚动穿透问题
  2. const fixedBody = () => {
  3. const scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
  4. document.body.style.cssText += 'position:fixed;width:100%;top:-' + scrollTop + 'px;';
  5. };
  6. const looseBody = () => {
  7. const body = document.body;
  8. body.style.position = '';
  9. const top = body.style.top;
  10. document.body.scrollTop = document.documentElement.scrollTop = -parseInt(top);
  11. body.style.top = '';
  12. };