使DOM IE友好 [英] Making DOM IE Friendly

查看:78
本文介绍了使DOM IE友好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使这个脚本IE友好?唯一不是IE友好的部分是变量 scrolledtonum heightofbody ...

How can I make this script IE friendly? The only parts that aren't IE friendly are the variables scrolledtonum and heightofbody...

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);


推荐答案

滚动的mozilla MDN文档包含交易的示例代码与pageYOffset的兼容性问题: https://developer.mozilla.org/En/DOM/Window .scrollY

The mozilla MDN documentation for scrollY contains sample code for dealing with compatibility issues with pageYOffset: https://developer.mozilla.org/En/DOM/Window.scrollY.

它说如下:

对于跨浏览器兼容性,请使用 window.pageYOffset 而不是 window.scrollY ,除了使用

For cross-browser compatibility, use window.pageYOffset instead of window.scrollY, except use

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

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

这篇关于使DOM IE友好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆