0
我放在一起jQuery和css函數放大和縮小鼠標懸停的圖像,同時保持約束框大小不變。我發現這是一個例子,並編輯更多我想要的。如何在不影響圖像大小的情況下使用響應式圖像進行居中放大?
演示是在這裏:https://jsfiddle.net/2fken8Lg/1/
下面是代碼:
JS:
$('.zoom img').on({
mouseover: function() {
var $scale = 1.5;
if (!$(this).data('w')) {
var $w = $(this).width();
var $h = $(this).height();
$(this).data('w', $w).data('h', $h);
}
$(this).stop(true).animate({
width: $(this).data('w') * $scale,
height: $(this).data('h') * $scale,
left: -$(this).data('w') * ($scale - 1)/2,
top: -$(this).data('h') * ($scale - 1)/2
}, 'fast');
},
mouseout: function() {
$(this).stop(true).animate({
width: $(this).data('w'),
height: $(this).data('h'),
left: 0,
top: 0
}, 'fast');
}
});
CSS:
.zoom {
position: relative;
float: left;
margin: 30px 0 0 30px;
width: 400px;
height: 180px;
overflow: hidden;
border: 1px solid #000;
}
img {
position: absolute;
width: 400px;
height: 180px;
}
HTML:
<div class="zoom">
<img src="https://www.lamborghini.com/en-en/sites/en-en/files/DAM/it/models_gateway/blocks/special.png">
</div>
它與固定的圖像大小很好,但我的問題是如何將其擴展到響應圖像?我的網頁完全基於響應能力,所以我無法在任何地方獲得固定的CSS寬度或高度,因爲它會混淆不同的瀏覽器大小。無論如何要做我正在嘗試完成的響應圖像或沒有css?
你可以使用轉換和CSS動畫。看看這個CodePen。 https://codepen.io/afinedayproductions/pen/EzvLp/ – Keith
@Keith這看起來很有希望,我會編輯我的jsfiddle,看看它是否有效(使用jsfiddle沒有js的諷刺是令人難以置信的,我知道) – Twisterz