【问题标题】:Making DOM IE Friendly使 DOM IE 友好
【发布时间】:2011-10-07 23:38:42
【问题描述】:

我怎样才能使这个脚本对 IE 友好?唯一对 IE 不友好的部分是变量 scrolledtonumheightofbody...

function getheight() {

            var myWidth = 0,
        myHeight = 0;
         if (typeof (window.innerWidth) == 'number') {
                //Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
            } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
            } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                //IE 4 compatible
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight;
            }
            var scrolledtonum = window.pageYOffset + myHeight + 2;
            var heightofbody = document.body.offsetHeight;
            if (scrolledtonum >= heightofbody) {
                document.body.scrollTop = 0;
            }
        }

        window.onscroll = getheight; 

        function func() {
            window.document.body.scrollTop++;
        }

        window.document.onmouseover = function () {
            clearInterval(interval);
        };

        window.document.onmouseout = function () {
            interval = setInterval(func, 20);
        };

        var interval = setInterval(func, 20);

【问题讨论】:

  • @Mike Samuel 不在怪癖模式。
  • 你真的有IE4用户吗?如果没有,请去掉 IE4 支持代码。
  • 你永远不知道...可能不会。

标签: javascript internet-explorer dom


【解决方案1】:

scrollY 的 mozilla MDN 文档包含处理 pageYOffset 兼容性问题的示例代码:https://developer.mozilla.org/En/DOM/Window.scrollY

上面写着:

为了跨浏览器的兼容性,使用window.pageYOffset而不是window.scrollY,除了使用

(((t = document.documentElement) || (t = document.body.parentNode)) && typeof t.ScrollTop == 'number' ? t : document.body).ScrollTop

window.pageYOffset(和window.scrollY)未定义时。

【讨论】:

  • 我添加了代码:(在这里)我在这里做错了什么吗? pastebin.com/nqaUQTW6
  • 您可能希望将 ScrollTop 更改为 scrollTop - 它应该区分大小写。
  • 我也想知道这一点。这是从 MDN 页面逐字复制的,我想知道是否一些非常旧的浏览器将其命名为 ScrollTop
猜你喜欢
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-03
  • 2015-06-02
  • 2016-04-28
相关资源
最近更新 更多