Scaling an inline image from the absolute center of its parent?

只愿长相守 提交于 2019-12-06 16:54:40

If all you are trying to achieve is keeping the centre of an image in a constant position, then you just need to adjust the top and left properties of the image, so that the centre of the image lies in the middle of the surrounding frame.

The calculation is simply finding the midpoint of your frame, which is half way along it, and subtracting half the width of the image to find your left position, the same with heights for the top position.

I have made you a demonstration program to show you, change the widths in the box, maximum of 500, and you will see the image stays centred.

Demo: http://jsfiddle.net/Orbling/ZZrjP/

Centred Image - 200x200

Centred Image - 400x400

I am using the #outer-frame to represent your window, or outer div of your website, the #image-frame is whatever size you want the image to appear at. If you want your image to occupy the full window, dispense with the concept of the outer frame and use the window as your image frame.

HTML:

<div id="outer-frame">
    <div id="image-frame">
        <img src="http://fc01.deviantart.net/fs50/f/2009/317/f/4/Colour_Spiral_by_Deatant2.jpg" alt="Spiral by Deatant2" width="1280" height="736" />
    </div>
</div>
<div id="controls">
    Width: <input type="text" id="iwidth" name="iwidth" value="200" />
    Height: <input type="text" id="iheight" name="iheight" value="200" />
</div>

CSS:

#outer-frame { width: 500px; height: 500px; margin: 10px; border: 1px dashed black; }
#image-frame { width: 200px; height: 200px; overflow: hidden; position: relative; }
#image-frame img { position: relative; }
#controls { margin: 10px 10px 0 10px; }
#controls input { width: 80px; }

Note the position: relative;​ directives, these ensure the positions are relative to the container, if they were relative to the page, the calculations would need to be offset.

jQuery:

$('#image-frame').bind('reposition-image', function() {
    var imageFrame = $(this);
    var outerFrame = $('#outer-frame');
    var image = imageFrame.find('img');

    var frameWidth = imageFrame.width();
    var frameHeight = imageFrame.height();

    // center image frame
    imageFrame.css({ left: (outerFrame.width() / 2) - (imageFrame.width() / 2),
                     top:  (outerFrame.height() / 2) - (imageFrame.height() /2)});

    // position image in frame
    image.css({ left: (imageFrame.width() / 2) - (image.attr('width') / 2),
                top: (imageFrame.height() / 2) - (image.attr('height') / 2)});
}).trigger('reposition-image');

$('#controls input').change(function() {
    var jThis = $(this);
    var cDim = jThis.attr('id').replace(/^i/, '');

    var nSize = parseInt(jThis.val());

    if (!isNaN(nSize) && nSize != 0) {
        var outerFrame = $('#outer-frame');
        nSize = (nSize > outerFrame[cDim]() ? outerFrame[cDim]() : nSize);        
        $('#image-frame')[cDim](nSize).trigger('reposition-image');
    }
});
​
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!