热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

JS自动适应的图片弹窗实例_javascript技巧

这篇文章介绍了JS自动适应的图片弹窗实例代码,有需要的朋友可以参考一下
代码如下:


/************************************自动适应的图片弹窗*********************************/
var autoImg=function(argcs){/*调整图片大小,等比例缩放argcs['maxHeight']=>最大高度,argcs['maxWidth']=>最大宽度,argcs['height']=>图片高度,argcs['width']=>图片宽度*/
var _maxHeight='';
var _maxWidth='';
var _newSize=[];

if(argcs['maxHeight']){
_maxHeight=argcs['maxHeight'];
}
if(argcs['maxWidth']){
_maxWidth=argcs['maxWidth'];
}
if(!argcs['height']){
throw new Error('height未指定');
}
if(!argcs['width']){
throw new Error('width未指定');
}
if(argcs['height']>argcs['width']||argcs['height']==argcs['width']){//高度不小于宽度的情况
if(argcs['height']>=_maxHeight){
_newSize['height']=_maxHeight;
_newSize['width']=(_maxHeight/argcs['height'])*argcs['width'];
}else{
_newSize['width']=argcs['width'];
_newSize['height']=argcs['height'];
}
return _newSize;
}
if(argcs['width']>argcs['height']){//宽度大于高度的情况
if(argcs['width']>=_maxWidth){
_newSize['width']=_maxWidth;
_newSize['height']=(_maxWidth/argcs['width'])*argcs['height'];
}else{
_newSize['width']=argcs['width'];
_newSize['height']=argcs['height'];
}
return _newSize;
}
}

var imgBox=function(imgSrc){
var winImg=new popBox({//图片弹窗
ID:'imgBox',
bgColor:'#a3c90e',
width:906,
moveHandle:false,
closeButton:false,
height:'auto',
times:250,
lock:true,
content:'',
shadow:true,
position:'center',
displayCallBack:function(){
$('body').append('');
$('img[id=loading]').css('z-index',110000).css({position:'absolute',left:$(window).scrollLeft()+($(window).width())/2-($('img[id=loading]').width())/2-22,top:$(window).scrollTop()+($(window).height()/2-($('img[id=loading]').width())/2)});
/************图片预加载,重新调整窗口大小及位置**************/
var img=new Image();
var _imgWidth=0;
var _imgHeight=0;
img.src=imgSrc;//为img对象添加地址
// console.log(imgSrc);
/*************************图片加载完成之后***************************/
img.Onload=function(){
$('img[id=loading]').remove();
_imgWidth=img.width;
_imgHeight=img.height;
var argcs=[];
var winWidth=$(window).width();
argcs['maxHeight']=750;//最大高度
argcs['maxWidth']=900;//最大宽度
argcs['height']=_imgHeight;
argcs['width']=_imgWidth;
var newWH=autoImg(argcs);//获得缩略后的图片宽和高
/************图片预加载,重新调整窗口大小及位置************/
$('#'+winImg.ID).css({width:newWH['width'],height:newWH['height'],top:parseInt($(document).scrollTop())+parseInt(($(window).height()-newWH['height'])/2),left:$(document).scrollLeft()+parseInt(winWidth/2)-parseInt(newWH['width']/2)});
$('#'+winImg.ID).html('');
/************图片预加载,重新调整窗口大小及位置**************/

$('#'+winImg.ID+'_bg').css('cursor','pointer').click(function(){
winImg.kill();
});
};
/*************************图片加载完成之后***************************/
},
unDisplayCallBack:function(){

},
killCallBack:function(){
}
});
winImg.dispaly();
}


popBox代码

代码如下:


//若使用移动功能,请先导入jQuery移动UI组件
var popBox=function(settings){//弹窗函数settings=[]
//alert(typeof settings['width']);
//alert(settings['displayCallBack']);
/************************本类私有变量*****************************/
/*******************默认值*****************/
var _shadow=true;//是否有遮罩true/false
var _closeButton=false;//关闭按钮false/dom元素
var _killButton=false;//kill按钮false/dom元素
var _moveHandle=false;//拖动手柄false/dom元素
var _width=650;//宽,
var _bgColor='#FFF';//背景样式
var _height='auto';//高
var _cOntent='没有内容';//内容
var _position='center';/*位置topLeft,topCenter,topRight,center,bottomLeft,bottomRight,bottomCenter*/
var _lock=true;//是否锁定
var _times=500;//显示,隐藏的时间
var _displayCallBack=function(){//dispaly回调函数
alert('display');
}
var _unDisplayCallBack=function(){//unDispaly回调函数
alert('unDisplay');
}
var _beforeKillCallBack=function(){
alert('beforeKill');
}//kill之前的回调函数
var _killCallBack=function(){//kill回调函数
alert('kill');
}
/*******************默认值*****************/
if(settings['closeButton']!==undefined){
//alert('shadow');
_closeButton=settings['closeButton'];
}
if(settings['killButton']!==undefined){
//alert('shadow');
_killButton=settings['killButton'];
}
if(settings['moveHandle']!==undefined){
//alert('shadow');
_moveHandle=settings['moveHandle'];
}
/******************获得设置值********************/
/**settings['shadow']!=' ' && settings['shadow']!=undefined*/
if(settings['shadow']!==undefined){
//alert('shadow');
_shadow=settings['shadow'];
}
if(settings['bgColor']!==undefined){
//alert('shadow');
_bgColor=settings['bgColor'];
}
if( settings['width']!==undefined){
_width=settings['width'];
}
if( settings['height']!==undefined){
_height=settings['height'];
}
if(settings['content']!==undefined){
_cOntent=settings['content'];
}
if(settings['position']!==undefined){
_position=settings['position'];
}
if( settings['times']!==undefined){
_times=settings['times'];
}
if(settings['lock']!==undefined){
_lock=settings['lock'];
}
if(settings['displayCallBack']!=undefined){
//alert('here');
_displayCallBack=settings['displayCallBack'];
}
if( settings['unDisplayCallBack']!==undefined){
_unDisplayCallBack=settings['unDisplayCallBack'];
}
if( settings['beforeKillCallBack']!==undefined){
_beforeKillCallBack=settings['beforeKillCallBack'];
}
if( settings['killCallBack']!==undefined){
_killCallBack=settings['killCallBack'];
}

//alert(settings['shadow']);
//alert(_shadow);
/************************本类私有变量******************************/



/*********************本类内部变量********************/
var _this=this;
var _baseZindex=10000;
var _domWidth=$(document).width();
var _domHeight=$(document).height();
/********************本类内部变量*******************/


/********************本类私有函数**********************/
var _getZindex=function(){/*获得z-index->首先遍历网页p元素ID中含有popBox的DOM,获得数量,然后本弹窗背景z-index=基数+当前数量+1,弹框z-index=基数+当前数量+2*/
var _len=$('body').children('p').length;
var _countDiv=0;
var _pObj=$('body').children('p');
var _reg=/^popBox_/;//正则表达式
for(var i=0;i<_len;i++){
if(_reg.test(_pObj.eq(i).attr('ID'))){
_countDiv+=1;
}
}
return _countDiv;//返回已有弹框的数量
}
var _getWinZindex=function(){//获得弹窗的z-index
var _winZindex=_baseZindex+_getZindex()+2;
return _winZindex;
}
var _geWinBgZindex=function(){//获得弹窗背景的z-index
var _winBgZindex=_baseZindex+_getZindex()+1;
return _winBgZindex;
}
var _renderBg=function(){//渲染背景 www.jb51.net
var _winBgZindex=_geWinBgZindex();
//alert($(document).height());
$('body').append('

');//在body中插入一个半透明的背景
$('#'+_this.ID+'_bg').addClass('popBox_bg').css({height:_domHeight,width:_domWidth,opcity:0}).css('z-index',_winBgZindex).fadeTo(_times,0.7);
}
var _creatWin=function(){//创建窗体
$('body').append('

');
_renderContent(_content);//渲染弹窗主体
_initWin();//初始化窗体
}
var _initWin=function(){//初始化窗体
var _winZindex=_getWinZindex();
var _transHeight=0;
if(_height=='auto'){
_transHeight='auto';
}else{
_transHeight=parseInt(_height)+'px';
}
$('#'+_this.ID).css({width:parseInt(_width)+'px',height:_transHeight,position:'absolute',opticity:1.0,background:_bgColor}).css('z-index',_winZindex);
if(_lock==false){
if(_moveHandle!==undefined&&_moveHandle!==false&&_moveHandle!==' '){
$('#'+_this.ID).children(_moveHandle).css('cursor','move');
//alert(_moveHandle);
//alert($('#'+_this.ID).children(_moveHandle).html());
}
}
_locationWin();//为窗体定位
}
var _locatiOnWin=function(){/*为窗体定位topLeft,topCenter,topRight,centerLeft,center,centerRight,bottomLeft,bottomCenter,bottomRight*/
var _windowHeight=parseInt($(window).height());
var _windowWidth=parseInt($(window).width());
//alert(_windowWidth+_height);
var _left=(_windowWidth-parseInt(_width))/2;
var _top=parseInt($(document).scrollTop())+parseInt(($(window).height()-$('#'+_this.ID).height())/2);
$('#'+_this.ID).css({top:_top+'px',left:_left+'px'});
}
var _renderCOntent=function(content){//渲染弹窗主体
$('#'+_this.ID).append(content);
}
var bindEvent=function(){//绑定事件
if(_this.status!=='kill'&&_this.status!=='init'){
if(_closeButton!==undefined&&_closeButton!==' '&&_closeButton!==false){
$('#'+_this.ID+' '+_closeButton).css('cursor','pointer').live('click',function(e){
_this.unDisplay();
});
}//若设置了关闭(close)按钮
if(_killButton!==undefined&&_killButton!==' '&&_killButton!==false){
$('#'+_this.ID+' '+_killButton).css('cursor','pointer').live('click',function(e){
_this.kill();
});
}//若设置了杀死(kill)按钮
}
if(_lock==false){
$('#'+_this.ID).draggable({cancel:''});
}
}
/*********************本类私有函数*****************/


/**********************本类公有函数******************/
this.status='init';//当前状态init->初始化状态,display->display状态,undisplay->undisplay状态,kill->kill状态
this.ID='';
var _ID=settings['ID'];
if(_ID==' '||(typeof _ID)==undefined){
throw new Error('ID不能为空');
}else{
this.ID='popBox_'+_ID;
}
this.display=function(){//显示函数,如果状态是init或者kill重新渲染页面
//alert(_this.status);
if(_this.status=='init'||_this.status=='kill'){
_creatWin();//创建窗体
//$('#'+_this.ID).css('height',_domHeight);
if(_shadow==true){//渲染遮罩
/*alert(_shadow);*/
_renderBg();
}
_this.status='display';
_displayCallBack();
}else{
$('#'+_this.ID).fadeIn(_times);
if(_shadow==true){
$('#'+_this.ID+'_bg').fadeIn(_times);
}
_this.status='display';
}
//alert(typeof _displayCallBack);
//alert(_this.status);
bindEvent();
}
this.kill=function(){//彻底移除
//alert(_this.status);
//alert(_this.status);
if(_this.status=='kill'||_this.status=='init'){
//alert(_this.status);
throw new Error('非法操作,当前状态不允许kill');
}
if(_beforeKillCallBack!=undefined){
_beforeKillCallBack();
}
$('#'+_this.ID).remove();
if(_shadow==true){
$('#'+_this.ID+'_bg').remove();
}
_this.status='kill';
if(_killCallBack!=undefined){
_killCallBack();
}
}
this.unDisplay=function(){//隐藏函数
if(_this.status=='init'||_this.status=='kill'){
throw new Error('非法操作,当前状态不允许undisplay');
}
if(_unDisplayCallBack!=undefined){
_unDisplayCallBack();
}
$('#'+_this.ID).fadeOut(_times);
$('#'+_this.ID+'_bg').fadeOut(_times);
_this.status='undisplay';
}
/**********************本类公有函数******************/


}//popBox网页弹窗

var errorBox=function(errorMsg){
//alert(typeof errorBox);
//alert(errorBox.length);
var errorObj=new popBox({
ID:'errorObj',
bgColor:'#FFF',
width:300,
moveHandle:false,
closeButton:false,
height:'auto',
times:200,
lock:true,
content:$('#errorBoxContent').html(),
shadow:true,
position:'center',
displayCallBack:function(){
$('#'+errorObj.ID).find('.errorMessage').html();
$('#'+errorObj.ID).find('.errorMessage').html(errorMsg);
$('#'+errorObj.ID).find('.errorConfirm input').click(function(){
//alert('here');
errorObj.kill();
});
},
unDisplayCallBack:function(){
throw new Error('错误不可以关闭,只可以Kill');//错误方法只能Kill,不能关闭
},
killCallBack:function(){
//errorBox=null;
}
});
errorObj.dispaly();
}//错误弹窗
推荐阅读
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 本文介绍了解决IE678伪类不兼容问题的方法,包括少用CSS3和HTML5独有的属性,使用CSS hacker,使用last-child清除浮动、批量添加标签、去掉list item最后一个的border-right等技巧。同时还介绍了使用after清除浮动时加上IE独有属性zoom:1的处理方法。另外,本文还提到可以使用jQuery代替批量添加标签的功能,以及使用负边距和CSS2选择器element+element去掉list item最后一个的border-right的方法。 ... [详细]
  • 本文介绍了前端人员必须知道的三个问题,即前端都做哪些事、前端都需要哪些技术,以及前端的发展阶段。初级阶段包括HTML、CSS、JavaScript和jQuery的基础知识。进阶阶段涵盖了面向对象编程、响应式设计、Ajax、HTML5等新兴技术。高级阶段包括架构基础、模块化开发、预编译和前沿规范等内容。此外,还介绍了一些后端服务,如Node.js。 ... [详细]
  • HTML5网页模板怎么加百度统计?
    本文介绍了如何在HTML5网页模板中加入百度统计,并对模板文件、css样式表、js插件库等内容进行了说明。同时还解答了关于HTML5网页模板的使用方法、表单提交、域名和空间的问题,并介绍了如何使用Visual Studio 2010创建HTML5模板。此外,还提到了使用Jquery编写美好的HTML5前端框架模板的方法,以及制作企业HTML5网站模板和支持HTML5的CMS。 ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
  • 从零基础到精通的前台学习路线
    随着互联网的发展,前台开发工程师成为市场上非常抢手的人才。本文介绍了从零基础到精通前台开发的学习路线,包括学习HTML、CSS、JavaScript等基础知识和常用工具的使用。通过循序渐进的学习,可以掌握前台开发的基本技能,并有能力找到一份月薪8000以上的工作。 ... [详细]
  • 本文介绍了如何在Jquery中通过元素的样式值获取元素,并将其赋值给一个变量。提供了5种解决方案供参考。 ... [详细]
  • 本文介绍了使用jQuery实现图片预加载和等比例缩放的方法,同时提供了演示和相关代码。该方法可以重置图片的宽度和高度,并使图片在水平和垂直方向上居中显示。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 例如控件ID为user.id使用$(#user.id)不能得到正确的结果必须使用\\转义即$(#user\\.id)转载于:https:www.cnblogs.comrch ... [详细]
  • jQuery实现简单的动画效果及用法详解
    本文详细介绍了使用jQuery实现简单动画效果的方法,包括显示/隐藏、向上收缩/向下展开、淡入/淡出、自定义动画等。同时提供了具体的用法示例,并解释了参数的含义和使用技巧。通过本文的学习,读者可以掌握如何使用jQuery实现各种动画效果,为网页增添生动和互动性。 ... [详细]
author-avatar
早晚虚文_114
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有