有一個很好的css技巧,始終將圖像放在div的中心,而不管圖像大小或縱橫比如何。如何在JS中旋轉後改變圖像的CSS?
<style>
.img_wrap {
padding-bottom: 56.25%;
width: 100%;
position: relative;
overflow: hidden;
}
#imgpreview {
display: block;
max-height: 100%;
max-width: 100%;
width: auto;
height: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
</style>
<div class="img_wrap">
<img id="imgpreview" src="http://i.imgur.com/RRUe0Mo.png" alt="your image" />
</div>
然後我說jQuery代碼用於旋轉圖像
$(document).ready(function(){
var rotation = 0;
jQuery.fn.rotate = function(degrees) {
$(this).css({'-webkit-transform' : 'rotate('+ degrees +'deg)',
'-moz-transform' : 'rotate('+ degrees +'deg)',
'-ms-transform' : 'rotate('+ degrees +'deg)',
'transform' : 'rotate('+ degrees +'deg)'});
};
$('#imgpreview').click(function() {
rotation += 90;
$(this).rotate(rotation);
// $(this).toggleClass("imgpreview");
});
});
然而,當我旋轉圖像,它就會被裁剪。我想避免這種情況。
我試圖玩addClass功能,但沒有成功。如果有人可以建議任何解決方案將會升值很多。
我爲此問題創建了jsfidlle。 這裏updated小提琴
刪除'溢出:隱藏;'從.IMG換行,它不會裁剪 –
對不起,我沒有正確解釋這個問題。我的意思是我想要旋轉的圖像採取img_wrap大小。在這種情況下,原始圖片是橫向導向的,所以旋轉90度時,我希望它在寬度上「縮小」,這樣寬度將採用img_wrap高度的值。 –
請注意'img_wrap'具有零高度 - 它具有的任何高度都來自'padding-bottom' – kukkuz