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.
According to MDN, the pageXoffset and pageYoffset is not deprecated.
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 ...
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.
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.
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
};
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With