jQuery点击图片弹出放大可拖动图片查看

时间:2021-09-18 14:08:44

CSS代码:

 .popup-bigic {
position: absolute;
left:;
top:;
background: #eee;
overflow: hidden;
z-index:;
}
.popup-bigic .loading-bigic {
position: absolute;
left: 50%;
top: 50%;
width: 24px;
height: 24px;
margin-left: -12px;
margin-top: -12px;
}
.popup-bigic .img-bigic {
position: absolute;
}
.option-bigic {
position: absolute;
right: 20px;
top: 20px;
z-index:;
}
.option-bigic span {
display: inline-block;
width: 40px;
height: 40px;
margin-right: 20px;
text-indent: -999px;
overflow: hidden;
cursor: pointer;
border-radius: 5px;
background-image: url(../images/icons.png);
background-repeat: no-repeat;
background-color: #fff;
opacity: .5;
}
.option-bigic span:hover {
opacity:;
}
.option-bigic span.change-bigic {
display: none;
background-position: -52px 3px;
}
.option-bigic span.max-bigic {
display: none;
background-position: -119px 3px;
}
.option-bigic span.close-bigic {
background-position: 8px 8px;
}

JS代码:

 /**
* jQuery Plugin bigic v1.0.0
/*
*/
(function ($) {
$.fn.bigic = function () { /*
* 构造函数 @Bigic
* 定义基础变量,初始化对象事件
*/
function Bigic($obj){
this.$win = $(window);
this.$obj = $obj;
this.$popup,
this.$img,
this.nWinWid = 0;
this.nWinHei = 0;
this.nImgWid = 0;
this.nImgHei = 0;
this.nImgRate = 0;
this.sImgStatus;
this.sImgSrc,
this.bMoveX = true,
this.bMoveY = true; this.init();
} /*
* 初始化 绑定基础事件
*/
Bigic.prototype.init = function(){
var oThis = this,
timer = null; // 为图片绑定点击事件
this.$obj.off('.bigic').on('click.bigic', function(){
var sTagName = this.tagName.toLowerCase();
if(sTagName == 'img'){
// 更新基础变量
oThis.sImgSrc = this.getAttribute('src');
oThis.sImgStatus = 'min';
// 显示弹窗
oThis.show();
}else{
alert('非IMG标签');
}
}); // 浏览器缩放
this.$win.off('.bigic').on('resize.bigic', function(){
clearTimeout(timer);
timer = setTimeout(function(){
oThis.zoom();
}, 30);
});
} /*
* 弹窗初始化
*/
Bigic.prototype.show = function(){
var oThis = this,
oImg = new Image(); oThis.popup(); // 显示弹窗 // 图片加载
oImg.onload = function(){
oThis.nImgWid = this.width;
oThis.nImgHei = this.height;
oThis.nImgRate = oThis.nImgWid/oThis.nImgHei; $('#LoadingBigic').remove();
oThis.$popup.append('<img id="imgBigic" class="img-bigic" src="'+ oThis.sImgSrc +'" />');
oThis.$img = $('#imgBigic'); oThis.zoom();
}
oImg.src = oThis.sImgSrc;
} /*
* 弹窗显示 及相关控件事件绑定
*/
Bigic.prototype.popup = function(){
var sHtml = '',
oThis = this;
// 生成HTML 选中DOM节点
sHtml += '<div id="popupBigic" class="popup-bigic" style="width:'+ this.nWinWid +'px;height:'+ this.nWinHei +'px;">'
+ '<div class="option-bigic">'
+ '<span id="changeBigic" class="change-bigic min-bigic" state-bigic="min">放大</span>'
+ '<span id="closeBigic" class="close-bigic">关闭</span>'
+ '</div>'
+ '<img id="LoadingBigic" class="loading-bigic" src="preloader.gif" />'
+ '</div>';
$('body').append(sHtml);
oThis.$popup = $('#popupBigic'); // 事件绑定 - 关闭弹窗
$('#closeBigic').off().on('click',function(){
oThis.$popup.remove();
}); // 事件绑定 - 切换尺寸
$('#changeBigic').off().on('click',function(){
if(!document.getElementById('imgBigic')) return;
if($(this).hasClass('min-bigic')){
oThis.sImgStatus = 'max';
$(this).removeClass('min-bigic').addClass('max-bigic').html('缩小');
}else{
oThis.sImgStatus = 'min';
$(this).removeClass('max-bigic').addClass('min-bigic').html('放大');;
}
oThis.zoom();
});
} /*
* 图片放大缩小控制函数
*/
Bigic.prototype.zoom = function(){
var nWid = 0,cnHei = 0,
nLeft = 0, nTop = 0,
nMal = 0, nMat = 0; // 弹窗未打开 或 非img 返回
if(!document.getElementById('popupBigic') || !this.nImgWid) return; this.nWinWid = this.$win.width();
this.nWinHei = this.$win.height();
this.bMoveX = true;
this.bMoveY = true; // 显示隐藏放大缩小按钮
if(this.nImgWid > this.nWinWid || this.nImgHei > this.nWinHei){
$('#changeBigic')[0].style.display = 'inline-block';
}else{
$('#changeBigic')[0].style.display = 'none';
} if(this.sImgStatus == 'min'){
nWid = this.nImgWid > this.nWinWid ? this.nWinWid : this.nImgWid;
nHei = nWid / this.nImgRate; if(nHei > this.nWinHei) nHei = this.nWinHei;
nWid = nHei*this.nImgRate; this.$img.css({'width': nWid +'px', 'height': nHei +'px', 'left': '50%', 'top': '50%', 'margin-top': -nHei/2+'px', 'margin-left': -nWid/2+'px'});
this.$popup.css({'width': this.nWinWid +'px', 'height': this.nWinHei+'px'});
this.move(false);
}else{
if(this.nImgWid < this.nWinWid){
nLeft = '50%'
nMal = this.nImgWid / 2;
this.bMoveX = false;
}
if(this.nImgHei < this.nWinHei){
nTop = '50%'
nMat = this.nImgHei / 2;
this.bMoveY = false;
}
this.$img.css({'width': this.nImgWid +'px', 'height': this.nImgHei+'px', 'left': nLeft, 'top': nTop, 'margin-top': -nMat +'px', 'margin-left': -nMal +'px'});
this.$popup.css({'width': this.nWinWid +'px', 'height': this.nWinHei+'px'});
this.move(true);
}
} /*
* 图片移动事件
*/
Bigic.prototype.move = function(bln){
var _x, _y, _winW, _winH,
_move = false,
_boxW = this.nImgWid,
_boxH = this.nImgHei,
oThis = this; if(!oThis.$img) return;
// 解除绑定
if(!bln){
oThis.$img.off('.bigic');
$(document).off('.bigic');
return;
} // 弹窗移动
oThis.$img.off('.bigic').on({
'click.bigic': function(e){
e.preventDefault();
},
'mousedown.bigic': function(e){
e.preventDefault();
_move=true;
_x=e.pageX-parseInt(oThis.$img.css("left"));
_y=e.pageY-parseInt(oThis.$img.css("top"));
_winW = oThis.nWinWid;
_winH = oThis.nWinHei;
oThis.$img.css('cursor','move');
}
});
$(document).off('.bigic').on({
'mousemove.bigic': function(e){
e.preventDefault();
if(_move){
var x=e.pageX-_x;
var y=e.pageY-_y;
if(x > 0) x = 0;
if(y > 0) y = 0;
if(_winW && x < _winW-_boxW) x = _winW - _boxW;
if(_winH && y < _winH-_boxH) y = _winH - _boxH;
if(oThis.bMoveX) oThis.$img[0].style.left = x +'px';
if(oThis.bMoveY) oThis.$img[0].style.top = y +'px';
}
},
'mouseup.bigic': function(){
_move=false;
oThis.$img.css('cursor','default');
}
});
} /*
* 实例化
*/
new Bigic($(this));
};
})(jQuery);
 <script src="http://code.jquery.com/jquery-2.2.3.min.js"></script>

HTML代码:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery实现点击小图弹出大图 </title> <link href="css/style.css" rel="stylesheet" type="text/css"/> <style>
body {
margin: 0;
padding: 0;
overflow: hidden;
}
body img {
width: 300px;
height: 200px;
}
</style> </head>
<body> <div style="width:960px; margin:0 auto">
<h1>jQuery bigic Plugin Demo</h1>
<img class="test" src="photo/1.jpg" alt="">
<img class="test" src="photo/2.jpg" alt="">
<img class="test" src="photo/3.jpg" alt="">
</div> <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/jquey-bigic.js"></script>
<script type="text/javascript">
$(function(){
$('img').bigic();
});
</script> </body>
</html>

案例下载:Demo

jQuery点击图片弹出放大可拖动图片查看的更多相关文章

  1. jQuery效果之封装一个文章图片弹出放大效果

    首先先搭写一个基本的格式: $.fn.popImg = function() { //your code goes here } 然后用自调用匿名函数包裹你的代码,将系统变量以变量形式传递到插件内部, ...

  2. jquery点击按钮弹出图片

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. jQuery点击图片弹出放大特效下载

    效果体验:http://hovertree.com/texiao/jqimg/1/ 效果图: 代码如下: <!DOCTYPE html> <html> <head> ...

  4. JS jQuery 点击页面弹出文字

    <style> .animate{ animation:myfirst 3s; -moz-user-select:none;/*火狐*/ -webkit-user-select:none; ...

  5. jQuery点击图片弹出大图遮罩层

    使用jQuery插件HoverTreeShow弹出遮罩层显示大图 效果体验:http://hovertree.com/texiao/hovertreeshow/ 在开发HoverTreeTop项目的产 ...

  6. 前端jquery实现点击图片弹出大图层&lpar;且滚动鼠标滑轮图片缩放&rpar;

    <img src="{$vo.photo}" height="50px" onclick="showdiv({$i});"> & ...

  7. 基于jQuery图片弹出翻转特效代码

    分享一款基于jQuery图片弹出翻转特效代码.这是一款基于jQuery+HTML5实现的,里面包含六款不同效果的鼠标点击图片弹出特效下载.效果图如下: 在线预览   源码下载 实现的代码. html代 ...

  8. jquery实现点击按钮弹出层和点击空白处隐藏层

    昨天做项目遇到一个问题,和大家分享下,jquery实现点击按钮弹出层和点击空白处隐藏层的问题 if($('.autoBtn').length){                $('.autoBtn' ...

  9. jQuery WIN 7透明弹出层效果

    jQuery WIN 7透明弹出层效果,点击可以弹出一个透明层的jquery特效,插件可以调弹出框的宽度和高度,很不错的一个弹出层插件. 适用浏览器:IE8.360.FireFox.Chrome.Sa ...

随机推荐

  1. python os&period;walk&lpar;&rpar;

    os.walk()返回三个参数:os.walk(dirpath,dirnames,filenames) for dirpath,dirnames,filenames in os.walk(): 返回d ...

  2. Hydra用户手册

    Hydra 参数: -R继续从上一次进度接着破解 -S大写,采用SSL链接 -s <PORT>小写,可通过这个参数指定非默认端口 -l <LOGIN>指定破解的用户,对特定用户 ...

  3. Sprint第三个冲刺(第三天)

    一.Sprint介绍 任务进度: 二.Sprint周期 看板: 燃尽图:

  4. JVM调优浅谈

    1.数据类型 java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:它代表的值就是数值本身,而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本 ...

  5. ubuntu 安装 rabbitmq-server

    Rabbitmq 是用 erlang 语言写的,所以我们需要安装 Erlang,安装 erlang 又需要安装 python 与 simplejson,所以我们从python开始: 1.安装 pyth ...

  6. MVVM in Depth

    这篇文章开始粗略的介绍了软件开发中松耦合的概念并讲述了使用MVC.MVP和MVVM三种模式达到松耦合.然后分析了这三种模式适用范围,其中: MVC(Model-View-Controller)适用于w ...

  7. TFS 安装与管理

    整了几天TFS,把相关的一些配置与安装的要点简单记下,希望对大家有用.本篇主要是安装与配置上的内容,下一篇会介绍如何使用以及使用方面的相关心得体会. 本篇内容简要: 1.   安装部署 1.1.  流 ...

  8. Spring IOC之基于JAVA的配置

    基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...

  9. 即时作图新工具—ProcessOn【推荐】

    www.processon.com 推荐这个在线作图网站:免费登录,云端存储,面向对象,最重要的是提供了丰富模板! 在线软件的人气会越来越高,这是趋势啊~

  10. flask 实现登录 登出 检查登录状态 的两种方法的总结

    这里我是根据两个项目的实际情况做的总结,方法一(来自项目一)的登录用的是用户名(字符串)和密码,前后端不分离,用form表单传递数据:方法二用的是手机号和密码登录,前后端分离,以json格式传递数据, ...