js實現浮動框跟隨頁面滾動,最后停留在原來位置


左邊懸浮的二維碼會跟隨頁面向上或者向下滾動,最后停留在原來的位置。

<div style="background:red; width:1000px; height:7000px; margin:auto;">&nbsp;&nbsp;</div>
<div id="aa" style="position: absolute;right: 0px; top: 4600px;"><img src="001.jpg"/></div>
<script>
function scrollx(p){
     var d = document,dd = d.documentElement,db = d.body,w = window,o = d.getElementById(p.id),ie6 = /msie 6/i.test(navigator.userAgent),style,timer;
     if(o){
      o.style.cssText +=";position:"+(p.f&&!ie6?'fixed':'absolute')+";"+(p.l==undefined?'left:0;':'left:'+p.l+'px;')+(p.t!=undefined?'top:'+p.t+'px':'bottom:0');
      if(p.f&&ie6){
    o.style.cssText +=';left:expression(documentElement.scrollLeft + '+(p.l==undefined?dd.clientWidth-o.offsetWidth:p.l)+' + "px");top:expression(documentElement.scrollTop +'+(p.t==undefined?dd.clientHeight-o.offsetHeight:p.t)+'+ "px" );';
    dd.style.cssText +=';background-image: url(about:blank);background-attachment:fixed;';
   }else{
    if(!p.f){
      w.onresize = w.onscroll = function(){
       clearInterval(timer);
       timer = setInterval(function(){
        //雙選擇為了修復chrome 下xhtml解析時dd.scrollTop為 0
        var st = (dd.scrollTop||db.scrollTop),c;
        c = st  - o.offsetTop + (p.t!=undefined?p.t:(w.innerHeight||dd.clientHeight)-o.offsetHeight);
        if(c!=0){
         o.style.top = o.offsetTop + Math.ceil(Math.abs(c)/10)*(c<0?-1:1) + 'px';
        }else{
         clearInterval(timer);  
        }
       },10)
      
     }
     
    }
   }
     }  
   }
    scrollx({
     id:'aa',
    t:300,
    })
 
    /*
     id 你要滾動的內容的id
     l 橫坐標的位置  不寫為緊貼右邊
     t 你要放在頁面的那個位置默認是貼着底邊 0是貼着頂邊
     f 1表示固定 不寫或者0表示滾動
    */
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM