472,805 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Center <IMG> in <DIV>

Hi all
I have a javascript function that drags and drops an element (ie img)
into a container (ie bordered div). The function works and returns the
element and and container.
My next step is to center the element in the container if the user is
sloppy with positioning it.
I can only test this on IE6 and IE5.5 and it fails. It positions the
element too much right and low.
Here is the function:
function SnapToContainer(Container,El) {
//X = left
//Y = top

var Cont_Style = Container.style
var El_Style = El.style

//get container's height and width and centers
var Cont_Width = parseInt(Cont_Style.width);//int value
var Cont_Center_X = Cont_Width/2//int value
var Cont_Height = parseInt(Cont_Style.height);//int value
var Cont_Center_Y = Cont_Height/2;//int value

//get the container's left and top
var Cont_Left = parseInt(Cont_Style.left);//int value
var Cont_Top = parseInt(Cont_Style.top);//int value

//get element's height and width and centers
var El_Width = parseInt(El_Style.width);//int value
var El_Center_X = El_Width/2;//int value
var El_Height = parseInt(El_Style.height);//int value
var El_Center_Y = El_Height/2;//int value

//new positions
var lft = Cont_Left + (Cont_Center_X - El_Center_X);//int value
var tp = Cont_Top + (Cont_Center_Y - El_Center_Y);//int value

//check if the container is larger than the element
if (Cont_Width >= El_Width && Cont_Height >= El_Height)
{
//move the element
El_Style.top = tp+"px";//string value
El_Style.left = lft+"px";//string value
//reset the starting coords
El.yPos = tp;//int value
El.xPos = lft;//int value

return true;
}

return false;
}

Here are the elements:
<p>
<img id="helpI" src="help.gif" onMouseDown="return
dragImg(this,event)" style="width=27;height=27"/>
<img id="printI" src="print.gif" onMouseDown="return
dragImg(this,event)" " style="width=27;height=27"/>
<img id="emailI" src="email.gif" onMouseDown="return
dragImg(this,event) style="width=27;height=27"/>
</p>

And here are some of the containers:
<div id="placeholder1"
style="position:absolute;left:50px;top:320px;width =150;height=50;border=solid
2px">
</div>
<div id="placeholder2"
style="position:absolute;left:50px;top:520px;width =150;height=50;border=solid
2px">
</div>

Jan 11 '07 #1
1 4536

Carl wrote:
Hi all
I have a javascript function that drags and drops an element (ie img)
into a container (ie bordered div). The function works and returns the
element and and container.
My next step is to center the element in the container if the user is
sloppy with positioning it.
I can only test this on IE6 and IE5.5 and it fails. It positions the
element too much right and low.
Your issue is with CSS, not javascript. Once you have worked out how
to properly center your element, then you can apply script to make that
happen.

Ask about centering the image at
comp.infosystems.www.authoring.stylesheets

<URL:
http://groups.google.com.au/group/co...ng.stylesheets
>
But fix all the syntax errors first.
Here is the function:
function SnapToContainer(Container,El) {
[...]
var Cont_Center_X = Cont_Width/2//int value
Cont_Center_X will only be an int if Cont_Width is even. Similarly for
other values divided by 2 that you seem to expect to give integer
results.

[...]
Here are the elements:
<p>
<img id="helpI" src="help.gif" onMouseDown="return
dragImg(this,event)" style="width=27;height=27"/>
You have some typos here and elsewhere:

- you appear to be using XHTML, attribute names must be lower case
- use colons ":" for CSS property/value separators, not equals "="
- CSS lengths must have a unit unless the value is zero
--
Rob

Jan 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

61
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
2
by: Marcus Otmarsen | last post by:
Ok, the situation is as follows: I defined a <DIV> like: <div id="myid" style="position: absolute; height: 10; width: 10;"><img src="images/object.gif" height=10 width=10></div> This pane is...
1
by: Alan Hoyle | last post by:
I was using a <table border> to generate borders around some info/images, and decided to follow the w3c guidelines and convert it to CSS boxes with borders since it wasn't really tabular data. ...
7
by: Zhang Weiwu | last post by:
Hello. This is problem puzzled me a long time. I wish to organize some block elements and let them flow one after each other like text. Think about a album, I wish the album have 12 thumbnails,...
3
by: Henry Johnson | last post by:
Okay - I'm spinning my wheels on this one... can someone help me figure out how to programmatically populate a table cell as follows (from C# code-behind)? I've tried using a Literal control in the...
10
by: News | last post by:
I am trying to be able to manipulate the width and height of an <img> but do not seem to be able. "Yes", I know the JavaScript will "not" manip anything, which is ok. I simply do not know how to...
5
by: Agix | last post by:
Hi there, Please check out : http://clarifysolutions.co.uk/certenroll/ The source is included below. This page is a test, so I can play about with paddings, margins and layouts using divs as...
5
nathj
by: nathj | last post by:
Hi All, I'm working on a new site that has a two column layout underneath a title bar. If you check out: http://www.christianleadership.org.uk/scratch/mainpage.php using IE or Opera you will...
4
by: harryusa | last post by:
I am trying to center 2 images concentrically which are z-indexed to lay on top of each other making an image with a border from another image that has a transparent center. I need the images to be...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.