CSS+jquery 实现滑动下拉框 DIV相对于浏览器固定位置

Html代码   收藏代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >  
  2.   
  3. <html>  
  4.     <head>  
  5.         <title>Anchor Properties</title>  
  6.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  7.         <meta content="noindex, nofollow" name="robots">  
  8.         <script>  
  9. function top(){  
  10.     window.scrollTo(0,0);  
  11. }         
  12.           
  13. function bottom(){  
  14.     //scrollHeight属性是获取对象的滚动高度。  
  15.     window.scrollTo(0,document.body.scrollHeight);  
  16. }  
  17.   
  18. function left(){      
  19.     //alert("left");alert(document.body.scrollLeft)  
  20.     var left = 0-document.body.scrollWidth;//向左移动的像素  
  21.     window.scrollBy(left,0 );  
  22. }  
  23.   
  24. function right(){  
  25.     //alert(document.body.scrollWidth);  
  26.     //整个滚动条的宽度  
  27.     window.scrollBy(document.body.scrollWidth,0);  
  28. }         
  29.   
  30.         function init() {
                
                var oLayer = document.getElementById("BtnLeft") ;
                var oLayer_R = document.getElementById("BtnRight");
                //左边
                var init_pos = oLayer.offsetTop;
                var init_left = oLayer.offsetLeft;
                //右边
                var init_pos_R = oLayer_R.offsetTop;
                window.onscroll = function () {
                    //document.body.scrollTop等于滚动滑块上端离滚动的开始点的距离  
                    var heightTop = document.documentElement.scrollTop || document.body.scrollTop;
                    var left = document.documentElement.scrollLeft || document.body.scrollLeft;
                    
                    oLayer.style.top = heightTop+ init_pos;
                    //scrollLeft设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离  
                    //oLayer.style.posLeft = left + init_left;

                    //document.body.scrollTop等于滚动滑块上端离滚动的开始点的距离  
                    oLayer_R.style.top = heightTop + init_pos_R;
                    //scrollLeft设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离  
                    //oLayer_R.style.posLeft = document.body.scrollLeft + init_Left_R;
                    //alert(oLayer_R.style.posLeft);
                }
  31. }         
  32.           
  33.           
  34.         </script>  
  35.     </head>  
  36.     <body onload="init()">  
  37.     <a onclick="bottom()">置底</a>  
  38.     <div id="oLayer" style="position:absolute;left:120;top:60;z-index:2;background:green;width:120px;height:120px"> ddddd  
  39. </div>   
  40.   
  41.     <br>  
  42.     <a onclick="right()">置&nbsp;右</a>  
  43.     <div style="width:1500px;height:30px;float:left">width="2000px"</div>  
  44.     <a style="float:right" onclick="left()">置&nbsp;左</a>  
  45.     <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><a onclick="top()">置&nbsp;顶</a>  
  46.     </body>  
  47. </html> 



原文地址:http://hbiao68.iteye.com/blog/1603621

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值