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实现tab键进行选择后enter键触发click行为
Mar 29 jQuery
jquery.validate表单验证插件使用详解
Jun 21 jQuery
深入研究jQuery图片懒加载 lazyload.js使用方法
Aug 16 jQuery
一个有意思的鼠标点击文字特效jquery代码
Sep 23 jQuery
jQuery实现滚动到底部时自动加载更多的方法示例
Feb 18 jQuery
jQuery AJAX 方法success()后台传来的4种数据详解
Aug 08 jQuery
jQuery+Datatables实现表格批量删除功能【推荐】
Oct 24 jQuery
超好用的jQuery分页插件jpaginate用法示例【附源码下载】
Dec 06 jQuery
jquery实现垂直无限轮播的方法分析
Jul 16 jQuery
jQuery实现雪花飘落效果
Aug 02 jQuery
jQuery实现简单全选框
Sep 13 jQuery
jQuery class属性操作addClass()与removeClass()、hasClass()、toggleClass()
Mar 31 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
如何获得PHP相关资料
2006/10/09 PHP
Dedecms常用函数解析
2008/02/01 PHP
php面向对象的方法重载两种版本比较
2008/09/08 PHP
10条PHP编程习惯助你找工作
2008/09/29 PHP
PHP安全防范技巧分享
2011/11/03 PHP
php数组合并array_merge()函数使用注意事项
2014/06/19 PHP
详解PHP中的序列化、反序列化操作
2017/03/21 PHP
一个加密JavaScript的开源工具PACKER2.0.2
2006/11/04 Javascript
JavaScript高级程序设计
2006/12/29 Javascript
JS中的log对象获取以及debug的写法介绍
2014/03/03 Javascript
js获取域名的方法
2015/01/27 Javascript
javascript获取select标签选中的值
2016/06/04 Javascript
javascript另类方法实现htmlencode()与htmldecode()函数实例分析
2016/11/17 Javascript
jQuery Mobile漏洞会有跨站脚本攻击风险
2017/02/12 Javascript
如何通过非数字与字符的方式实现PHP WebShell详解
2017/07/02 Javascript
原生JS实现简单的无缝自动轮播效果
2018/09/26 Javascript
详解如何用webpack4从零开始构建react开发环境
2019/01/27 Javascript
JS实现的自定义map方法示例
2019/05/17 Javascript
javascript设计模式 ? 迭代器模式原理与用法实例分析
2020/04/17 Javascript
浅谈vue 组件中的setInterval方法和window的不同
2020/07/30 Javascript
js实现炫酷光感效果
2020/09/05 Javascript
JS中循环遍历数组的四种方式总结
2021/01/23 Javascript
[02:44]DOTA2英雄基础教程 钢背兽
2013/12/19 DOTA
基python实现多线程网页爬虫
2015/09/06 Python
使用Python脚本和ADB命令实现卸载App
2017/02/10 Python
Python基于回溯法子集树模板实现8皇后问题
2017/09/01 Python
Python 中Django安装和使用教程详解
2019/07/03 Python
vscode 配置 python3开发环境的方法
2019/09/19 Python
python文件绝对路径写法介绍(windows)
2019/12/25 Python
python模块如何查看
2020/06/16 Python
用Python自动清理电脑内重复文件,只要10行代码(自动脚本)
2021/01/09 Python
局域网标准
2016/09/10 面试题
学生出入校管理制度
2014/01/16 职场文书
小学优秀教育工作者事迹材料
2014/05/09 职场文书
观看信仰心得体会
2014/09/04 职场文书
公司致全体员工的感谢信
2019/06/24 职场文书