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 src="https://www.jb51.net/template/27/img/loading.gif"/>');
                    $('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('<img src="'https://www.jb51.net/+img.src+'"/>');
                        /************图片预加载,重新调整窗口大小及位置**************/

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

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


popBox代码

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdgzxp.html