Wednesday, 28 August 2013

(doc && doc.scrollTop || body && body.scrollTop || 0) return 'Window' object

(doc && doc.scrollTop || body && body.scrollTop || 0) return 'Window' object

I just want to get the scrollTop of document and use the code from this
answer. But what I get is a 'Window' object. I have tested with IE 10,
Chrome and Firefox. Here is my code:
var doc = document.documentElement, body = document.body;
var left = (doc && doc.scrollLeft || body && body.scrollLeft || 0);
var top = (doc && doc.scrollTop || body && body.scrollTop || 0);
scroll = {
top: (doc && doc.scrollTop || body && body.scrollTop || 0),
left: (doc && doc.scrollLeft || body && body.scrollLeft || 0)
};
console.log(scroll.top); // return 0
console.log(top); // return object 'Window'
I think it's a simple question but I can't figure out why it will return
an object.

No comments:

Post a Comment