jQuery實(shí)現(xiàn)文章圖片彈出放大效果
首先先搭寫(xiě)一個(gè)基本的格式:
$.fn.popImg = function() {
//your code goes here
}
然后用自調(diào)用匿名函數(shù)包裹你的代碼,將系統(tǒng)變量以變量形式傳遞到插件內(nèi)部,如下:
;(function($,window,document,undefined){
$.fn.popImg = function() {
//your code goes here
}
})(jQuery,window,document);
那么接下來(lái)我們就在里面實(shí)現(xiàn)點(diǎn)擊文章圖片彈出該圖片并放大的效果。
整體代碼如下:
;(function($,window,document,undefined){
$.fn.popImg = function(){
//創(chuàng)建彈出層
var $layer = $("<div>").css({
position:'fixed',
left:0,
right:0,
top:0,
bottom:0,
width:'100%',
height:'100%',
zIndex:9999999,
display:'none',
background: "#000",
opacity:'0.6'
});
//復(fù)制點(diǎn)擊的圖片,獲得圖片的寬高以及位置
var cloneImg = function($targetImg){
var cloneW = $targetImg.width(),
cloneH = $targetImg.height(),
left = $targetImg.offset().left,
top = $targetImg.offset().top;
return $targetImg.clone().css({
position:'fixed',
width:cloneW,
height:cloneH,
left:left,
top:top,
zIndex:10000000
});
};
//讓復(fù)制的圖片居中顯示
var centerImg = function($cloneImg){
var dW = $(window).width();
var dH = $(window).height();
$cloneImg.css('cursor','zoom-out').attr('clone-bigImg',true);
var img = new Image();
img.onload = function(){
$cloneImg.stop().animate({
width: this.width,
height: this.height,
left: (dW - this.width) / 2,
top: (dH - this.height) / 2
},300);
}
img.src = $cloneImg.attr('src');
};
this.each(function(){
$(this).css('cursor','zoom-in').on('click',function(){
var $body = $("body");
$layer.appendTo($body);
$layer.fadeIn(300);
var $c = cloneImg($(this));
$c.appendTo($body);
centerImg($c);
});
});
var timer = null;
$(window).on("resize", function(){
$("img[clone-bigImg]").each(function(){
var $this = $(this);
timer && clearTimeout(timer);
timer = setTimeout(function(){
centerImg($this);
}, 10);
});
});
$(window).on("click keydown", function(evt){
if(evt.type == "keydown" && evt.keyCode === 27) {
$layer.fadeOut(300);
$("img[clone-bigImg]").remove();
}
var $this = $(evt.target);
if($this.attr("clone-bigImg")){
$layer.fadeOut(300);
$("img[clone-bigImg]").remove();
}
});
}
})(jQuery,window,document);
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- jQuery實(shí)現(xiàn)的鼠標(biāo)滑過(guò)彈出放大圖片特效
- jQuery插件zoom實(shí)現(xiàn)圖片全屏放大彈出層特效
- jQuery實(shí)現(xiàn)等比例縮放大圖片讓大圖片自適應(yīng)頁(yè)面布局
- 用JQuery模仿淘寶的圖片放大鏡顯示效果
- 基于jquery實(shí)現(xiàn)一張圖片點(diǎn)擊鼠標(biāo)放大再點(diǎn)縮小
- 基于Jquery插件開(kāi)發(fā)之圖片放大鏡效果(仿淘寶)
- jQuery當(dāng)鼠標(biāo)懸停時(shí)放大圖片的效果實(shí)例
- jquery圖片放大鏡功能的實(shí)例代碼
相關(guān)文章
JQuery中關(guān)于jquery.js與jquery.min.js的比較探討
jquery-1.4.2.min.js是優(yōu)化的,而query-1.4.2.js是易于開(kāi)發(fā)著閱讀的,具體詳解祥看本文,希望對(duì)你有所幫助2013-05-05
jQuery幫助之篩選查找 children([expr])
取得一個(gè)包含匹配的元素集合中每一個(gè)元素的所有子元素的元素集合。2011-01-01
jQuery動(dòng)畫(huà)animate方法使用介紹
基于jQuery實(shí)現(xiàn)滾動(dòng)切換效果
jquery實(shí)現(xiàn)滑動(dòng)圖片自己測(cè)試的例子

