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


Posted in jQuery onApril 06, 2017

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

$.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 = $("<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);

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

jQuery 相关文章推荐
jQuery实现的动态文字变化输出效果示例【附演示与demo源码下载】
Mar 24 jQuery
如何编写jquery插件
Mar 29 jQuery
文本溢出插件jquery.dotdotdot.js使用方法详解
Jun 22 jQuery
jQuery实现倒计时功能 jQuery实现计时器功能
Sep 19 jQuery
jQuery序列化form表单数据为JSON对象的实现方法
Sep 20 jQuery
jQuery实现动态添加和删除input框实例代码
Mar 26 jQuery
基于 jQuery 实现键盘事件监听控件
Apr 04 jQuery
jquery 验证用户名是否重复代码实例
May 14 jQuery
jQuery擦除插件eraser使用方法详解
Jan 11 jQuery
jQuery实现视频展示效果
May 30 jQuery
jQuery实现简单QQ聊天框
Aug 27 jQuery
jquery插件懒加载的示例
Oct 24 jQuery
jQuery自定义图片上传插件实例代码
Apr 04 #jQuery
jQuery使用unlock.js插件实现滑动解锁
Apr 04 #jQuery
利用jquery正则表达式在页面验证url网址输入是否正确
Apr 04 #jQuery
jQuery中animate()的使用方法及解决$(”body“).animate({“scrollTop”:top})不被Firefox支持的问题
Apr 04 #jQuery
使用jQuery卸载全部事件的思路详解
Apr 03 #jQuery
jQuery实现分页功能(含ajax请求、后台数据、附完整demo)
Apr 03 #jQuery
基于JQuery和原生JavaScript实现网页定位导航特效
Apr 03 #jQuery
You might like
弄了个检测传输的参数是否为数字的Function
2006/12/06 PHP
php pcntl_fork和pcntl_fork 的用法
2009/04/13 PHP
php 网页游戏开发入门教程一(webgame+design)
2009/10/26 PHP
PHP性能优化准备篇图解PEAR安装
2011/12/05 PHP
PHP5中Cookie与 Session使用详解
2013/04/30 PHP
深入PHP数据缓存的使用说明
2013/05/10 PHP
解析PHP中ob_start()函数的用法
2013/06/24 PHP
PHP中构造函数和析构函数解析
2014/10/10 PHP
tp5框架的增删改查操作示例
2019/10/31 PHP
YUI 读码日记之 YAHOO.lang.is*
2008/03/22 Javascript
ExtJS的FieldSet的column列布局
2009/11/20 Javascript
日历查询的算法 如何计算某一天是星期几
2012/12/12 Javascript
JavaScript动态创建div属性和样式示例代码
2013/10/09 Javascript
js如何设置在iframe框架中指定div不显示
2013/12/04 Javascript
JS动态添加与删除select中的Option对象(示例代码)
2013/12/25 Javascript
Javascript获取表单名称(name)的方法
2015/04/02 Javascript
轻松使用jQuery双向select控件Bootstrap Dual Listbox
2015/12/13 Javascript
浅谈javascript中的数据类型转换
2016/12/27 Javascript
jQuery Validate格式验证功能实例代码(包括重名验证)
2017/07/18 jQuery
JavaScript面试出现频繁的一些易错点整理
2018/03/29 Javascript
使用gulp构建前端自动化的方法示例
2018/12/25 Javascript
layer的prompt弹出框,点击回车,触发确定事件的方法
2019/09/06 Javascript
整理 node-sass 安装失败的原因及解决办法(小结)
2020/02/19 Javascript
uni-app从安装到卸载的入门教程
2020/05/15 Javascript
python 实现红包随机生成算法的简单实例
2017/01/04 Python
tensorflow入门:TFRecordDataset变长数据的batch读取详解
2020/01/20 Python
马来西亚与新加坡长途巴士售票网站:BusOnlineTicket.com
2018/11/05 全球购物
Kendra Scott官网:美国领先的时尚配饰品牌
2020/10/22 全球购物
大学毕业生求职自荐信
2014/02/20 职场文书
男女朋友协议书
2014/04/23 职场文书
老兵退伍标语
2014/10/07 职场文书
初中语文教学随笔
2015/08/15 职场文书
详解JS ES6编码规范
2021/05/07 Javascript
详解PHP服务器如何在有限的资源里最大提升并发能力
2021/05/25 PHP
java泛型通配符详解
2021/07/25 Java/Android
微信小程序中wxs文件的一些妙用分享
2022/02/18 Javascript