Given X and Y coordinates, I want to position an element so it's in that position on screen/window.
I want scroll to work as normal, so position fixed isn't a solution. I want what fixed does but the element should move like a normal element when scrolling.
There are no guarantees what the parent html element position will be anything. That is it can be static, relative, absolute or fixed).
I understand css only solution is not possible and that's OK.
The position of (X, Y) means the co-ordinate of an element at the top-left point in a document. X represent the horizontal position and Y represent the vertical position. Use element. getBoundingClientRect() property to get the position of an element.
If the element is in the main document you can get the DIV's coordinates with... var X=window. getComputedStyle(abc,null). getPropertyValue('left'); var Y=window.
Draw a Circle arc(x,y,r,startangle,endangle) - creates an arc/curve. To create a circle with arc(): Set start angle to 0 and end angle to 2*Math. PI. The x and y parameters define the x- and y-coordinates of the center of the circle.
The equivalent of fixed positioning but with scroll is position: absolute
. It is relative to the parent position, as long as that doesn't have static positioning.
In the CSS:
#yourElement {
position: absolute;
}
And in JavaScript with x
and y
values:
var element = document.getElementById("yourElement");
element.style.left = x + "px";
element.style.top = y + "px";
Since this one is still open, got bored with JS Fiddle... Not sure if i meet the requirements of what you wanted?
http://jsfiddle.net/9Lz58n3o/
Example Page
<div>
using this div as an example of large text / body contents... can be ignored
</div>
<div class="myStaticPos" tmpleft="32" tmptop="32" style="left:32px; top:32px;"></div>
<div class="myStaticPos" tmpleft="123" tmptop="123" style="left:123px; top:123px;"></div>
CSS
.myStaticPos {
position:absolute;
background-color:#d00;
width:32px;
height:32px;
}
Javascript
$(function() {
$(window).scroll(function() {
alignElements();
});
});
function alignElements() {
var scrollTop = $(window).scrollTop();
$( ".myStaticPos" ).each(function() {
$(this).offset({ top: scrollTop + parseInt($(this).attr("tmptop")), left: parseInt($(this).attr("tmpleft")) });
});
}
This above example and in the fiddle link is one way of using a elements offset to have it always appear regardless of scrolling.. Otherwise a position absolute is a position absolute..
If this does not suite your needs or cant be adjusted, then please verify the question a little more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With