var
您好,欢迎访问景安网络旗下资讯网!
运营 建站 系统 数据库 编程

首页 > JS教程  >JS实现兼容性较好的随屏滚动效果

JS实现兼容性较好的随屏滚动效果

来源:jb51作者:server发布时间:2015-11-10点击:2291

本文实例讲述了JS实现兼容性较好的随屏滚动效果。分享给大家供大家参考,具体如下:

本文实例讲述了JS实现兼容性较好的随屏滚动效果。分享给大家供大家参考,具体如下:

代码中的参数解释如下:

id 你要滚动的内容的id

l 横坐标的位置 不写为紧贴右边

t 你要放在页面的那个位置默认是贴着底边 0是贴着顶边

f 1表示固定 不写或者0表示滚动。


本代码滚动时不会出现抖动,随着屏幕大小而变动


运行效果截图如下:

具体代码如下:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gbk" />

<title>随屏滚动</title>

<style>

html,body{

 padding:0;

 margin:0;

}

</style>

</head>

<body>

<div id="aa" style="width:200px;height:200px;background:#c0c0c0;" >我在随屏滚</div>

<div id="bb" style="width:200px;height:200px;background:#c0c0c0;" >我静止不动</div>

<div style="width:100%;height:500px;background:#000"></div>

<div style="width:100%;height:500px;background:green"></div>

<div style="width:100%;height:500px;background:red"></div>

</body>

</html>

<script>

function scroll(p){

var d = document,dd = d.documentElement,db = d.body,w = window,o = d.getElementById(p.id),ie = /msie/i.test(navigator.userAgent),style;

 if(o){

 o.style.cssText +=";position:"+(p.f&&!ie?'fixed':'absolute')+";"+(p.l==undefined?'right:0;':'left:'+p.l+'px;')+(p.t!=undefined?'top:'+p.t+'px':'bottom:0');

  if(p.f&&ie){

  o.style.cssText +=';left:expression(body.scrollLeft + '+(p.l==undefined?db.clientWidth-o.offsetWidth:p.l)+' + "px");top:expression(body.scrollTop +'+(p.t==undefined?db.clientHeight-o.offsetHeight:p.t)+'+ "px" );'

   db.style.cssText +=";background-image:url(about:blank);background-attachment:fixed;"

  }else{

   if(!p.f){

    w.onresize = w.onscroll = function(){

     var timer,timer1;

     return function(){

      if(timer)

       clearTimeout(timer);

      timer = setTimeout(function(){

       timer1 = setInterval(function(){

        var st = db.scrollTop,c;

        c = st - o.offsetTop + (p.t!=undefined?p.t:(w.innerHeight||db.clientHeight)-o.offsetHeight);

        if(c!=0){

         o.style.top = o.offsetTop + Math.ceil(Math.abs(c)/10)*(c<0?-1:1) + 'px';

        }else{

         clearInterval(timer1);  

        }

       },10)

      },100)//控制滚动的频率越大频率越慢

     }

    }()

   }

  }

 }

}

scroll({

 id:'aa'

})

 scroll({

 id:'bb',

 l:0,

 t:200,

 f:1

 })

</script>


关键词: JS

版权声明:本文系技术人员研究整理的智慧结晶,转载勿用于商业用途,并保留本文链接,侵权必究!

本文链接:https://www.zzidc.com:443/info/jsjc/1398.html

返回顶部