资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

jQuery实现文章图片弹出放大效果

首先先搭写一个基本的格式:

创新互联是一家专业提供丹凤企业网站建设,专注与成都网站建设、网站制作、H5高端网站建设、小程序制作等业务。10年已为丹凤众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

$.fn.popImg = function() {
  //your code goes here
}

然后用自调用匿名函数包裹你的代码,将系统变量以变量形式传递到插件内部,如下:

;(function($,window,document,undefined){
  $.fn.popImg = function() {
   //your code goes here
  }
})(jQuery,window,document);

那么接下来我们就在里面实现点击文章图片弹出该图片并放大的效果。

整体代码如下:

;(function($,window,document,undefined){
 $.fn.popImg = function(){

   //创建弹出层
   var $layer = $("
").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);

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持创新互联!


当前标题:jQuery实现文章图片弹出放大效果
分享网址:http://cdkjz.cn/article/gejoop.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220