需求與思路
做電商網站時,商品圖的放大鏡效果幾乎是標配。最初我嘗試直接裁剪圖片,卻發現難以實現。後來找到一個更好的思路:把展示區做成一小塊可視區域,放進一張較大的圖,超出可視範圍的部分隱藏起來,再透過移動一個「放大框」對應到可視區的位移,就能實現放大效果。先來看HTML結構。
<div id="pic_wrap"> <div id="float_box"></div> <img src="1.jpg"> </div> <div id="show"> <img src="1.jpg" id="big_img"> </div>
最外層兩個 div 分別是商品原圖區與放大後的可視區,float_box 就是放大鏡框,第一張 img 是商品圖,第二張 img 是預留的放大大圖。CSS 如下:
img { width: 250px; height: 150px; }
#pic_wrap { position: relative; width: 250px; height: 150px; }
#float_box {
width: 100px; height: 100px;
background-color: green;
filter: opacity(alpha: 30);
opacity: 0.3;
position: absolute;
display: none;
}
#big_img {
background-image: url(1.jpg);
height: 450px; width: 750px;
background-repeat: no-repeat;
background-size: cover;
position: relative;
}
#show {
width: 300px; height: 300px;
background-color: white;
opacity: 1;
filter: opacity(alpha:1);
overflow: hidden;
display: none;
}
HTML與CSS完成後,透過JS加上互動效果。整體步驟:
onmouseover),顯示放大鏡與可視窗。onmousemove),同步更新放大框與大圖。onmouseout),隱藏放大鏡與可視窗。完整程式碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
img { width: 250px; height: 150px; }
#pic_wrap { position: relative; width: 250px; height: 150px; }
#float_box {
width: 100px; height: 100px;
background-color: green;
filter: opacity(alpha: 30);
opacity: 0.3;
position: absolute;
display: none;
}
#big_img {
background-image: url(1.jpg);
height: 450px; width: 750px;
background-repeat: no-repeat;
background-size: cover;
position: relative;
}
#show {
width: 300px; height: 300px;
background-color: white;
opacity: 1;
filter: opacity(alpha:1);
overflow: hidden;
display: none;
}
</style>
</head>
<body>
<div id="pic_wrap">
<div id="float_box"></div>
<img src="1.jpg">
</div>
<div id="show">
<img src="1.jpg" id="big_img">
</div>
<script>
var pic_wrap = document.getElementById('pic_wrap'),
float_box = document.getElementById("float_box"),
show = document.getElementById('show'),
big_img = document.getElementById("big_img");
// 鼠標移入,讓放大鏡與放大區顯示
pic_wrap.onmouseover = function() {
float_box.style.display = "block";
show.style.display = "block";
};
// 鼠標移動時,即時更新
pic_wrap.onmousemove = function(event) {
floatMove(float_box, pic_wrap, event);
showPic();
};
// 鼠標移出,隱藏
pic_wrap.onmouseout = function() {
float_box.style.display = "none";
show.style.display = "none";
};
// 即時獲取樣式值(offset 含邊框易出錯,故用 style)
function getStyle(obj, attr) {
if (obj.currentStyle) {
return parseInt(obj.currentStyle[attr]);
} else {
return parseInt(getComputedStyle(obj, null)[attr]);
}
}
// 控制放大鏡在原圖中的位置
function floatMove(argu1, argu2, event) {
var e = event || window.event;
var minLeft = getStyle(argu1, "width");
var maxLeft = getStyle(argu2, "width") - minLeft / 2;
var minHeight = getStyle(argu1, "height");
var maxHeight = getStyle(argu2, "height") - minHeight / 2;
if (e.clientX < minLeft / 2) {
float_box.style.left = "0px";
} else if (e.clientX > maxLeft) {
float_box.style.left = getStyle(argu2, "width") - getStyle(argu1, "width") + "px";
} else {
float_box.style.left = event.clientX - minLeft / 2 + "px";
}
if (e.clientY < minHeight / 2) {
float_box.style.top = "0px";
} else if (e.clientY > maxHeight) {
float_box.style.top = getStyle(argu2, "height") - getStyle(argu1, "height") + "px";
} else {
float_box.style.top = event.clientY - minHeight / 2 + "px";
}
}
// 依座標計算並更新可視區的大圖
function showPic() {
var iCurLeft = getStyle(float_box, "left");
var iCurTop = getStyle(float_box, "top");
var a = getStyle(pic_wrap, "width") - getStyle(float_box, "width");
var b = getStyle(big_img, "width") - getStyle(show, "width");
var moveWidth = -(iCurLeft / a) * b;
big_img.style.left = moveWidth + "px";
var c = getStyle(pic_wrap, "height") - getStyle(float_box, "height");
var d = getStyle(big_img, "height") - getStyle(show, "height");
var moveHigth = -(iCurTop / c) * d;
big_img.style.top = moveHigth + "px";
}
</script>
</body>
</html>
關鍵在於 floatMove 負責讓放大框在原圖內移動並限制邊界,而 showPic 則依放大框的位置比例,反推出大圖在可視區內的位移,兩者搭配就能呈現流暢的放大效果。