localstorage本地存储和动态刷新

1.实现动态刷新思路

  - 通过比较
               "滚动条高度":parseInt(document.documentElement.scrollTop)
                "浏览器实际高度":document.documentElement.clientHeight
                "实际内容高度":document.documentElement.scrollHeight
      的关系,从而实现动态刷新。

2 localstorage本地存储

  • 注意:需要创建一个数组,实现存储数据,每次获取本地存储的资源时,需要将获取的资源先存入数组中,然后再将新的数据插入数组末尾,遍历渲染后,将新的数据重新存入本地存储中,方便下次调用数据。

3 fecth的使用

  • 两个方法:response.text()方法与response.json()方法,分别处理字符串与json的数据。
  • get的方法:
  function getApi() {
            fetch("be.json")
                .then(res => res.json())
                .then(res => {
                    console.log(res);
                })
        }
  • post的方法:
 function getApi() {
            fetch("url", { //请求的地址
                    //body:{name:"jack",age:18}
                    body: "name=jack&age=18", //请求的数据
                    method: "POST", //请求头类型
                    headers: { //请求头信息
                        'Content-Type': 'application/x-www-form-urlencoded' //采用的URL编码
                            //'Content-Type':'application/json'//采用的URL编码 body为json格式

                    }
                }).then(res => res.text())
                .then(res => console.log(res))
                .catch(err => console.log(err))



        }

4 案例代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .item {
            padding: 15px;
            border-bottom: 1px solid red;
        }
        
        h4 {
            color: red;
        }
        
        p:first-child {
            width: 100%;
            height: 30px;
            background: red;
            line-height: 30px;
        }
        
        p:last-child {
            width: 100%;
            height: 30px;
            background: green;
            line-height: 30px;
        }
    </style>
</head>
<!-- https://www.520mg.com/mi/list.php?page= -->

<body>
    <h1>段子</h1>
    <button onclick="dellocal()">删除浏览记录</button>
    <div class="list"></div>
    <script>
        var count = 1;
        var page = 1;
        var temp = []; //存入段子数据
        var list = document.querySelector(".list");
        var url = "https://www.520mg.com/mi/list.php?page=";

        function dellocal() {
            localStorage.removeItem("obj");
            localStorage.removeItem("page");
        }

        //遍历函数
        function get_api_succ(res) {
            for (let i of res.result) {
                var item = document.createElement("div");
                item.classList.add("item");
                item.innerHTML = i.summary;
                var h4 = document.createElement("h4");
                h4.innerHTML = `${count}.` + i.title;
                list.appendChild(item);
                list.insertBefore(h4, item);
                count++;

            }
        }

        //判断数据是否存在
        var temp_local = JSON.parse(localStorage.getItem("obj"));
        // console.log(temp_local)
        if (localStorage.getItem("obj") != null) {
            //将数据存入数组
            for (let i of temp_local.name) {
                temp.push(i);
            }
            //将存储的数据展示
            var temp_content = document.createElement("p");
            temp_content.innerHTML = "历史段子";
            list.appendChild(temp_content);


            for (let i of temp) {
                get_api_succ(i);
            }
            var temp_content1 = document.createElement("p");
            temp_content1.innerHTML = "新段子";
            list.appendChild(temp_content1);
        } else {
            page = localStorage.getItem("page");
            get_api();
        }


        function get_api() {
            fetch(url + page).then(res => res.json()).then(res => {


                //通过创建content对象,以name为键,temp为值传入数据
                temp.push(res);
                var content = new Object(); //创建对象
                content.name = temp;
                //为数组添加数据
                var obj = JSON.stringify(content);

                get_api_succ(res);

                page++;

                localStorage.setItem("page", page);

                //将数据存入localStorage 
                localStorage.setItem("obj", obj);


            })
        }


        window.onscroll = function() {
            // console.log("滚动条高度", parseInt(document.documentElement.scrollTop));
            // console.log("浏览器实际高度", document.documentElement.clientHeight)
            // console.log("实际内容高度", document.documentElement.scrollHeight)
            if (document.documentElement.scrollHeight - 1 == (parseInt(document.documentElement.scrollTop) + document.documentElement.clientHeight)) {
                console.log("更新内容-------------------------");
                page = parseInt(localStorage.getItem("page"));
                get_api();

            }


        }
    </script>

</body>

</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Vue中,可以使用localStorage来进行本地存储localStorage是浏览器提供的一种存储数据的机制,可以将数据存储在浏览器中而不会随着页面的刷新或关闭而丢失。localStorage的使用方法与sessionStorage基本一致,只需将localStorage替换为sessionStorage即可。localStorage具有永久有效的特点,一般浏览器可以存储大约5MB左右的数据。而sessionStorage的有效期是浏览器的会话时间,即标签页关闭后数据就会消失。localStorage的作用域是协议、主机名和端口,而sessionStorage的作用域是窗口、协议、主机名和端口。所以在Vue中,可以直接使用localStorage来进行本地存储,不需要写this.localStorage。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【Vue】本地存储(LocalStorage)和会话存储(SessionStorage)](https://blog.csdn.net/m0_59792745/article/details/124512519)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vue中使用localStorage存储信息](https://blog.csdn.net/weixin_44255044/article/details/114666488)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

圣京都

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值