Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:05:01+00:00 2026-06-14T11:05:01+00:00

I have successfully centered my image using JavaScript and css. The issue I am

  • 0

I have successfully centered my image using JavaScript and css. The issue I am having is that the coordinates for the centered image are not what I expected. Instead of the Top and Left values being relative to the top and left of the containing div, they represent the exact center of the image. Here is the fiddle and I will paste in my code. I need to know the exact top and left of the frame so I can calculate an offset on the server side.

Html:

<div id="containment-wrapper">
    <img id="imgFrame" class="centerImg" src="http://i.imgur.com/2KKIB.png" name=
    "imgFrame">

    <div id="drag_resize" style=
    "position: absolute; z-index: 1; width:500px; height: 375px;"><img id="#imgSource"
    src="http://data.whicdn.com/images/35067333/ocean-life-029_large.jpg" style=
    "width: 100%; height:100%;" name="#imgSource"></div>
  </div>

  <div style="height: 200px;"></div>Frame Position:

  <form>
    <input type="text" id="framePos"><br>
    Image Size: <input type="text" id="imgSize" style="width: 500px;"><br>
    Orgignal Image Position: <input type="text" id="imgPos" style="width: 500px;"><br>
    Calcualted Image Position: <input type="text" id="imgPosCalc" style=
    "width: 500px;"><br>
  </form>

CSS:

#containment-wrapper {
  border:1px solid; width: 432px;
  height: 348px; position: relative;
  border: solid 1px;display: table;
  text-align: center;
}

.centerImg {
    display:block;
    margin:auto;
    max-width:100%;
    top:50%;
    left:50%;
    opacity:0;
    position: absolute; z-index: 9999; pointer-events:none;  
}​

JavaScript:

$(function () {
    var frameTop = 0;
    var frameLeft = 0;

    $('#imgFrame').each(function () {
        imgheight = Math.round($(this).height() / 2);
        imgwidth = Math.round($(this).width() / 2);
        $(this).attr("style", "margin-top: -" + imgheight + "px; margin-left: -" + imgwidth + "px; opacity:1;");
    });

    var pos = $('#imgFrame').position();
    $('#framePos').val("Top: " + pos.top + " Left: " + pos.left);
    frameTop = pos.top;
    frameLeft = pos.left;

    $("#drag_resize").resizable({
        aspectRatio: true,
        resize: function (event, ui) {
            $('#imgSize').val("W: " + ui.size.width + " H: " + ui.size.height);
        },
        handles: 'e,w,n,s'


    }).draggable({
        drag: function (event, ui) {
            $('#imgPos').val("Top: " + (ui.position.top) + " Left: " + (ui.position.left));
            $('#imgPosCalc').val("Top: " + (ui.position.top - frameTop) + " Left: " + (ui.position.left - frameLeft));

        }
    });

    $('#imgSource').css({
        'width': '200px',
        'height': '200px;'
    })
});​
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:05:02+00:00Added an answer on June 14, 2026 at 11:05 am

    I added a function to calculate the true top and true left position of the frame:

    function getTrueXY(){
            var img = document.getElementById('imgFrame'); 
            //or however you get a handle to the IMG
            var fameWidth= img.clientWidth;
            var frameHeight= img.clientHeight;
    
            var container = document.getElementById('containment-wrapper');
            var conWidth= container .clientWidth;
            var conHeight = container .clientHeight;
    
            var trueLeft = Math.round((conWidth / 2) - (fameWidth / 2));
            var trueTop = Math.round((conHeight / 2) - (frameHeight / 2));
    
    
    
        }
    

    I updated the fiddle as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully resized images using GD library. I resize any image to 350
I have successfully replaced the button with the image. But it appears that the
I have successfully created a feature in sharepoint that modifies the existing edit dialog
I have successfully been using the eclipse Indigo internal browser to view my web
I have successfully installed mercurial on media temple grid server. But I can not
I have successfully issued a Start-Backup request to my local RavenDB using an asynchronous
I have successfully fetched an image from photo library and I have set it
I have successfully implemented django-ajax-uploader into my project. But I do not know how
I have successfully scheduled local notifications in my app using the code below: Class
I have successfully setup castle windsor using an xml configuration file and everything works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.