JavaScript實現(xiàn)多張圖片放大鏡效果示例【不限定圖片尺寸,rem單位】
本文實例講述了JavaScript實現(xiàn)多張圖片放大鏡效果。分享給大家供大家參考,具體如下:
效果如下:可以展示圖片列表的放大鏡效果,圖片尺寸沒有要求會自動調(diào)整至水平垂直居中效果

代碼如下,除了圖片要替換一下,其它的可直接運行查看效果,enlarge是圖片要放大查看的倍數(shù),注意:.bigBox的寬高與.tool的寬高比值要與enlarge保持一致,比如本例中這個比值是4
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
function fontAuto() {
document.documentElement.style.fontSize = document.documentElement.clientWidth / 19.2 + 'px';
}
fontAuto();
window.onresize = function () {
fontAuto();
}
</script>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
/*圖片放大鏡*/
.result-list li {
float: left;
width: 3rem;
margin: 0.15rem;
border: 1px solid #ddd;
padding: 0.08rem;
border-radius: 0.05rem;
list-style-type: none;
}
.result-list li:hover {
box-shadow: 0 0 10px 5px #ddd;
}
.img-to-big {
width: 100%;
height: 1.5rem;
margin: 0 auto;
}
.small-box {
width: 100%;
height: 1.5rem;
border: 1px #ccc solid;
cursor: move;
position: relative;
vertical-align: middle;
display: block;
}
.small-box img {
max-width: 100%;
max-height: 100%;
margin: auto;
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.tool {
width: 1rem;
height: 1rem;
background-color: lightgray;
opacity: 0.6;
filter: alpha(opacity=60);
position: absolute;
left: 0;
top: 0;
display: none;
}
.tool.active {
display: block;
}
.big-box {
width: 4rem;
height: 4rem;
overflow: hidden;
border: 2px solid lightgray;
position: absolute;
background: #fff;
display: none;
left: 3rem;
z-index: 100;
}
.big-box.active {
display: table-cell;
vertical-align: middle;
}
.big-box img {
position: absolute;
display: block;
}
/*圖片放大鏡*/
</style>
</head>
<body>
<div class="result-list">
<ul>
<li>
<div class="img-to-big">
<div class="small-box">
<img class="small-img" src="img/zs2.jpg"/>
<div class="tool"></div>
</div>
<div class="big-box">
<img src="img/zs2.jpg" class="big-img"/>
</div>
</div>
</li>
<li>
<div class="img-to-big">
<div class="small-box">
<img class="small-img" src="img/zs2.jpg"/>
<div class="tool"></div>
</div>
<div class="big-box">
<img src="img/zs2.jpg" class="big-img"/>
</div>
</div>
</li>
<li>
<div class="img-to-big">
<div class="small-box">
<img class="small-img" src="img/zs2.jpg"/>
<div class="tool"></div>
</div>
<div class="big-box">
<img src="img/zs2.jpg" class="big-img"/>
</div>
</div>
</li>
<li>
<div class="img-to-big">
<div class="small-box">
<img class="small-img" src="img/zs2.jpg"/>
<div class="tool"></div>
</div>
<div class="big-box">
<img src="img/zs2.jpg" class="big-img"/>
</div>
</div>
</li>
</ul>
</div>
<script>
window.onload = function () {
forImg();
window.onresize = function () {
forImg();
};
}
function forImg() {
var enlarge = 4;
var imgToBig = document.getElementsByClassName("img-to-big");
var list = document.getElementsByClassName("result-list")[0];
for (var i = 0; i < imgToBig.length; i++) {
var smallBox = imgToBig[i].getElementsByClassName("small-box")[0];//小盒子
var smallImg = smallBox.getElementsByClassName("small-img")[0];
var tool = imgToBig[i].getElementsByClassName("tool")[0];//小盒子中的灰色區(qū)域
var bigBox = imgToBig[i].getElementsByClassName("big-box")[0];//大盒子
bigBox.style.left = smallBox.offsetLeft + smallBox.offsetWidth + "px";
bigBox.style.top = smallBox.offsetTop + "px";
var bigImg = imgToBig[i].getElementsByClassName("big-img")[0];//放大的圖片
var leftNum = smallBox.offsetParent;
var num = leftNum.offsetLeft;
imgSize(smallBox, smallImg, smallImg.getAttribute("src"), bigImg, enlarge);
toBigImg(smallBox, tool, bigBox, bigImg, num, smallImg, list, enlarge);
}
function imgSize(smallBox, thisImg, src, bigImg, enlarge) {
var img = new Image();
img.src = src;
img.onload = function () {
var realWidth = img.width;
var realHeight = img.height;
if ((realWidth / smallBox.offsetWidth) >= (realHeight / smallBox.offsetHeight)) {//當展示的圖片尺寸并不統(tǒng)一時,根據(jù)圖片長寬比例確定圖片以高度還是寬度為準進行縮放展示
thisImg.style.width = smallBox.offsetWidth + "px";
thisImg.style.height = "auto";
bigImg.style.width = smallBox.offsetWidth * enlarge + "px";
bigImg.style.height = "auto";
} else {
thisImg.style.height = smallBox.offsetHeight + "px";
thisImg.style.width = "auto";
bigImg.style.height = smallBox.offsetHeight * enlarge + "px";
bigImg.style.width = "auto";
}
}
}
function toBigImg(smallBox, tool, bigBox, bigImg, num, smallImg, list, enlarge) {
smallBox.onmouseenter = function () {
tool.className = "tool active";
bigBox.className = "big-box active";
};
//鼠標離開小盒子區(qū)域,不顯示黃色區(qū)域和大盒子
smallBox.onmouseleave = function () {
tool.className = "tool";
bigBox.className = "big-box";
};
//鼠標在小盒子內(nèi)移動
smallBox.onmousemove = function (e) {
var _e = window.event || e;//事件對象
var x = _e.clientX - this.offsetLeft - tool.offsetWidth / 2 - num;//事件對象在小盒子內(nèi)的橫向偏移量
var y = _e.clientY - this.offsetTop - list.offsetTop - tool.offsetHeight / 2;//豎向偏移量
if (x < 0) {
x = 0;//當左偏移出小盒子時,設(shè)為0
}
if (y < 0) {
y = 0;//當上偏移出小盒子時,設(shè)為0
}
if (x > this.offsetWidth - tool.offsetWidth) {
x = this.offsetWidth - tool.offsetWidth;//當右偏移出小盒子時,設(shè)為小盒子的寬度-黃色放大區(qū)域?qū)挾?
}
if (y > this.offsetHeight - tool.offsetHeight) {
y = this.offsetHeight - tool.offsetHeight;//當下偏移出小盒子時,設(shè)為小盒子的高度-黃色放大區(qū)域高度
}
tool.style.left = x + "px";//灰色放大區(qū)域距離小盒子左偏距
tool.style.top = y + "px";//灰色放大區(qū)域距離小盒子上偏距
bigImg.style.left = (-x + smallImg.offsetLeft) * enlarge + "px";//放大圖片移動方向相反,偏移距離加倍
bigImg.style.top = (-y + smallImg.offsetTop) * enlarge + "px";
}
}
}
</script>
</body>
</html>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript圖片操作技巧大全》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript運動效果與技巧匯總》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
關(guān)于uniapp微信小程序左上角返回按鈕的監(jiān)聽詳解
uniapp是一個支持多端的技術(shù),因此它是兼容性比較強的,而且速度也很快,下面這篇文章主要給大家介紹了關(guān)于uniapp微信小程序左上角返回按鈕監(jiān)聽的相關(guān)資料,需要的朋友可以參考下2022-04-04
使用JavaScriptCore實現(xiàn)OC和JS交互詳解
JavaScriptCore是webkit的一個重要組成部分,主要是對JS進行解析和提供執(zhí)行環(huán)境。下面這篇文章主要給大家介紹了使用JavaScriptCore實現(xiàn)OC和JS交互的相關(guān)資料,文中介紹的非常詳細,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。2017-03-03
javascript實現(xiàn)類似于新浪微博搜索框彈出效果的方法
這篇文章主要介紹了javascript實現(xiàn)類似于新浪微博搜索框彈出效果的方法,涉及javascript彈出搜索框的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
JavaScript this調(diào)用規(guī)則說明
我希望通過這些來使你們理解各種函數(shù)調(diào)用方式的不同,讓你的JavaScript代碼遠離bugs。2010-03-03
webpack中CommonsChunkPlugin詳細教程(小結(jié))
本篇文章主要介紹了webpack中CommonsChunkPlugin詳細教程(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
JavaScript計算字符串中每個字符出現(xiàn)次數(shù)的小例子
這篇文章介紹了在JS中計算字符串中每個字符出現(xiàn)的次數(shù),有需要的朋友可以參考一下2013-07-07
bootstrap表格內(nèi)容過長時用省略號表示的解決方法
這篇文章主要介紹了bootstrap表格內(nèi)容過長時用省略號表示的解決方法,需要的朋友可以參考下2017-11-11

