<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.magifier {
margin: 20px auto;
width: 500px;
display: flex;
justify-content: flex-start;
align-items: flex-start;
}
.magifier .abbre {
width: 200px;
height: 200px;
position: relative;
box-sizing: border-box;
}
.magifier .abbre img {
width: 100%;
height: 100%;
}
.magifier .abbre .mask {
position: absolute;
z-index: 3;
display: none;
top: 0;
left: 0;
width: 80px;
height: 80px;
border: 1px solid #aaa;
background: 50% top no-repeat #fede4f;
opacity: 0.5;
-moz-opacity: 0.5;
-khtml-opacity: 0.5;
filter: alpha(Opacity=50);
cursor: move;
}
.magifier .detail {
box-sizing: border-box;
position: relative;
/* 為什么此處沒有寬高的時候,小圖展示不全耸采?授舟?妈嘹? */
width: 300px;
height: 300px;
overflow: hidden;
display: none;
}
.magifier .detail img {
position: absolute;
top: 0;
left: 0;
z-index: 10;
}
</style>
</head>
<body>
<section class="magifier">
<!-- 縮略圖 -->
<div class="abbre">
<img src="https://img11.360buyimg.com/n1/jfs/t1/175546/26/9058/217713/609b376dEc171ccc5/1f6c5290df3830da.jpg"
alt="" sizes="" srcset="" />
<div class="mask"></div>
</div>
<!-- 詳情圖 -->
<div class="detail">
<img src="https://img11.360buyimg.com//n0/jfs/t1/175546/26/9058/217713/609b376dEc171ccc5/1f6c5290df3830da.jpg"
alt="" />
</div>
</section>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
$(function () {
let $magifier = $(".magifier");
$abbre = $magifier.find(".abbre");
$mask = $abbre.find(".mask");
$detail = $magifier.find(".detail");
$detailIMG = $detail.find("img");
let abbreOffset = $abbre.offset(); //abbre的相對于document的偏移具伍,原生的aip為offsetLeft
// console.log("$magifier", $magifier, $abbre, $mask, $detail, $detailIMG);
// console.log("mousemove");
// 計算出大圖的大小
let abbreW = $abbre.width();
(abbreH = $abbre.height()),
(maskW = $mask.width()),
(maskH = $mask.height()),
(detailW = $detail.width()),
(detailH = $detail.height()),
(detailIMGW = 0),
(detailIMGH = 0);
// abbre/mask = detailIMG/detail;
// abbre*detail/mask = detailIMG;
detailIMGW = (abbreW * detailW) / maskW;
detailIMGH = (abbreH * detailH) / maskH;
// console.log("大圖的寬高為", detailIMGW, detailIMGH);
$detailIMG.css({
width: detailIMGW,
height: detailIMGH,
});
// mask的位置
// 鼠標的位置 - abbre的偏移 - mask的寬高的一半
// mask不可以超過的邊界
// 最小的top/left = 0
// 最大top/left = abbre - mask;
const computed = function computed(ev) {
console.log("ev", ev);
let curLeft = ev.pageX - abbreOffset.left - maskW / 2;
let curTop = ev.pageY - abbreOffset.top - maskH / 2;
// 處理邊界情況
let minL = 0,
minT = 0,
maxL = abbreW - maskW,
maxT = abbreH - maskH;
curLeft = curLeft < minL ? minL : curLeft > maxL ? maxL : curLeft;
curTop = curTop < minT ? minT : curTop > maxT ? maxT : curTop;
$mask.css({
left: curLeft,
top: curTop,
});
// mask在abbre里面移動的距離/abbre的寬度 = detailIMG在detail移動的距離/detail的寬度
$detailIMG.css({
left: -curLeft / abbreW * detailIMGW,
top: -curTop / abbreH * detailIMGH
})
};
$abbre
.mouseenter(function (ev) {
$mask.css("display", "block");
$detail.css("display", "block");
computed(ev);
})
.mousemove(function (ev) {
computed(ev);
})
.mouseleave(function (ev) {
$mask.css("display", "none");
$detail.css("display", "none");
});
});
</script>
</html>
放大鏡效果代碼實現(xiàn)
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門夏志,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人苛让,你說我怎么就攤上這事沟蔑。” “怎么了狱杰?”我有些...
- 文/不壞的土叔 我叫張陵溉贿,是天一觀的道長。 經(jīng)常有香客問我浦旱,道長宇色,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任颁湖,我火速辦了婚禮宣蠕,結果婚禮上,老公的妹妹穿的比我還像新娘甥捺。我一直安慰自己抢蚀,他們只是感情好,可當我...
- 文/花漫 我一把揭開白布镰禾。 她就那樣靜靜地躺著皿曲,像睡著了一般。 火紅的嫁衣襯著肌膚如雪吴侦。 梳的紋絲不亂的頭發(fā)上屋休,一...
- 文/蒼蘭香墨 我猛地睜開眼易阳,長吁一口氣:“原來是場噩夢啊……” “哼附较!你這毒婦竟也來了?” 一聲冷哼從身側響起潦俺,我...
- 正文 年R本政府宣布,位于F島的核電站帝蒿,受9級特大地震影響荐糜,放射性物質發(fā)生泄漏。R本人自食惡果不足惜葛超,卻給世界環(huán)境...
- 文/蒙蒙 一暴氏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧绣张,春花似錦答渔、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至芜飘,卻和暖如春务豺,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背嗦明。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 什么是淘寶放大鏡 如圖 實現(xiàn)思路 左邊一個小圖,右邊一個原圖累榜,當鼠標在小圖上移動的時候营勤,通過更改left和top的...
- 放大鏡效果在電商網(wǎng)站上較為常見的 一個效果,主要針對鼠標在圖片選取部分放大查看葛作。效果如圖所示寿羞。 分析頁面可知,頁面...