e 1 year ago
parent
commit
759cd75f9d
2 changed files with 76 additions and 0 deletions
  1. 45 0
      html5/12.本地存储2.html
  2. 31 0
      移动端/rem.js

+ 45 - 0
html5/12.本地存储2.html

@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+
+    <script>
+        document.cookie = "name1 = LiLi";
+        var date = new Date();
+        date.setDate(date.getDate() + 2);
+        document.cookie = 'news=新的;expires=' + date.toUTCString();
+
+        // 设置cookie
+        function setCookie(cName,cValue,cTime) {
+            var date = new Date();
+            date.setDate(date.getDate() + cTime);
+            document.cookie = cName + '=' + cValue + ';expires=' + date.toUTCString();
+        }
+        setCookie("a",1,3);
+        setCookie("小红",18,30);
+        // 删除cookie
+        function delCookie(key) {
+            var date = new Date();
+            date.setDate(date.getDate() - 1);
+            document.cookie = key + '=null;expires=' + date.toUTCString();
+        }
+        delCookie("小红");
+        // 获取cookie
+        function getCookie(name) {
+            var cookie = document.cookie;
+            var arr = cookie.split(";");
+            for(var i=0;i<arr.length;i++) {
+                var temp = arr[i].split("=");
+                if(temp[0].trim() == name) {
+                    return temp[1];
+                }
+            }
+        }
+        console.log(getCookie("news"));
+    </script>
+</body>
+</html>

+ 31 - 0
移动端/rem.js

@@ -0,0 +1,31 @@
+;
+(function(win) {
+    var doc = win.document;
+    var docEl = doc.documentElement; 
+    var tid;
+
+    function refreshRem() {
+        // 获取屏幕宽度
+       
+        var width = docEl.getBoundingClientRect().width;
+        var rem = width / 7.5; // 将屏幕宽度分成6.4份, 1份为1rem   320/6.4 = 50 
+        // 让html的fontSize = 50px   1rem = 50px
+        docEl.style.fontSize = rem + 'px'; 
+        // 320  /6.4 = 50   html->50      1rem   50 
+        // 640  /6.4 = 100  html-> 100           100
+    }
+
+    win.addEventListener('resize', function() {
+        clearTimeout(tid);
+        tid = setTimeout(refreshRem, 10);
+    }, false);
+    win.addEventListener('pageshow', function(e) {
+        if (e.persisted) {
+            clearTimeout(tid);
+            tid = setTimeout(refreshRem, 10);
+        }
+    }, false);
+
+    refreshRem();
+
+})(window);