您的位置: 首页 > 源码资料

div在页面中上下左右固定位置跟随的浮动方式实现

源码资料 时间:2013-03-13 作者/发布人:科杰在线 点击:3106

div固定悬浮(左侧、右侧、任意相对位置),兼容多浏览器。

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
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) {
  cssPub = ";position:"+(p.f&&!ie6?'fixed':'absolute')+";"+(p.t!=undefined?'top:'+p.t+'px;':'bottom:0;');
  if (p.r != undefined && p.l == undefined) {
   o.style.cssText += cssPub + ('right:'+p.r+'px;');
  } else {
   o.style.cssText += cssPub + ('margin-left:'+p.l+'px;');
  }
  if(p.f&&ie6){
   cssTop = ';top:expression(documentElement.scrollTop +'+(p.t==undefined?dd.clientHeight-o.offsetHeight:p.t)+'+ "px" );';
   cssRight = ';right:expression(documentElement.scrollright + '+(p.r==undefined?dd.clientWidth-o.offsetWidth:p.r)+' + "px")';
   if (p.r != undefined && p.l == undefined) {
    o.style.cssText += cssRight + cssTop;
   } else {
    o.style.cssText += cssTop;
   }
   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)
    }
   }
  }
 }
}
</script>
</head>
<body>
<div style="width:700px; margin:0 auto; height:1000px; background:#ccc">
 <div id="float_father">相对固定</div>
 <div id="float_father_2">相对固定(动)</div>
 <p>测试内容</p>
 <p>测试内容</p>
</div>
<div id="float_left">左侧固定</div>
<div id="float_right">右侧固定</div>
<script type="text/javascript">
//左右侧固定浮动的div建议放在html的最低部
//右侧固定
scrollx({id:'float_right', r:0, t:200, f:1});
//左侧固定
scrollx({id:'float_left', t:200, f:1});
//相对父级相定固定
scrollx({id:'float_father', l:300, t:200, f:1});
//页面滚动同时滚动固定对像
scrollx({id:'float_father_2', l:500, t:300, f:0});
/*
scrollx参数说明

id:浮动对象的id
r:右边距(窗口右边距,不写为靠左浮动)
l:左边距(距离父级对象的左边距) “r”和“l”只能有其中一个参数
t:上边距(默认贴着底边,0是贴着顶边)
f:1表示固定(不写或者0表示滚动)
*/
</script>
</body>
</html>

--------------------------全文完----------------------------
0% (0)
0% (0)
整站字母快速检索: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 0

综合报道 经济形势 劳动就业 政策法规 热点推荐 创业新闻 创业指导 创业课堂 创业故事 大学生创业 | 装修日记 | 学驾驶经历 | 免费信息发布 | 网站地图

地址:合肥市临泉路香格里拉花园 邮箱:pc354@163.com QQ:55769640 | 皖ICP备06007228号 
版权所有:科杰服务(www.pc354.com) 建议使用IE7.0或以上版本,最少1280分辨率浏览本站,可获得最佳浏览效果

飞到顶部