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实现鼠标经过显示动画边框特效
Mar 24 jQuery
jQuery表单验证之密码确认
May 22 jQuery
jQuery选择器之属性筛选选择器用法详解
Sep 19 jQuery
javaScript和jQuery自动加载简单代码实现方法
Nov 24 jQuery
jQuery实现输入框的放大和缩小功能示例
Jul 21 jQuery
jquery层次选择器的介绍
Jan 18 jQuery
jQuery ajax仿Google自动提示SearchSuggess功能示例
Mar 28 jQuery
jquery实现垂直无限轮播的方法分析
Jul 16 jQuery
jquery实现商品sku多属性选择功能(商品详情页)
Dec 20 jQuery
jQuery操作动画完整实例分析
Jan 10 jQuery
jQuery实现简单QQ聊天框
Aug 27 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
Win2003下APACHE+PHP5+MYSQL4+PHPMYADMIN 的简易安装配置
2006/11/18 PHP
PHP用SAX解析XML的实现代码与问题分析
2011/08/22 PHP
php快递单号查询接口使用示例
2014/05/05 PHP
ThinkPHP框架安全实现分析
2016/03/14 PHP
[企业公众号]升级到[企业微信]之后发送消息失败的解决方法
2017/06/30 PHP
网上抓的一个特效
2007/05/11 Javascript
JS中的substring和substr函数的区别说明
2013/05/07 Javascript
浅谈Web页面向后台提交数据的方式和选择
2016/09/23 Javascript
JS中append字符串包含onclick无效传递参数失败的解决方案
2016/12/26 Javascript
微信小程序实现聊天对话(文本、图片)功能
2018/07/06 Javascript
详解Bootstrap 学习(一)入门
2019/04/12 Javascript
JavaScript数值类型知识汇总
2019/11/17 Javascript
Vue将props值实时传递 并可修改的操作
2020/08/09 Javascript
Windows下实现Python2和Python3两个版共存的方法
2015/06/12 Python
python如何获取服务器硬件信息
2017/05/11 Python
Python调用系统底层API播放wav文件的方法
2017/08/11 Python
Python基于更相减损术实现求解最大公约数的方法
2018/04/04 Python
解决python tkinter界面卡死的问题
2019/07/17 Python
django云端留言板实例详解
2019/07/22 Python
手机使用python操作图片文件(pydroid3)过程详解
2019/09/25 Python
PyTorch使用cpu加载模型运算方式
2020/01/13 Python
python异常处理、自定义异常、断言原理与用法分析
2020/03/23 Python
Python调用接口合并Excel表代码实例
2020/03/31 Python
从一次项目重构说起CSS3自定义变量在项目的使用方法
2021/03/01 HTML / CSS
Html5之自定义属性(data-,dataset)
2019/11/19 HTML / CSS
大学应届毕业生个人求职信
2013/09/23 职场文书
电子专业推荐信范文
2013/11/18 职场文书
公司成立感言
2014/01/11 职场文书
2014年最新学习全国两会精神心得
2014/03/17 职场文书
无毒社区工作方案
2014/05/23 职场文书
党的群众路线教育实践活动组织生活会发言材料
2014/10/17 职场文书
三提三创主题教育活动查摆整改措施
2014/10/25 职场文书
优秀党员推荐材料
2014/12/18 职场文书
2015中秋节晚会主持词
2015/07/01 职场文书
宿舍管理制度范本
2015/08/07 职场文书
python中的random模块和相关函数详解
2022/04/22 Python