Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pageYOffset Scrolling and Animation in IE8

I am working on a scrolling page design and I have the following Javascript to hide and show a dialog box:

        if(window.pageYOffset >= 300){

            $('#m1').fadeIn('slow');

    }

    if(document.documentElement.scrollTop >=300){

        $('#m1').fadeIn('slow');

    }

This works great in Chrome,FF, IE9+

However, in IE8,7 it only kind of works. It shows and hides the element properly but the delay between when it evaluates the scroll position and when it hides the element is horrendous. Also, there is no fade, it just happens.

I am wondering if its just a problem with IE8 that I need to deal with or if there is a way for me to achieve a reactive, clean fade with IE8.

like image 528
Shawn Borsky Avatar asked Apr 23 '12 18:04

Shawn Borsky


People also ask

Is pageYOffset deprecated?

According to MDN, the pageXoffset and pageYoffset is not deprecated.

What is pageYOffset in Javascript?

The read-only Window property pageYOffset is an alias for scrollY ; as such, it returns the number of pixels the document is currently scrolled along the vertical axis (that is, up or down) with a value of 0.0, indicating that the top edge of the Document is currently aligned with the top edge of the window's content ...

How do I scroll down to a specific element in Javascript?

The following are the methods available in javascript to scroll to an element. The scrollIntoView method: The scrollIntoView() is used to scroll to the specified element in the browser. Example: Using scrollIntoView() to scroll to an element.

How do you scroll automatically to the top of the page using Javascript?

Method 1: Using window.scrollTo() The scrollTo() method of the window Interface can be used to scroll to a specified location on the page. It accepts 2 parameters the x and y coordinate of the page to scroll to. Passing both the parameters as 0 will scroll the page to the topmost and leftmost point.


2 Answers

pageYOffset and pageXOffset are not supported in IE8 and before, try this function:

// Return the current scrollbar offsets as the x and y properties of an object
function getScrollOffsets() {

    // This works for all browsers except IE versions 8 and before
    if ( window.pageXOffset != null ) 
       return {
           x: window.pageXOffset, 
           y: window.pageYOffset
       };

    // For browsers in Standards mode
    var doc = window.document;
    if ( document.compatMode === "CSS1Compat" ) {
        return {
            x: doc.documentElement.scrollLeft, 
            y: doc.documentElement.scrollTop
        };
    }

    // For browsers in Quirks mode
    return { 
        x: doc.body.scrollLeft, 
        y: doc.body.scrollTop 
    }; 
}
like image 114
undefined Avatar answered Sep 29 '22 17:09

undefined


You can also fix it using this:

document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset;

So you have it

if((document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset) >= 300){
        $('#m1').fadeIn('slow');
}

In this way you can avoid replicate code.

like image 21
pm.calabrese Avatar answered Sep 29 '22 18:09

pm.calabrese