首先先搭寫一個基本的格式:

創(chuàng)新互聯(lián)是一家專業(yè)提供丹鳳企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、網(wǎng)站制作、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為丹鳳眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計公司優(yōu)惠進(jìn)行中。
$.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);那么接下來我們就在里面實(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);
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持創(chuàng)新互聯(lián)!
新聞名稱:jQuery實(shí)現(xiàn)文章圖片彈出放大效果
轉(zhuǎn)載源于:http://chinadenli.net/article8/gejoop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、Google、商城網(wǎng)站、網(wǎng)站營銷、網(wǎng)站內(nèi)鏈、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)