實現(xiàn)jquery放大鏡的兩種方法
更新時間:2018年02月22日 14:10:17 作者:web_emmet
本篇文章給大家詳細(xì)分析了用jquery實現(xiàn)放大鏡效果的方法,以及代碼分享,有興趣的學(xué)習(xí)下。
jquery寫的兩種放大鏡效果,沒有使用到插件。調(diào)理和思路清晰。不是使用面向?qū)ο蠓绞綄懙?,初學(xué)者較容易看懂。廢話不多說,看代碼。圖片這里就不上傳了,大家自己找下。最好是找到比例的,這樣效果比較好。
<body>
<div id="father">
<div id="container">
<img src="img/400_1.jpg" style="display: block;">
<img src="img/400_2.jpg" >
<div class="shade"></div>
</div>
<div class="small first"><img src="img/50_1.jpg"></div>
<div class="small second"><img src="img/50_2.jpg"></div>
</div>
<div class="big">
<img src="img/800_1.jpg" style="display: block;">
<img src="img/800_2.jpg">
</div>
</body>
css代碼
*{padding: 0; margin: 0;}
#father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;}
#father .second{left: 70px;}
.third{left: 140px;}
#father{position: relative; top: 100px; left: 50px; height: 460px;}
#container{position: absolute; width: 400px; height: 400px;}
#container img{position: absolute; display: none;}
.shade{width: 200px; height: 200px; position: absolute; background: #000; opacity: 0.4; top: 0;
left: 0; display: none;}
.big{width: 400px; height: 400px; position: absolute; top: 100px; overflow: hidden; left: 500px; display: none;}
.big img{width: 800px; height: 800px; position: absolute; display: none;}
js代碼
<script type="text/javascript" src='js/jquery-1.12.4.min.js'></script>
<script type="text/javascript">
$(function () {
changePic('.first',0);
changePic('.second',1);
var shadeWidth = $('.shade').width(),//陰影的寬度
shadeHeight = $('.shade').height(),//陰影的高度
middleWidth = $('#container').width(),//容器的寬度
middleHeight = $('#container').height(),//容器的高度
bigWidth = $('.big').width(),//放大圖片盒子的寬度
bigHeight = $('.big').height(),//放大圖片盒子的高度
rateX = bigWidth / shadeWidth,//放大區(qū)和遮罩層的寬度比例
rateY = bigHeight / shadeHeight;//放大區(qū)和遮罩層的高度比例
//當(dāng)鼠標(biāo)移入與移出時陰影與放大去顯現(xiàn)/消失
$('#container').hover(function() {
$('.shade').show();
$('.big').show();
}, function() {
$('.shade').hide();
$('.big').hide();
}).mousemove(function(e) {//當(dāng)鼠標(biāo)移動時,陰影和放大區(qū)圖片進(jìn)行移動
//記錄下光標(biāo)距離頁面的距離
var x = e.pageX,
y = e.pageY;
//設(shè)置遮罩層的位置
$('.shade').offset({
top: y-shadeHeight/2,
left: x-shadeWidth/2
});
//獲取遮罩層相對父元素的位置
var cur = $('.shade').position(),
_top = cur.top,
_left = cur.left,
hdiffer = middleHeight - shadeHeight,
wdiffer = middleWidth - shadeWidth;
if (_top < 0) _top = 0;
else if (_top > hdiffer) _top = hdiffer;
if (_left < 0) _left = 0;
else if (_left > wdiffer) _left =wdiffer;
//判斷完成后設(shè)置遮罩層的范圍
$('.shade').css({
top: _top,
left: _left
});
//設(shè)置放大區(qū)圖片移動
$('.big img').css({
top: - rateY*_top,
left: - rateX*_left
});
});;
//封裝的改變圖片顯示的函數(shù)
function changePic (element,index) {
$(element).click(function() {
$('#container img').eq(index).css('display', 'block').siblings().css('display', 'none');
$('.big img').eq(index).css('display', 'block').siblings().css('display', 'none');
});
}
});
以上是常用的,下面這個是在原圖基礎(chǔ)上放大的
htm
<body>
<div id="father">
<div id="container">
<img src="img/400_1.jpg" style="display: block;">
<img src="img/400_2.jpg" >
<img src="img/400_3.jpg" >
<div class="shade">
<img src="img/800_1.jpg" style="display: block;">
<img src="img/800_2.jpg">
<img src="img/800_3.jpg">
</div>
</div>
<div class="small first"><img src="img/50_1.jpg"></div>
<div class="small second"><img src="img/50_2.jpg"></div>
<div class="small third"><img src="img/50_3.jpg"></div>
</div>
</body>
css代碼
*{padding: 0; margin: 0;}
#father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;}
#father .second{left: 70px;}
.third{left: 140px;}
#father{position: relative; top: 100px; left: 50px; height: 460px;}
#container{position: absolute; width: 400px; height: 400px;}
#container img{position: absolute; display: none;}
.shade{width: 200px; height: 200px; position: absolute; top: 0;left: 0; display: none; border-radius: 50%; overflow: hidden; background: #000;}
.shade img{display: none; width: 800px; height: 800px; position: absolute;}
js代碼
<span style="white-space:pre"> </span><script type="text/javascript" src='js/jquery-1.12.4.min.js'></script>
<script type="text/javascript">
$(function () {
changePic('.first',0);
changePic('.second',1);
changePic('.third',2);
var shadeWidth = $('.shade').width(),//陰影的寬度
shadeHeight = $('.shade').height(),//陰影的高度
middleWidth = $('#container').width(),//容器的寬度
middleHeight = $('#container').height(),//容器的高度
bigImgWidth = $('.shade img').width(),//放大圖片盒子的寬度
bigImgHeight = $('.shade img').height(),//放大圖片盒子的高度
rateX = bigImgWidth / middleWidth,//放大區(qū)和遮罩層的寬度比例2
rateY = bigImgHeight / middleHeight;//放大區(qū)和遮罩層的高度比例2
//當(dāng)鼠標(biāo)移入與移出時陰影與放大去顯現(xiàn)/消失
$('#container').hover(function() {
$('.shade').show();
$('.big').show();
}, function() {
$('.shade').hide();
$('.big').hide();
}).mousemove(function(e) {//當(dāng)鼠標(biāo)移動時,陰影和放大區(qū)圖片進(jìn)行移動
//記錄下光標(biāo)距離頁面的距離
var x = e.pageX,
y = e.pageY;
//設(shè)置遮罩層的位置
$('.shade').offset({
top: y-shadeHeight/2,
left: x-shadeWidth/2
});
//獲取遮罩層相對父元素的位置
var cur = $('.shade').position(),
_top = cur.top,
_left = cur.left,
hdiffer = middleHeight - shadeHeight,
wdiffer = middleWidth - shadeWidth;
if (_top < 0) _top = 0;
else if (_top > hdiffer) _top = hdiffer;
if (_left < 0) _left = 0;
else if (_left > wdiffer) _left =wdiffer;
//判斷完成后設(shè)置遮罩層的范圍
$('.shade').css({
top: _top,
left: _left
});
//設(shè)置放大區(qū)圖片移動
$('.shade img').css({
top: - _top*rateY*3/2,
left: - _left*rateX*3/2
});
});;
//封裝的改變圖片顯示的函數(shù)
function changePic (element,index) {
$(element).click(function() {
$('#container img').eq(index).css('display', 'block').siblings().css('display', 'none');
$('.shade img').eq(index).css('display', 'block').siblings().css('display', 'none');
});
}
});
<span style="white-space:pre"> </span></script>
您可能感興趣的文章:
- jquery圖片放大鏡效果
- jquery實現(xiàn)放大鏡簡潔代碼(推薦)
- jQuery實現(xiàn)的放大鏡效果示例
- 基于jquery編寫的放大鏡插件
- jQuery實現(xiàn)圖片局部放大鏡效果
- jQuery實現(xiàn)放大鏡效果實例代碼
- 基于jQuery仿淘寶產(chǎn)品圖片放大鏡特效
- 基于jQuery實現(xiàn)放大鏡特效
- jquery實現(xiàn)圖片放大鏡功能
- 基于jQuery仿淘寶產(chǎn)品圖片放大鏡代碼分享
- 基于jquery實現(xiàn)放大鏡效果
- 使用jquery實現(xiàn)放大鏡效果
- jquery放大鏡效果超漂亮噢
- jquery圖片放大鏡功能的實例代碼
- 基于jquery的放大鏡效果
相關(guān)文章
jquery Deferred 快速解決異步回調(diào)的問題
下面小編就為大家?guī)硪黄猨query Deferred 快速解決異步回調(diào)的問題。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-04-04
jQuery插件imgPreviewQs實現(xiàn)上傳圖片預(yù)覽
這篇文章主要介紹了jQuery插件imgPreviewQs實現(xiàn)上傳圖片預(yù)覽的相關(guān)資料,需要的朋友可以參考下2016-01-01
jQuery加PHP實現(xiàn)圖片上傳并提交的示例代碼
這篇文章主要介紹了jQuery加PHP實現(xiàn)圖片上傳并提交的實例,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07

