前端开发

 首页 > 前端开发 > javascript > js的window.onscroll事件兼容各大浏览器!

js的window.onscroll事件兼容各大浏览器!

分享到:
【字体:
导读:
          为窗口添加滚动条事件其实非常的简单, window.onscroll=function(){}; 注意在获取滚动条距离的时候 谷歌不识别document.documentElement.scrollTop,必须要加上document.body.scrollTop;即 var scrolltop=docum...

为窗口添加滚动条事件其实非常的简单,

    window.onscroll=function(){};

    注意在获取滚动条距离的时候

    谷歌不识别document.documentElement.scrollTop,必须要加上document.body.scrollTop;即

    var scrolltop=document.documentElement.scrollTop||document.body.scrollTop;

    这样才能兼容各个浏览器!



代码如下:






自学php网

#box{ height:50px; width:200px; background:#666; position:absolute; left:0px; top:0px;}


 
  window.onscroll = function(e){ var e =e || window.event; var scrolltop=document.documentElement.scrollTop||document.body.scrollTop;   var box = document.getElementById('box'); box.style.top = scrolltop+'px'; //console.log(scrolltop);  }  

     


分享到:
移动端js触摸事件大全一、手机上的触摸事...
今天带来:移动端js触摸事件大全 一、手机上的触摸事件 基本事件: touchstart   //手指刚接触屏幕时触发 touchmove    //手指在屏幕上移动时触发 touchend     //手指从屏幕上移开时触发   下面这个比较少用: touchcancel  //触摸过程被系统取消时触发 每个事件都有以下列表,比如touchend的targetTouches当然是 ...
js检查某个元素在数组中的索引值
在现在代浏览器中判断一个元素在不在一个数组中,咱们可以用Array对象的indexOf()方法来取得这个元素在当前数组中的索引值,若索引值不等于-1,数组中就存在这个元素, 例如: var arr = [2,53,23,'test',9,'array']; //判断array在不在数组arr中 arr.indexOf('array') !== -1 ? ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……