Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find element's position in browser scroll

i need some help about finding elements position. im working on an e-book reader, also its all Html with css. All html sectioned page by page, and i have to find an element like this

<span name="Note" style="background-color:rgb(255,255,204)">Example</span>

Everyone suggests code like this;

function position(elem) {     var left = 0,         top = 0;      do {         left += elem.offsetLeft;         top += elem.offsetTop;     } while ( elem = elem.offsetParent );      return [ left, top ]; }position(document.getElementsByName('Note')[0]); 

but it does not work for me; I need element's real position in scroll with JavaScript.

like image 258
relower Avatar asked Nov 09 '11 19:11

relower


People also ask

What is scroll offset?

forward, then scroll offset is the amount the top of the sliver has been scrolled past the top of the viewport. This value is typically used to compute whether this sliver should still protrude into the viewport via SliverGeometry.

What method allows you to get the vertical position of the scroll bar for an element?

The scrollTop() method sets or returns the vertical scrollbar position for the selected elements.

How do you know if an element is visible in scroll?

Summary. Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.

What is scrollTop?

An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0 .


2 Answers

var note = document.getElementsByName('Note')[0]; var screenPosition = note.getBoundingClientRect(); 

The ClientRect returned by getBoundingClientRect() has values for .top, .left, .right, .bottom, .width, and .height.

These are pixel positions on the visible window; as you scroll the page the .top and .bottom values will change, and may even become negative as the item scrolls off the top of the view.

Note that—unlike the solution accumulating offsetLeft/offsetTop—this solution properly accounts for borders and padding on the body and html elements in all browsers (Firefox).

See this test case: http://jsfiddle.net/QxYDe/4/ (scroll the page and watch the values change).

Also supported by Internet Explorer.

like image 149
Phrogz Avatar answered Sep 19 '22 06:09

Phrogz


function position(elem) {      var left = 0,          top = 0;       do {          left += elem.offsetLeft-elem.scrollLeft;          top += elem.offsetTop-elem.scrollTop;      } while ( elem = elem.offsetParent );       return [ left, top ];  }  var elem = document.getElementById('id'); position(elem);  Subtract the scroll positions. 
like image 32
Niet the Dark Absol Avatar answered Sep 19 '22 06:09

Niet the Dark Absol