烈14火64网(LieHuo38.Net)网页特效教68程 动态设定一个固定层的位置,并不随滚动条的滚动而变化,可适时设定层的X/Y位置,当你拖动滚动条的时候,它并不会变化,一个参考例子吧,或许对你有用处。 实例效果: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><br /> <html xmlns="http://www.w3.org/1999/xhtml"><br /> <head><br /> <meta http-equiv="Content-Type" content="text/html;charset=gbk" /><br /> <title>Fixed position定位 </title><br /> <style type="text/css"><br /> body {<br /> font-size:12px;<br /> margin:0;<br /> }<br /> #posBlock {<br /> margin:0;<br /> border:4px solid #D2E2F4;<br /> width:480px;<br /> line-height:22px;<br /> background:#F0F5FB;<br /> color:#555555;<br /> font-size:12px;<br /> }<br /> #content {<br /> width:500px;<br /> line-height:22px;<br /> padding:5px;<br /> }<br /> #testBlock {<br /> border:1px solid #D2E2F4;<br /> background:#F0F5FB;<br /> width:300px;<br /> padding:5px;<br /> }<br /> </style><br /> <script type="text/javascript"><br /> var fixedPosition = function(element ,top ,left){<br /> if (!window.XMLHttpRequest && window.ActiveXObject)<br /> {<br /> element.style.position = "absolute";<br /> fixedPosition.setGlobal();<br /> }<br /> else<br /> {<br /> element.style.position = "fixed";<br /> }<br /> element.style.top = top +"px";<br /> element.style.left = left +"px";<br /> };<br /> /*添加css规则*/<br /> fixedPosition.addCSSRule = function(key ,value){<br /> var css = document.styleSheets[document.styleSheets.length-1];<br /> css.cssRules ?<br /> (css.insertRule(key +"{"+ value +"}", css.cssRules.length)) :<br /> (css.addRule(key ,value));<br /> };<br /> /*标志特定公共规则是否存在*/<br /> fixedPosition.inited = false;<br /> /*设置特定公共规则*/<br /> fixedPosition.setGlobal = function(){<br /> if (! fixedPosition.inited)<br /> {<br /> document.body.style.height = "100%";<br /> document.body.style.overflow = "auto";<br /> fixedPosition.addCSSRule("*html" ,"overflow-x:auto;overflow-y:hidden;");<br /> fixedPosition.inited = true;<br /> }<br /> };<br /> </script><br /> <script type="text/javascript"><br /> //<![CDATA[<br /> var $ = function(id){<br /> return document.getElementById(id);<br /> };<br /> var setPosition = function(){<br /> try<br /> {<br /> var x = parseInt($("x_").value);<br /> var y = parseInt($("y_").value);<br /> fixedPosition($("posBlock") ,y ,x);<br /> }<br /> catch (e)<br /> {<br /> alert("请输入数字");<br /> }<br /> };<br /> //]]><br /> </script><br /> </head><br /> <body><br /> <h5 id="posBlock">#posBlock <br />我就在这里,是不会跟着滚动条的变化而变化的。</h5><br /> <div id="testBlock"><br /> <p>重新设定#posBlock位置</p><br /> <p>x: <input type="text" id="x_" /></p><br /> <p>y: <input type="text" id="y_" /></p><br /> <p><br /> <input type="button" value="设定" onclick="setPosition()" /><br /> </p><br /> </div><br /> <script><br /> fixedPosition($("posBlock") ,150 ,5);<br /> fixedPosition($("testBlock") ,250 ,350);<br /> </script><br /> <p id="content"><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> 烈火.net.liehuo学院(www.veryhuo.com)<BR><BR><BR><BR><br /> </p><br /> <script>fixedPosition($("adBlock") ,450 ,250);</script><br /> </body><br /> </html> <br /><center>如不能显示效果,请按Ctrl+F5刷新本页,更多网页代码:<a href='http://www.veryhuo.com/' target='_blank'>http://www.veryhuo.com/</a></center>提示:可修改后代码再运行! 文章导航 CSS实现图片在DIV层纵向水平居中QQ在线客服悬浮窗,点击展开的网页代码