本地存储

复制代码
     本地存储:可以将数据存储本地的技术
     字符,不安全
     cookie:
         大小4K,时间,随着http发给服务器
     HTML5新增的API:localstorage,sessionStorage
         大小5M,时间:永久/会话级,不会随着http发给服务器
     localstorage和sessionStorage:
         永久/会话
     window身上的子对象localstorage
     console.log(window.localStorage)
     console.log(localStorage)

     localStorage对象,使用对象的操作:点,中括号,delete关键字
    
     使用对象操作
     localStorage.name = "admin";
     localStorage.name = "root";
     console.log(localStorage.name)
     delete localStorage.name;

     使用方法操作
     localStorage.setItem("name","qwe")
     localStorage.setItem("a","1")
     localStorage.setItem("b","2")
     localStorage.setItem("c","3")
     localStorage.setItem("b","666")

     console.log(localStorage.getItem("name"))
     console.log(localStorage.getItem("a"))
     console.log(localStorage.getItem("b"))
     console.log(localStorage.getItem("c"))
     console.log(localStorage.getItem("d"))

     localStorage.removeItem("name")
     localStorage.removeItem("a")
     localStorage.removeItem("b")
     localStorage.removeItem("c")

     localStorage.clear(); //清除所有
复制代码

利用storage实现同步拖拽

复制代码
<script>
    class Move{
        constructor(){
            this.box=document.querySelector(".box")
            this.init();
            this.getPos();
        }
        getPos(){
            this.pos=localStorage.getItem("pos")?JSON.parse(localStorage.getItem("pos")):{}; //
            this.box.style.left=this.pos.x+"px";
            this.box.style.top = this.pos.y + "px";
        }
        init(){
            var that=this;
            this.box.onmousedown=function (eve) {
                var e1=eve||window.event;
                console.log(1)
                document.onmousemove=function (eve) {
                    var e2=eve||window.event
                    that.box.style.left=e2.clientX-e1.offsetX+"px";
                    that.box.style.top=e2.clientY-e1.offsetY+"px";

                    var obj={
                        x:that.box.offsetLeft,
                        y:that.box.offsetTop
                    };
                    localStorage.setItem("pos",JSON.stringify(obj));
                }
                document.onmouseup = function(){
                    document.onmousemove = document.onmouseup = null;
                }
            };

        }


    }

    new Move();
</script>
复制代码
复制代码
<script>
    this.box=document.querySelector(".box")
    this.pos=localStorage.getItem("pos")?JSON.parse(localStorage.getItem("pos")):{}; //
    this.box.style.left=this.pos.x+"px";
    this.box.style.top = this.pos.y + "px";
    onstorage=function () {
        this.pos=localStorage.getItem("pos")?JSON.parse(localStorage.getItem("pos")):{}; //
        this.box.style.left=this.pos.x+"px";

        this.box.style.top = this.pos.y + "px";
    }

</script>
复制代码

htm

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box{width:100px;height:100px;background: red;position: absolute;left:0;top:0;}

    </style>
</head>
<body>
<div class="box"></div>
</body>
复制代码
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box{width:100px;height:100px;background: green;position: absolute;left:0;top:0;}

    </style>
</head>
<body>
<div class="box"></div>
</body>
复制代码

 

posted @   菜鸟小何  阅读(245)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示