Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the position of a element based on it's parent

Tags:

html

jquery

css

hi I have a div which contains just a image like this :

(div)
    (img. …)
(/div)

Now,I want to create a mark in the image ,s I use :

$(div).append((img src =mark.jpg id =mark)).

then set its position :

$(div img#mark).position(…)

Here I don't know how to set it.

I just want to make the mark's position within its parent .

In fact ,what I want like the google map ,when you search something in google map ,the results will be marked in the map .

Of course,my require is much easy.

I have thought to get the offset of the parent ,then caculate the really position of the mark image .

But I can't make it. Any idea ?

Btw ,I ask this in my phone ,so I can't make a pretty format.

like image 741
hguser Avatar asked Jan 10 '11 15:01

hguser


2 Answers

You could use CSS:

div { position: relative; }
div img#mark { position:absolute; }

And then set the position like this:

$("div img#mark").css({
    'top': '10px',
    'left': '10px'
});

Does that do what you meant?

like image 105
polarblau Avatar answered Sep 28 '22 06:09

polarblau


$("div").css('position', 'relative');
$("div img#mark").css({
    position: 'absolute',
    top: '0px',
    left: '0px'
});

Change top and left values to suit where you would like to position the mark.

like image 30
Matt Asbury Avatar answered Sep 28 '22 08:09

Matt Asbury