javascript - get top offset of a div on window scroll -


i'm trying top offset of element using code it's not working it's getting same position every time 108 , not changing though margin-top 100px only

http://jsfiddle.net/np16jm3o/1/

$(function() {         $(window).scroll(function() {             var container= $("#container");             console.log(container.offset().top);         });     }); 

html code:

<div id="container" style="float: left; width: 100%; background: #ccc; height: 1200px; margin-top: 100px;"> 

the offset function returns position relative document, not offset of element itself, parent(s). when @ example in fiddle, can see body has margin of 8 px, resulting in 108px saw.

if want offset relative parent, substract offset of parent. use .scrolltop function height scrolled if want position relative page.


Comments

Popular posts from this blog

Python Kivy ListView: How to delete selected ListItemButton? -

asp.net mvc 4 - A specified Include path is not valid. The EntityType '' does not declare a navigation property with the name '' -