首先先搭寫一個基本的格式:
創(chuàng)新互聯(lián)是一家專業(yè)提供丹鳳企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站建設、網(wǎng)站制作、H5高端網(wǎng)站建設、小程序制作等業(yè)務。10年已為丹鳳眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設計公司優(yōu)惠進行中。
$.fn.popImg = function() { //your code goes here }
然后用自調用匿名函數(shù)包裹你的代碼,將系統(tǒng)變量以變量形式傳遞到插件內部,如下:
;(function($,window,document,undefined){ $.fn.popImg = function() { //your code goes here } })(jQuery,window,document);
那么接下來我們就在里面實現(xià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' }); //復制點擊的圖片,獲得圖片的寬高以及位置 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 }); }; //讓復制的圖片居中顯示 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);
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持創(chuàng)新互聯(lián)!
新聞名稱:jQuery實現(xiàn)文章圖片彈出放大效果
轉載源于:http://bm7419.com/article8/gejoop.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、Google、商城網(wǎng)站、網(wǎng)站營銷、網(wǎng)站內鏈、外貿網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)