zoukankan      html  css  js  c++  java
  • js的window.onscroll事件兼容各大浏览器

    为窗口添加滚动条事件其实非常的简单,
    window.onscroll=function(){};
    注意在获取滚动条距离的时候
    谷歌不识别document.documentElement.scrollTop,必须要加上document.body.scrollTop;即
    var scrolltop=document.documentElement.scrollTop||document.body.scrollTop;
    这样才能兼容各个浏览器!
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    #box{ height:50px; width:200px; background:#666; position:absolute; left:0px; top:0px;}
    </style>
    </head>
    <body style="height:9000px;">
     
    <div id="box"></div>
     
    <script language="javascript">
    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);
    }
    </script>
     
    </body>
    </html>
  • 相关阅读:
    2018.6.8 现代企业管理复习总结
    写时复制
    字符串类示例
    信号量示例
    对象赋值的语义
    对象复制的语义
    无用单元和悬挂引用
    初始化
    静态数据成员,静态成员函数
    同时找出最大数和最小数
  • 原文地址:https://www.cnblogs.com/xiaomili/p/6841315.html
Copyright © 2011-2022 走看看