A
A
Alex Shinkevich2010-11-18 13:45:14
JavaScript
Alex Shinkevich, 2010-11-18 13:45:14

Relative positioning with jQuery?

Help position elements relative to each other, taking into account scrolling in different browsers.

Made an additional block when hovering the mouse. Here is a piece of code responsible for calculating the position of the new block on the page:

...<br/>
<br/>
var mytop = image.offset().top - (container.height() /2) + image.height()/2 + $(document).scrollTop();<br/>
var myleft = image.offset().left - (container.width() /2 ) + image.width()/2 + $(document).scrollLeft();<br/>
<br/>
if($.browser.msie){<br/>
 mytop = image.offset().top - (container.height() /2) + image.height()/2;<br/>
 myleft = image.offset().left - (container.width() /2 ) + image.width()/2;<br/>
}<br/>
<br/>
container.offset({top: mytop, left: myleft});<br/>
container.show();<br/>
<br/>
...<br/>


Those. we look at the coordinates of the current element, and if the browser is IE, then we add the height / width of the scroll.

Just yesterday, either Chrom was updated, or jQuery (loads the latest from the Google CDN), but Chrome began to add scroll (maybe it appears to IE). Maybe there is a way to position an element without writing all versions of "buggy" browsers?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vasya_Sh, 2010-11-18
@Vasya_Sh

In jQuery, everything is fine, the interpretation of chrome has changed, now it does not compensate for scrolling.

A
Alex Shinkevich, 2010-11-18
@aleXoid

Is it common chrome or innovations in webkit?

M
Mithgol, 2010-11-19
@Mithgol

Try using (with appropriate modifications) the $(…).position() method instead of the $(…).offset() method , if the conditions of the problem allow it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question