function
img_adaption(img) {
if
(!$(img).hasClass(
'img_adapted'
)) {
$(img).css({
'display'
:
'none'
});
var
originalWidth = $(img).width();
var
originalHeight = $(img).height();
var
parentWidth = $(img).parent().width();
var
parentHeight = $(img).parent().height();
var
originalScale = originalWidth / originalHeight;
var
parentScal = parentWidth / parentHeight;
var
scaleNum, newImgWidth;
var
newImgHeight = 0;
var
changeNum = 0;
if
($(img).parent().css(
'position'
) ==
'static'
) {
$(img).parent().css({
'position'
:
'relative'
,
'overflow'
:
'hidden'
})
}
if
(originalScale > parentScal) {
scaleNum = originalHeight / parentHeight;
newImgWidth = originalWidth / scaleNum;
changeNum = (newImgWidth - parentWidth) / 2;
$(img).css({
'width'
:
'auto'
,
'height'
: parentHeight,
'margin-left'
: -changeNum,
'display'
:
'block'
,
'border-radius'
:
'0'
});
$(img).fadeIn();
}
else
if
(originalScale < parentScal) {
scaleNum = originalWidth / parentWidth;
newImgHeight = originalHeight / scaleNum;
changeNum = (newImgHeight - parentHeight) / 2;
$(img).css({
'width'
: parentWidth,
'height'
:
'auto'
,
'margin-top'
: -changeNum,
'display'
:
'block'
,
'border-radius'
:
'0'
});
$(img).fadeIn();
}
else
{
$(img).css({
'width'
:
'100%'
,
'height'
:
'100%'
,
'display'
:
'block'
});
$(img).fadeIn();
}
$(img).addClass(
'img_adapted'
);
}
else
{
$(img).css({
'display'
:
'block'
})
}
}