bob体育官方平台
javascript跟随滚动条滚动的层_javascript本领_脚本之家

其实这个效果在很多网站中都能见到,其主要表现为网页两侧的浮动广告。看起来感觉很难做,但其实原理是很简单的,使用定时器没0.1秒检测层的位置并将其置在指定的位置。写了一个简单的代码:

复制代码 代码如下:window.onresize = baiduResizeDiv; window.onerror = function(){} var divTop,divLeft,divWidth,divHeight,docHeight,docWidth,objTimer,i = 0; var px = document.doctype?"px":0; var scrollwidth = navigator.userAgent.indexOf>0?16:0; var iframeheight = navigator.userAgent.indexOf>0?170-2:209-2; String.prototype.Trim = function(){return this.replace;} function baidu_collapse{ ct = document.getElementById; if(ct.style.display=="none"){ ct.style.display=""; obj.src=obj.src.replace; } else { ct.style.display="none"; obj.src=obj.src.replace; } baiduResizeDiv(); } function baiduMsg() { try{ divTop = parseInt(document.getElementById.style.top,10); divLeft = parseInt(document.getElementById.style.left,10); divHeight = parseInt(document.getElementById.offsetHeight,10); divWidth = parseInt(document.getElementById.offsetWidth,10); var scrollPosTop,scrollPosLeft,docWidth,docHeight; if (typeof window.pageYOffset != 'undefined') { scrollPosTop = window.pageYOffset; scrollPosLeft = window.pageXOffset; docWidth = window.innerWidth; docHeight = window.innerHeight; } else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') { scrollPosTop = document.documentElement.scrollTop; scrollPosLeft = document.documentElement.scrollLeft; docWidth = document.documentElement.clientWidth; docHeight = document.documentElement.clientHeight; } else if (typeof document.body != 'undefined') { scrollPosTop = document.body.scrollTop; scrollPosLeft = document.body.scrollLeft; docWidth = document.body.clientWidth; docHeight = document.body.clientHeight; } document.getElementById.style.top = parseInt + docHeight + 10 + px;// divHeight document.getElementById.style.left = parseInt + docWidth - divWidth - scrollwidth + px; document.getElementById.style.visibility="visible"; objTimer = window.setInterval",10); }catch{} } function baiduResizeDiv() { i+=1; try{ divHeight = parseInt(document.getElementById.offsetHeight,10); divWidth = parseInt(document.getElementById.offsetWidth,10); var scrollPosTop,scrollPosLeft,docWidth,docHeight; if (typeof window.pageYOffset != 'undefined') { scrollPosTop = window.pageYOffset; scrollPosLeft = window.pageXOffset; docWidth = window.innerWidth; docHeight = window.innerHeight; } else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') { scrollPosTop = document.documentElement.scrollTop; scrollPosLeft = document.documentElement.scrollLeft; docWidth = document.documentElement.clientWidth; docHeight = document.documentElement.clientHeight; } else if (typeof document.body != 'undefined') { scrollPosTop = document.body.scrollTop; scrollPosLeft = document.body.scrollLeft; docWidth = document.body.clientWidth; docHeight = document.body.clientHeight; } document.getElementById.style.top = docHeight - divHeight + parseInt + px; document.getElementById.style.left = docWidth - divWidth + parseInt

此广告位招租

  • scrollwidth + px; }catch{} } function baidu_move_div() { var scrollPosTop,scrollPosLeft,docWidth,docHeight; if (typeof window.pageYOffset != 'undefined') { scrollPosTop = window.pageYOffset; scrollPosLeft = window.pageXOffset; docWidth = window.innerWidth; docHeight = window.innerHeight; } else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') { scrollPosTop = document.documentElement.scrollTop; scrollPosLeft = document.documentElement.scrollLeft; docWidth = document.documentElement.clientWidth; docHeight = document.documentElement.clientHeight; } else if (typeof document.body != 'undefined') { scrollPosTop = document.body.scrollTop; scrollPosLeft = document.body.scrollLeft; docWidth = document.body.clientWidth; docHeight = document.body.clientHeight; } try{ if(parseInt(document.getElementById.style.top,10) <= (docHeight - divHeight + parseInt { window.clearInterval;objTimer = window.setInterval; } divTop = parseInt(document.getElementById.style.top,10); document.getElementById.style.top = divTop - 1 + px; }catch{} } function baiduMessbox{ var styles='position:absolute;top:0px;left:0px;z-index:99999;visibility:hidden;'; var copic='图片 1'
  • '

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]注意: if{ posX=window.pageXOffset; posY=window.pageYOffset; } elseif(document.documentElement&&document.documentElement.scrollTop){ posX=document.documentElement.scrollLeft; posY=document.documentElement.scrollTop; } elseif{ posX=document.body.scrollLeft; posY=document.body.scrollTop; } 这段代码是为了兼容标准,在xhtml页面中,document.body.scrollTop始终为0,即该属性无效,因此必须用其他的属性来判断,为兼容新旧标准,应该对属性的可用性进行判断。 引用网上的一段文字: 引用 应用WEB标准会使ScrollTop属性失效!!! -//W3C//DTDXHTML1.0 Transitional//EN" "; 加上这段后,document.body.scrollTop永远等于0 bodyonscroll="alert(document.body.scrollTop);"永远也不会引发。 解决办法: 使用: document.documentElement.scrollTop 示例一: varscrollPos; if(typeofwindow.pageYOffset!='undefined'){ scrollPos=window.pageYOffset; } elseif(typeofdocument.compatMode!='undefined'&& document.compatMode!='BackCompat'){ scrollPos=document.documentElement.scrollTop; } elseif(typeofdocument.body!='undefined'){ scrollPos=document.body.scrollTop; } alert; 示例二: functionWebForm_GetScrollX() { if { returnwindow.pageXOffset; } else { if(document.documentElement&&document.documentElement.scrollLeft) { returndocument.documentElement.scrollLeft; } elseif { returndocument.body.scrollLeft; } } return0; } ------------------------------------- pageYOffset是netscape的 document.body.scrollTop和document.documentElement.scrollTop是ie的,但我不知道他们的真正区别,只知道documentElement.scrollTop是xhtml兼容的

' +'

' +'

' +'

' +'

' +'

' +'

' +'

图片 2

' +'

' +'

' +'

' +'

乡下人blog xxrblog.cn 图片 3' +'

' +'

' +'

' +'

' +'

返回顶部