xujunjie 1 年間 前
コミット
f5edb5ce1f
2 ファイル変更15 行追加3 行削除
  1. 11 3
      public/rem.js
  2. 4 0
      src/pages/Home/index.js

+ 11 - 3
public/rem.js

@@ -6,11 +6,11 @@
   var rootItem, rootStyle;
 
   function refreshRem() {
-    var width = docEl.getBoundingClientRect().width;
-    console.log('=======width=======================', width);
+    var width = localStorage.width;
+    if (!width) return;
 
     //与淘宝做法不同,直接采用简单的rem换算方法1rem=100px
-    var rem = (2560 * 100) / 2560;
+    var rem = (width * 100) / 2560;
     //兼容UC开始
     rootStyle = 'html{font-size:' + rem + 'px !important}';
     rootItem =
@@ -65,4 +65,12 @@
       false,
     );
   }
+
+  window.refreshRem = refreshRem;
+  window.addEventListener('storage', (e) => {
+    console.log(e);
+    if (e.key == 'width') {
+      refreshRem();
+    }
+  });
 })();

+ 4 - 0
src/pages/Home/index.js

@@ -14,6 +14,10 @@ const HomePage = (props) => {
   const { data } = useRequest(queryConditionSnapshot, {
     defaultParams: [{ project_id: projectId }],
   });
+  useEffect(() => {
+    localStorage.width = document.documentElement.getBoundingClientRect().width;
+    window.refreshRem();
+  }, []);
   return (
     <div className={styles.content}>
       <LeftContent data={data} />