JavaScript實現(xiàn)京東放大鏡效果
本文實例為大家分享了JavaScript實現(xiàn)京東放大鏡展示的具體代碼,供大家參考,具體內(nèi)容如下
實現(xiàn)效果:
1.鼠標放到圖片上顯示放大鏡和詳細圖,鼠標離開時什么都不顯示(效果消失)
2.鼠標一直在放大鏡的中間,且放大鏡隨鼠標移動
3.放大鏡不能出縮列圖的盒子
4.鼠標放在詳細圖上詳細圖消失

實現(xiàn)細節(jié):
1.大盒子雖然比詳細圖的盒子寬度小,但是在邏輯上詳細圖的盒子屬于大盒子
2.詳細圖不能使用浮動,因為盒子下面一般會有文字內(nèi)容
3.以父盒子來定位詳細圖的盒子
4.放大鏡鼠標選中為十字形
5.對圖片進行定位才能使圖片移動
6.使用var evt = event || window.event; //兼容性寫法
7.用放大鏡頂點在盒子中的位置根據(jù)比例找到圖片的位置,來顯示圖片
8.圖片在詳細圖中的定位為負值
代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>京東放大鏡</title>
<style>
.box{ /*大盒子雖然比詳細圖的盒子寬度小,但是在邏輯上詳細圖的盒子屬于大盒子*/
width: 350px;
height: 350px;
position: relative;
margin: 100px;
border: 1px solid #aaa;
}
.box .detailed{ /*詳細圖不能使用浮動,因為盒子下面一般會有文字內(nèi)容*/
width: 450px;
height: 450px;
border: 1px solid #aaa;
position: absolute;
overflow: hidden;
left: 365px; /*以父盒子來定位*/
top: 0;
/*初始設置為不可見*/
display: none;
}
.box .normal .magnfier{
width: 150px;
height: 150px;
top: 0;
left: 0;
position: absolute;
background-color: rgba(0, 0, 255, 0.2);/*也可以用opacity來設置透明度*/
cursor: move; /*鼠標選中為十字*/
display: none; /*初始設為不可見*/
}
.detailed img{ /*對圖片進行定位使圖片移動*/
position: absolute;
top: 0;
left: 0;
}
</style>
<script>
function $(id){
return document.getElementById(id);
}
window.onload = function(){
var box = $('box');
var normal = box.children[0];
var margnfier = normal.children[1];//獲取放大鏡的Dom對象,document.getElementsByClassName來獲取。
var detailed = box.children[1];//獲得縮略圖DOM對象也可以用document.getElementsByClassName('zoom')[0];
var detailedImg = box.children[1].children[0];
normal.onmouseover = function(){//不能給box注冊onmousever事件,否則快速移到詳細圖上是詳細圖也不會消失,
margnfier.style.display = 'block';
detailed.style.display = 'block';
}
normal.onmouseout = function(){
margnfier.style.display = 'none';
detailed.style.display = 'none';
}
var x = 0;
var y = 0;
//控制zoom放大鏡部分在normal里面的移動
normal.onmousemove = function(event){
var evt = event || window.event;
//兼容性寫法
x = evt.clientX - box.offsetLeft - margnfier.offsetWidth / 2;
y = evt.clientY - box.offsetTop - margnfier.offsetHeight / 2;
//判斷鼠標是不是溢出了normal的區(qū)域,
if(x < 0){
x = 0;
}else{
if(x > box.offsetWidth - margnfier.offsetWidth){
x = box.offsetWidth - margnfier.offsetWidth;
}
}
if(y < 0){
y = 0;
}else{
if(y > box.offsetHeight - margnfier.offsetHeight){
y = box.offsetHeight - margnfier.offsetHeight;
}
}
margnfier.style.left = x + 'px';
margnfier.style.top = y + 'px';
var detailedX = -x * 800 / this.offsetWidth;
var detailedY = -y * 800 / this.offsetHeight;
//用放大鏡頂點在盒子中的位置根據(jù)比例找到圖片的位置,來顯示圖片
//改變圖片位置
detailedImg.style.left = detailedX + 'px';
detailedImg.style.top = detailedY + 'px';
}
}
</script>
</head>
<body>
<div id="box" class="box"> <!--包含詳細圖和縮略圖的盒子-->
<div class="normal">
<img src="imgs/show.jpg" alt="">
<div class="magnfier"></div>
</div>
<div class="detailed">
<img src="imgs/detail.jpg" alt="">
</div>
</div>
</body>
</html>

更多關于放大鏡的效果,請查看專題:《放大鏡特效》
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Webpack打包css后z-index被重新計算的解決方法
這篇文章主要跟大家分享了Webpack打包css后z-index被重新計算的解決方法,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編一起來學習學習吧。2017-06-06
用javascript實現(xiàn)截取字符串包含中文處理的函數(shù)
一直不知道js可以截取中文字符,呵呵,原理用正則表達式,匹配中文的長度,中文算兩個,因為算一個,是個好東西,推薦大家收藏2008-04-04
Bootstrap 填充Json數(shù)據(jù)的實例代碼
本篇文章主要介紹了Bootstrap 填充Json數(shù)據(jù)的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
javascript間隔定時器(延時定時器)學習 間隔調(diào)用和延時調(diào)用
這篇文章主要介紹了javascript間隔調(diào)用和延時調(diào)用示例,介紹setInterval方法和clearInterval方法的使用方法,大家參考使用吧2014-01-01

