记住上次的滚动条的位置,下次打开直接在哪个位置

来源:互联网 发布:餐饮大数据分析报告 编辑:程序博客网 时间:2024/04/28 19:05
  var height=localStorage.getItem("Ltop");

if(height){     //如果有高度就说明以前存储到。获取到给滚动条
    if (document.documentElement.scrollTop!=undefined) {
      document.documentElement.scrollTop=height;
    }
    else{
       document.body.scrollTop=height;
     }

}
 window.addEventListener("scroll",function(){  //监听滚动条

     var  top=document.body.scrollTop || document.documentElement.scrollTop;    //document.body是获取的body滚动高度,document.documentElement是根节点html的     

       localStorage.setItem("Ltop", top);

 })
       
阅读全文
0 2
原创粉丝点击