使用jquery實(shí)現(xiàn)放大鏡效果
實(shí)現(xiàn)原理
首先,我們講解一下放大鏡效果的實(shí)現(xiàn)方式:
方法一:準(zhǔn)備一張高像素的大圖,當(dāng)鼠標(biāo)放到原圖上,加載顯示大圖的對應(yīng)位置。
方法二:對原圖片進(jìn)行放大,也就是調(diào)整原圖的長和寬。
上面我們介紹了通過兩種方式實(shí)現(xiàn)放大鏡效果,接下來,我們將以上的兩種方式應(yīng)用到我們的jQuery插件中。
首先,我們需要一個img元素顯示原圖對象,還需要一個容器作為顯示框;顯示框里面存放大圖對象。當(dāng)鼠標(biāo)移動到原圖上時,通過對大圖進(jìn)行絕對定位來顯示對應(yīng)的部位,實(shí)現(xiàn)類似放大鏡的效果。
接下來,讓我們定義Index.html頁面,具體實(shí)現(xiàn)如下:
<!DOCTYPE html>
<html>
<head>
<title>放大鏡效果</title>
<meta charset="utf-8"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<link type="text/css" rel="stylesheet" href="css/reset.css"/>
<link type="text/css" rel="stylesheet" href="css/main.css"/>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/jquery.imageZoom.js"></script>
</head>
<body>
<div class="magnify">
<div class="large"></div>
<img class="small" src="images/iphone.jpg" width="200" />
</div>
<div class="magnify_02">
<div class="large_02"></div>
<img class="small_02" src="images/img5.jpg" width="400"/>
</div>
<script type="text/javascript">
$(function(){
$(".magnify").hover(function(){
$.fn.imageZoom({
small :"small",
large : "large",
magnify: "magnify"
});
},function(){})
$(".magnify_02").hover(function(){
$.fn.imageZoom({
small : "small_02",
large : "large_02",
magnify: "magnify_02"
});
},function(){})
})
</script>
</body>
</html>
css樣式:
.magnify {width: 200px; margin: 50px auto; position: relative;}
.large {width: 175px; height: 175px;position: absolute;border-radius: 100%;z-index:99;box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25);background: url('../images/iphone.jpg') no-repeat;display: none;}
.small { display: block; }
.magnify_02 {width: 400px; margin: 50px auto; position: relative;}
.large_02 {width: 175px; height: 175px;position: absolute;border-radius: 100%;z-index:99;box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25);background: url('../images/iphone.jpg') no-repeat;display: none;}
.small_02 { display: block; }
mousemove事件
接下來,我們通過jQuery插件形式來實(shí)現(xiàn)放大鏡效果,當(dāng)鼠標(biāo)移動到small對象上方時,就會在large對象中顯示大圖的對應(yīng)位置,這就涉及到mousemove事件了,所以,我們需要實(shí)現(xiàn)mousemove事件的監(jiān)聽方法。
實(shí)現(xiàn)jquery.imagezoom.js插件:
(function($) {
$.fn.imageZoom = function(options) {
var defaults = {
scaling: 0.3,
small :"small",
large : "large",
magnify:"magnify"
};
options = $.extend(defaults, options),
native_width = 0,
native_height = 0,
current_width = 0,
current_height = 0,
magnify="."+options.magnify;
small="."+options.small;
$small=$(small);
large="."+options.large;
$large=$(large);
$(magnify).mousemove(function(e) {
var image_object = new Image();
image_object.src = $small.attr('src');
if(!+[1,]) {
native_height = image_object.height;
native_width = image_object.width;
}
else {
image_object.onload = function() {
image_object.onload = null;
native_height = image_object.height;
native_width = image_object.width;
}
}
current_height = $small.height();
current_width = $small.width();
var magnify_offset = $(this).offset();
var mx = e.pageX - magnify_offset.left;
var my = e.pageY - magnify_offset.top;
if (mx < $(this).width() && my <$(this).height() && mx > 0 && my > 0) {
$large.fadeIn(100);
} else {
$large.fadeOut(100);
}
if ($large.is(":visible")) {
var rx = Math.round(mx / $small.width() * native_width - $large.width() / 2) * -1,
ry = Math.round(my / $small.height() * native_height - $large.height() / 2) * -1,
bgp = rx + "px " + ry + "px",
px = mx - $large.width() / 2,
py = my - $large.height() / 2;
$large.css({
left: px,
top: py,
backgroundPosition: bgp
});
}
//}
});
};
})(jQuery);
注釋:當(dāng)鼠標(biāo)移動到magnify對象中,我們需要獲取鼠標(biāo)在magnify中的相對坐標(biāo)位置,這里我們把相對坐標(biāo)定義為(mx,my),通過上圖我們知道相對坐標(biāo)等于(pageX - offsetLeft, pageY - offsetTop)。
現(xiàn)在,我們已經(jīng)獲取鼠標(biāo)在magnify對象中的坐標(biāo)值,接下來,需要獲取對應(yīng)大圖的相應(yīng)坐標(biāo),這里我們把大圖的對應(yīng)坐標(biāo)定義為(rx,ry),我們可以通過比例關(guān)系獲取(rx,ry)的值。
mx / small.width (原圖的寬)= rx / native_width(大圖的寬)
my / small.height (原圖的長)= ry / native_height(大圖的長)
通過上面的比例關(guān)系,我們知道大圖的坐標(biāo)(rx,ry)等于(mx/small.widthnative_width, my/small.heightnative_height)。
mousewheel事件
前面,我們通過mousemove事件來放大圖片,這里我們將通過鼠標(biāo)的滾輪事件實(shí)現(xiàn)圖片放大效果。
由于,不同的瀏覽器有不同的滾輪事件。主要是有三種:onmousewheel(IE 6/7/8)、mousewheel(IE9,Chrome,Safari和Opera)和DOMMouseScroll(只有Firefox支持),關(guān)于這三個事件這里不做詳細(xì)的介紹了。
由于不同瀏覽器之間存在著差異,為了實(shí)現(xiàn)瀏覽器之間的兼容,所以,我們需要監(jiān)聽以上三種滾輪事件(onmousewheel,mousewheel和DOMMouseScroll),具體實(shí)現(xiàn)如下:
$(".magnify").bind('DOMMouseScroll mousewheel onmousewheel', function(e) {
});
上面,我們實(shí)現(xiàn)了兼容不同瀏覽器的滾輪事件監(jiān)聽方法,接下來,判斷滾輪向上或向下也要考慮不同瀏覽器的兼容性,主流的覽器(IE、Opera、Safari、Firefox、Chrome)中Firefox 使用detail,其余四類使用wheelDelta;兩者只在取值上不一致,代表含義一致,detail與wheelDelta只各取兩個值,detail只取±3,wheelDelta只取±120,其中正數(shù)表示為向上,負(fù)數(shù)表示向下。
由于detail和wheelDelta都有兩個值表示向上或向下滾動,所以不同瀏覽器間可以通過以下方式實(shí)現(xiàn)兼容,具體實(shí)現(xiàn)如下:
$(".magnify").bind('DOMMouseScroll mousewheel onmousewheel', function(e) {
// cross-browser wheel delta
var e = window.event || e; // old IE support.
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
});
上面,我們已經(jīng)處理了不同瀏覽器滾輪監(jiān)聽方法,當(dāng)用戶滾動滾輪時需要動態(tài)地修改原圖的尺寸,這里我們定義縮放比scaling為0.3,也就是說每當(dāng)用戶滾動一下滾輪原圖就按0.3的比例進(jìn)行縮放,具體實(shí)現(xiàn)如下:
// Gets the image scaling height and width.
native_height += (native_height * scaling * delta);
native_width += (native_width * scaling * delta);
// Update backgroud image size.
$large.css('background-size', native_width + "px " + native_height + "px");
上面,我們實(shí)現(xiàn)了放大鏡效果,當(dāng)我們鼠標(biāo)停留在圖片上方會自動放大圖片的相應(yīng)部位,當(dāng)然我們可以通過滾輪調(diào)整放大的比例。
參考
http://tech.pro/tutorial/681/css-tutorial-the-background-position-property http://www.sitepoint.com/html5-javascript-mouse-wheel/ http://thecodeplayer.com/walkthrough/magnifying-glass-for-images-using-jquery-and-css3
相關(guān)文章
輕松掌握jQuery中wrap()與unwrap()函數(shù)的用法
wrap()能夠?qū)⒅付℉TML元素包裹DOM結(jié)構(gòu),與之相反unwrap()函數(shù)則是將DOM去掉^^下面讓我們來以兩個小例子輕松掌握jQuery中wrap()與unwrap()函數(shù)的用法:)2016-05-05
jQuery+ThinkPHP+Ajax實(shí)現(xiàn)即時消息提醒功能實(shí)例代碼
這篇文章主要介紹了jQuery+ThinkPHP+Ajax實(shí)現(xiàn)即時消息提醒功能的實(shí)例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-03-03
jquery實(shí)現(xiàn)動態(tài)改變div寬度和高度
這篇文章主要介紹了jquery實(shí)現(xiàn)動態(tài)改變div寬度和高度,效果非常不錯,而且兼容性也很好,有需要的小伙伴可以參考下。2015-05-05
文本溢出插件jquery.dotdotdot.js使用方法詳解
這篇文章主要介紹了文本溢出插件jquery.dotdotdot.js使用方法詳解,需要的朋友可以參考下2017-06-06
基于jquery實(shí)現(xiàn)最簡單的選項(xiàng)卡切換效果
這篇文章主要介紹了基于jquery實(shí)現(xiàn)最簡單的選項(xiàng)卡切換效果的相關(guān)資料,具有一定的參考價值,感興趣的朋友可以參考一下2016-05-05
Jquery焦點(diǎn)與失去焦點(diǎn)示例應(yīng)用
這篇文章主要介紹了Jquery焦點(diǎn)與失去焦點(diǎn)示例應(yīng)用,需要的朋友可以參考下2014-06-06

