简体   繁体   English

IE8替代window.scrollY?

[英]IE8 alternative to window.scrollY?

I'm trying to determine how many pixels down I've scrolled using window.scrollY . 我正在尝试使用window.scrollY确定我滚动了多少像素。 But this isn't supported in IE8. 但IE8不支持此功能。 What is the safe, cross-browser alternative? 什么是安全的跨浏览器替代方案?

The cross-browser compatible version for window.scrollY is document.documentElement.scrollTop . window.scrollY的跨浏览器兼容版本是document.documentElement.scrollTop Please see the 'Notes' section of this piece of Mozilla documentation for a full, detailed workaround in IE8 and before. 请参阅此Mozilla文档的“注释”部分,以获取IE8及之前的完整,详细的解决方法。

As mentioned here , pageYOffset is another alternative to window.scrollY (note though that this is only IE9+ compatible). 正如这里提到的pageYOffset是window.scrollY的另一种替代方法(注意,这只是IE9 +兼容)。

In regard to the link above, check Example 4 for a fully compatible way to get the scroll position (it even accounts for zoom as @adeneo mentioned!) using document.documentElement.scrollTop and document.documentElement.scrollLeft . 关于上面的链接,请使用document.documentElement.scrollTopdocument.documentElement.scrollLeft检查示例4以获得完全兼容的方式来获取滚动位置(它甚至考虑缩放为@adeneo提到!)。

Here, try out the example for yourself! 在这里,亲自试试这个例子吧!

If you don't have to use it a lot, just do: 如果您不必经常使用它,请执行以下操作:

var scroll = window.scrollY //Modern Way (Chrome, Firefox) 
|| document.documentElement.scrollTop (Old IE, 6,7,8)

如果您正在使用jQuery,我使用$(window).scrollTop()来获取IE 8中的Y位置。它似乎有效。

If you have a valid reason for not just using a library to handle this kind of base functionality, don't hesitate 'not to re-invent the wheel'. 如果您有充分的理由不仅使用库来处理这种基本功能,请不要犹豫“不要重新发明轮子”。

Mootools is open source , and you can just 'steal' its implementation, relevant snippets: Mootools是开源的 ,你可以“偷”它的实现,相关的片段:

getScroll: function(){
    var win = this.getWindow(), doc = getCompatElement(this);
    return {x: win.pageXOffset || doc.scrollLeft, y: win.pageYOffset || doc.scrollTop};
}

function getCompatElement(element){
    var doc = element.getDocument();
    return (!doc.compatMode || doc.compatMode == 'CSS1Compat') ? doc.html : doc.body;
}

These 2 are the core of deciding which compatibility mode your current browser it has, and then whether to use window.pageYOffset or document.body.scrollTop based on that or even document.html.scrollTop for really ancient buggy browsers. 这两个是决定你当前浏览器具有哪种兼容模式的核心,然后是否使用window.pageYOffsetdocument.body.scrollTop基于它,甚至是document.html.scrollTop用于真正古老的buggy浏览器。

Based on Niels' answer, I come up with a slightly more compact solution when just the Y coord is needed: 根据Niels的回答,当需要Y坐标时,我想出了一个稍微紧凑的解决方案:

function get_scroll_y() {
    return window.pageYOffset || document.body.scrollTop || document.html.scrollTop;
}

Based on Mozilla, and answers above, I have a created the functions below to more easily get the coords: 基于Mozilla,以及上面的答案,我创建了下面的函数,以便更容易获得coords:

var windowEl = (function () {
    var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
    function scroll() {
        return { left: scrollLeft, top: scrollTop };
    };
    function scrollLeft() {
        return window.scrollX || window.pageXOffset || (isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft);
    };
    function scrollTop() {
        return window.scrollY || window.pageYOffset || (isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop);
    };
    return {
        scroll: scroll,
        scrollLeft: scrollLeft,
        scrollTop: scrollTop
    }
})();

According to the Mozilla documentation , as cited by lifetimes above, the The pageXOffset property is an alias for the scrollX property, so is stictly speaking not necessary. 根据Mozilla的文档 ,如上面引述的寿命,所述的pageXOffset属性是一个别名scrollX属性,所以stictly来说不是必要的。

Anyhoo, usage is: Anyhoo,用法是:

var scroll = windowEl.scroll();
// use scroll.left for the left scroll offset
// use scroll.top for the top scroll offset

var scrollLeft = windowEl.scrollLeft();
// the left scroll offset

var scrollTop = windowEl.scrollTop();
// the top scroll offset

Tested & works on Chrome, Firefox, Opera, Edge (8-Edge), IE (7-11), IE8 on XP 测试并适用于Chrome,Firefox,Opera,Edge(8-Edge),IE(7-11),XP上的IE8

In angular, we use: 在角度,我们使用:

  var windowEl = angular.element($window);
  scrolldist = windowEl.scrollTop();

window.scrollY & window.scrollX works fine in all modern browers like (Chrome, FireFox & Safari) but does not work in IE so to fix the use window.pageXOffset or window.pageYOffset. window.scrollY&window.scrollX适用于所有现代浏览器(Chrome,FireFox和Safari),但在IE中不起作用,以便修复使用window.pageXOffset或window.pageYOffset。

Here is a sample code I wrote to fix ie issue so that the programmatic scrolling works in all browsers including IE 下面是我编写的用于修复问题的示例代码,以便程序化滚动适用于包括IE在内的所有浏览器

if((window.scrollY || window.pageYOffset) >= 1100){
   //alert('Switch to land');
    $('#landTrst').trigger('click'); // your action goes here
}else if ((window.scrollY || window.pageYOffset) <= 900) {   
    //alert('Switch to Refernce Letter');
     $('#resLet').trigger('click'); // your action goes here
}                            

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM