Suggestion for FAQ notes scroll value script

P

Peter Michaux

Hi,

I have been working with the FAQ notes about page scroll values.

http://www.jibbering.com/faq/faq_notes/not_browser_detect.html#bdScroll

The article usage is like this

var scrollInterface = getPageScroll();
var scrollX = scrollInterface.getScrollX();
var scrollY = scrollInterface.getScrollY();

It seems to me that this first line is not necessary or shouldn't be
necessary. I think that the first call to obtain one of the scroll
values could do the setup as I have done in the following example. The
usage of this example is just

var scrollX = Scroll.getX();
var scrollY = Scroll.getY();

This reduces the complexity of the API to this utility a little and for
me the interior of the utility is less complex. I suppose this
lengthens the first call's computations a little bit but you could do
the following if you wanted too

Scroll.setup();
var scrollX = Scroll.getX();
var scrollY = Scroll.getY();

Peter

--------------------------------

Scroll = {

getX: function() {
Scroll.setup();
return Scroll.getX();
},

getY: function() {
Scroll.setup();
return Scroll.getY();
}

};

Scroll.setup = (function(){
var global = this;

return function() {
var readScroll,
readScrollY = 'scrollTop',
readScrollX = 'scrollLeft';

if (typeof global.pageXOffset == 'number') {
readScroll = global;
readScrollY = 'pageYOffset';
readScrollX = 'pageXOffset';

} else if ((typeof document.compatMode == 'string') &&
(document.compatMode.indexOf('CSS') >= 0) &&
(document.documentElement) &&
(typeof document.documentElement.scrollLeft=='number'))
{

readScroll = document.documentElement;

} else if ((document.body) &&
(typeof document.body.scrollLeft == 'number')) {
readScroll = document.body;
} else {
Scroll.getX = Scroll.getY = function() {return NaN;}
return;
}
Scroll.getX = function() {return readScroll[readScrollX];};
Scroll.getY = function() {return readScroll[readScrollY];};
};

})();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top