首頁  >  文章  >  web前端  >  js實作隨螢幕滾動的帶有緩衝效果的右下角廣告程式碼_javascript技巧

js實作隨螢幕滾動的帶有緩衝效果的右下角廣告程式碼_javascript技巧

WBOY
WBOY原創
2016-05-16 15:40:55931瀏覽

本文實例講述了js實現隨螢幕滾動的帶緩衝效果的右下角廣告代碼。分享給大家供大家參考。具體如下:

一個隨螢幕自動捲動的右下角廣告代碼,這裡請注意幾個參數:

id 你要捲動的內容的id
r 放在左邊還是右邊 預設是右邊
t 你要放在頁面的那個位置預設是貼著底邊 0是貼著頂邊
f 1表示固定 不寫或0表示滾動(ie6固定無效)
是不是很實用呢,這個版本經過作者二次修正,相容性還不錯。

運作效果截圖如下:

線上示範網址如下:

http://demo.jb51.net/js/2015/js-right-buttom-float-follow-scroll-adv-codes/

具體程式碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>随屏幕滚动的带缓冲效果的右下角广告</title>
<style>
 html,body{
  height:2000px;
 }
</style>
</head>
<body>
 <div id="aa" style="width:200px;height:200px;background:#c0c0c0;" >拖动滚动条试试哦~</div>
</body>
</html>
<script>
 function scroll(p){
  var d = document,w = window,o = d.getElementById(p.id),ie6 = /msie 6/i.test(navigator.userAgent);
  if(o){
   o.style.cssText +=";position:"+(p.f&&!ie6&#63;'fixed':'absolute')+";"+(p.r&#63;'left':"right")+":0;"+(p.t!=undefined&#63;'top:'+p.t+'px':'bottom:0');
   if(!p.f||ie6){
    -function(){
    var t = 500,st = d.documentElement.scrollTop||d.body.scrollTop,c;
   c = st - o.offsetTop + (p.t!=undefined&#63;p.t:(w.innerHeight||d.documentElement.clientHeight)-o.offsetHeight);//如果你是html 4.01请改成d.body,这里不处理以减少代码
    c!=0&&(o.style.top = o.offsetTop + Math.ceil(Math.abs(c)/10)*(c<0&#63;-1:1) + 'px',t=10);
    setTimeout(arguments.callee,t)
   }() 
   }
  }
 }
 scroll({
  id:'aa'
 })
</script>

希望本文所述對大家的javascript程式設計有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn