搜索
首页web前端js教程原生javascript实现图片弹窗交互效果_javascript技巧

【一】用var 声明多个变量,比每个变量都用var快多了   

复制代码 代码如下:

var sScrollTop = document.body.scrollTop || document.documentElement.scrollTop,
    sWindow_h = document.documentElement.clientHeight,
    t_h = parseInt(this.getCss(this.getId('gy_photoBox_head'),'height')),
    hold_h = sWindow_h - t_h - 20,
    width = this.nImgWidth ,
    height = this.nImgHeight;  

【二】Dom事件优化,在 window.onresize时,定义个定时器,setTimeout,可以防止事件频繁调用

复制代码 代码如下:

windowResize:function(){
            var _that = this,
                _timer = null;
            // 函数节流
            window.onresize = function(){
                clearTimeout(_timer);
                _timer = setTimeout(function(){
                    if( _that.tools.getId('gy_photoBox')){
                        _that.setBoxCss();
                    }

                },100);
            }       
        }

【三】图片加载的处理函数

复制代码 代码如下:

/*
        @ src [String] 图片的地址
        @ success [Function] 图片加载成功的回调函数
        @ error [Function] 图片加载失败的回调函数
        */
        imgLoading:function(opt){
            var _img = new Image(),
                _that = this;
            _img.onload = function(){
                _that.nImgWidth = this.width;
                _that.nImgHeight = this.height;
                if(typeof opt.success == 'function'){
                    setTimeout(function(){
                        opt.success();
                    },300);
                }
            }
            _img.onerror = function(){
                if(typeof opt.error){
                    opt.error();
                }           
            }
            // 注意:要放在onload事件下面,否则ie会出现BUG
            _img.src = opt.src;
        }

源代码:

复制代码 代码如下:

/*
author:laoguoyong
*/
(function(){
    /* -------------------------简单的选择器-----------------------
    @ 参数 [string]
    ---------------------------------------
    ★-只支持以下选择-★
    @ 支持一级选择器:如'#id','.class','p'
    @ 支持后代选择,如 '.class p','body span'
    @ 支持子元素选择,如 '.class>p','body>span'
    ----------------------------------------
    @ return [Array]
    */
    var selector = function(str){
        // 定义元素数组
        var elem = [];
        /* 私有方法
        ------------------------*/
        //返回是id的元素
        function _getId(id){
            return document.getElementById(id);
        }
        //返回存在此类名的元素-元素
        function _getByClassName(className,parent){
            var class_array = [],
                node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),
                reg = new RegExp("(^|\\s)"+className+"(\\s|$)");
            for(var n=0,i=node.length;n                 if(reg.test(node[n].className)){
                    class_array.push(node[n]);
                }
            }
            return class_array;
        }
        //一级选择,如 '#id','p','.class'
        // return [Array]
        function _getDom(s){
            var array_elem = [];
            if (s.indexOf('#')==0){
                array_elem.push(_getId(s.slice(1)));
            }
            else if(s.indexOf('.')==0){
                array_elem = array_elem.concat(_getByClassName(s.slice(1)));
            }
            else{
                var tag = document.getElementsByTagName(s);
                for(var n=0,i=tag.length;n                     array_elem.push(tag[n]);
                }
            }
            return array_elem;
        }
        /*
        @ arry_elm [Array] : 元素数组,如 ['.demo','p'] ,选择的是.demo下面的p元素,至于是选择后代还是子代,请看第2个参数解释
        @ r [String] -可选(不传默认为选择后代): '>',是选择子代元素;
        --------------------------
        @ return [Array]
        */
        function _query(array_elem,r){
            var node = array_elem,
                type_name = node[0].match(/\#/)?'id_'+node[0].slice(1):node[0].match(/\./)?'className_'+node[0].slice(1):'tagName_'+node[0],
                child = _getDom(node[1]),
                type = type_name.split('_'),
                len = document.getElementsByTagName('*').length,
                reg = new RegExp("(^|\\s)"+type[1]+"(\\s|$)");;
            for(var i=0,j=child.length;i                 var par = child[i].parentNode;
                for(var n=0;n                     if(par.nodeType == 9){
                        break;
                    }
                    if(reg.test(par[type[0]])){
                        elem.push(child[i]);
                        break;                   
                    }else{
                        if(r == '>') break;
                        par = par.parentNode;
                    }       
                }
            }
        }
        /* 接口
        -----------------------*/
        var elemStr = str.replace(/(^\s+)|(\s+$)/,'');
        if(document.querySelectorAll){
            var dom = document.querySelectorAll(elemStr);
            for(var n=0,len=dom.length;n                 elem.push(dom[n]);
            }
        }else{
            var    split = /[\>\s]/g.exec(elemStr);
            if(split){
                var node = elemStr.split(split[0]);
                _query(node,split[0]);
            }else{
                elem = elem.concat( _getDom(elemStr) );
            }
        }
        return elem;
    }
    /* 弹窗功能构造函数
    -----------------------*/
    function LGY_photoBox(option){
        this.opt = option;
        this.oTarget = typeof option.target == 'object'?option.target:selector(option.target);
        if(!this.oTarget) return;
        this.nLen = this.oTarget.length; //总个数
        this.aBigimg_src = []; //大图数据数组
        this.aTitle = []; //标题数据数组
        this.nIndex = 0; //索引
        this.nImgWidth = 0; //动态获取图片的宽
        this.nImgHeight = 0; //动态获取图片的高
        this.nDelay = 0.2;
        this.intit();
    }
    LGY_photoBox.prototype = {
        intit:function(){
            var _that = this;
            this.getData();
            for(var n=0;n                 this.oTarget[n].index = n;
                this.oTarget[n].onclick = function(e){
                    _that.createCover();
                    var e = _that.tools.getEvent(e),
                        target = _that.tools.getTarget(e);
                    // 设置浏页面没有滚动条出现
                    _that.tools.setCss(document.documentElement,{'height':'100%','overflow-y':'hidden','overflow-x':'hidden'});
                    // 获取当时索引
                    _that.nIndex = this.index;
                    //首次判断
                    _that.firstLoad(_that.aBigimg_src[_that.nIndex],function(){
                        //插入结构
                         _that.createBoxDom();
                        //关闭
                        _that.tools.getId('gy_photoBox_close').onclick = function(){
                            _that.removeBox();                                   
                        }
                        // 判断左右按钮显示
                        _that.btnIsShow();   
                        // 上一张
                        _that.btnPrev();
                        // 下一张
                        _that.btnNext();
                        // 加载图片
                        _that.imgChange(_that.aBigimg_src[_that.nIndex]);
                    });
                    // 重置窗口大小
                    _that.windowResize();
                     // 键盘事件
                    _that.keyEvent();
                    //阻止跳转
                    return false;   
                }
            }
        },
        createBoxDom:function(){
            var doc = document,
                exHtml = '',
                boxHtml = doc.createElement('div');
            boxHtml.id = 'gy_photoBox';
            doc.body.appendChild(boxHtml);
            if(typeof this.opt.appendHTML == 'string'){
                exHtml = this.opt.appendHTML;
            }
            boxHtml.innerHTML = '
'+
                            '
'+
                            ''+
                            '
'+exHtml+'
'+
                            '
'+
                                '原生javascript实现图片弹窗交互效果_javascript技巧http://www.pconline.com.cn/blank.gif" />'+
                                '原生javascript实现图片弹窗交互效果_javascript技巧'+
                                '
'+
                                    ''+
                                        ''+
                                        '/'+this.nLen+
                                    '
'+
                                    '

'+
                                '
'+
                            '
';
        },
        createCover:function(){
            // 创建覆盖层
            var    doc = document,
                coverHtml = doc.createElement('div');
                coverHtml.id = 'gy_photoBox_cover';
            doc.body.appendChild(coverHtml);
            //设置覆盖层的样式
            this.tools.setCss(this.tools.getId('gy_photoBox_cover'),{'height':(doc.body.scrollTop || doc.documentElement.scrollTop)+(doc.documentElement.clientHeight)+'px'});
        },
        setBoxCss:function(){
            var    doc = document,
                nScrollTop = doc.body.scrollTop || doc.documentElement.scrollTop,
                nWindow_h = doc.documentElement.clientHeight,
                eBox_head_h = this.tools.getId('gy_photoBox_head').clientHeight,
                eBox = this.tools.getId('gy_photoBox'),
                eBoxPadding = 10,
                hold_h = nWindow_h - eBoxPadding - 50 - eBox_head_h,
                width = this.nImgWidth ,
                height = this.nImgHeight;
            // alert('nWindow_h:'+nWindow_h+'-'+'eBoxPadding:'+eBoxPadding+'-'+'eBox_head_h:'+eBox_head_h);
            // 图片大小超过可见范围,进行缩放
            if(this.nImgHeight>hold_h){
                height = hold_h,
                width = Math.ceil(this.nImgWidth*(height/this.nImgHeight));
            }
            //设置盒子在整个页面居中
            this.tools.setCss(eBox,{'width':width+'px',
                                    'height':eBox_head_h + height + 'px',
                                    'margin-left':-(width+eBoxPadding)/2+'px',
                                    'top':nScrollTop+(nWindow_h-height-eBoxPadding)/2+'px'});
            this.tools.setCss(this.tools.getId('gy_photoBox_main'),{'width':width+'px','height':height + 'px'});
            //设置覆盖层的样式
            this.tools.setCss(this.tools.getId('gy_photoBox_cover'),{'height':nScrollTop+doc.documentElement.clientHeight+'px'});
        },
        removeBox:function(){
            var doc = document;
            if(this.tools.getId('gy_photoBox')){
                doc.body.removeChild(this.tools.getId('gy_photoBox'));
            }
            if(this.tools.getId('gy_photoBox_cover')){
                document.body.removeChild(this.tools.getId('gy_photoBox_cover'));
            }
            this.tools.setCss(document.documentElement,{'height':'auto','overflow-y':'auto','_overflow-y':'scroll','overflow-x':'auto'});
        },
        getData:function(){
            for(var n=0;n                 var src = this.oTarget[n].getAttribute('href'),
                    title = this.oTarget[n].getAttribute('title');
                this.aBigimg_src.push(src);
                if(!title) title = '';
                this.aTitle.push(title);
            }
        },
        btnIsShow:function(){
            this.tools.setCss(this.tools.getId('gy_photoBox_prev'),{'display':'block'});
            this.tools.setCss(this.tools.getId('gy_photoBox_next'),{'display':'block'});
            if(this.nIndex == 0) this.tools.setCss(this.tools.getId('gy_photoBox_prev'),{'display':'none'});
            if(this.nIndex == (this.nLen-1)) this.tools.setCss(this.tools.getId('gy_photoBox_next'),{'display':'none'});
        },
        imgChange:function(){
            var _that = this,
                _src = this.aBigimg_src[this.nIndex],
                eLoadingTips = this.tools.getId('gy_photoBox_img_loading'),
                eImg = this.tools.getId('gy_photoBox_img'),
                eTitle = this.tools.getId('gy_photoBox_title'),
                eInfor = this.tools.getId('gy_photoBox_infor');
            // 显示loading图片
            this.tools.setCss(eLoadingTips,{'display':'block'});
            this.tools.setCss(eInfor,{'display':'none'});
            // 判断左右按钮显示
            this.btnIsShow();
            // 图片加载处理
            this.imgLoading({
                'src':_src,
                'success':function(){
                    _that.tools.setCss(eLoadingTips,{'display':'none'});
                    _that.tools.setCss(eInfor,{'display':'block'});
                    // 设置真实图片路径,标题,当前页码
                    eImg.src = _src;
                    eTitle.innerHTML = _that.aTitle[_that.nIndex];
                    _that.tools.getId('gy_photoBox_index').innerHTML = (_that.nIndex+1);
                    // 设置样式
                    _that.setBoxCss();
                    // 弹窗呈现
                    _that.tools.setCss(_that.tools.getId('gy_photoBox'),{'visibility':'visible'});
                    if(_that.tools.getId('gy_photoBox_firstLoad')){
                        document.body.removeChild(_that.tools.getId('gy_photoBox_firstLoad'));
                    }
                    // 每次切换执行的回调函数
                    if(typeof _that.opt.onChange == 'function'){
                        _that.opt.onChange({'src':_src,'index':_that.nIndex,'title':_that.aTitle[_that.nIndex]});
                    }
                },
                'error':function(){
                    setTimeout(function(){
                        _that.tools.setCss(eLoadingTips,{'display':'none'});
                    },200);
                    eImg.src = 'gyPhotoBox/error.png';
                    eTitle.innerHTML = '暂无相关图片';
                    _that.nImgWidth = 400;
                    _that.nImgHeight = 300;
                    _that.setBoxCss();
                    _that.tools.setCss(_that.tools.getId('gy_photoBox'),{'visibility':'visible'});
                    if(_that.tools.getId('gy_photoBox_firstLoad')){
                        document.body.removeChild(_that.tools.getId('gy_photoBox_firstLoad'));
                    }
                }
            });
        },
        btnPrev:function(){
            var _that = this;
            this.tools.getId('gy_photoBox_prev').onclick = function(){
                _that.nIndex--;
                _that.imgChange();
            }
        },
        btnNext:function(){
            var _that = this;
            this.tools.getId('gy_photoBox_next').onclick = function(){
                _that.nIndex++;
                _that.imgChange();
            }
        },
        keyEvent:function(){
            var _that = this;
            document.onkeydown = function(e){
                var e = e || window.event;
                switch(e.keyCode){
                    case 37:{
                        if(_that.nIndex != 0&&_that.tools.getId('gy_photoBox_prev')){
                            _that.nIndex--;
                            _that.imgChange();   
                        }   
                    };break;
                    case 39 :{
                        if(_that.nIndex != (_that.nLen-1)&&_that.tools.getId('gy_photoBox_next')){
                            _that.nIndex++;
                            _that.imgChange();   
                        }           
                    };break;
                    case 27:{
                        _that.removeBox();                           
                    };break;
                }
            }
        },
        /*
        @ src [String] 图片的地址
        @ success [Function] 图片加载成功的回调函数
        @ error [Function] 图片加载失败的回调函数
        */
        imgLoading:function(opt){
            var _img = new Image(),
                _that = this;
            _img.onload = function(){
                _that.nImgWidth = this.width;
                _that.nImgHeight = this.height;
                if(typeof opt.success == 'function'){
                    setTimeout(function(){
                        opt.success();
                    },300);
                }
            }
            _img.onerror = function(){
                if(typeof opt.error){
                    opt.error();
                }           
            }
            // 注意:要放在onload事件下面,否则ie会出现BUG
            _img.src = opt.src;
        },
        firstLoad:function(src,callback){
            var _that = this,
                html = document.createElement('div');
                html.id = 'gy_photoBox_firstLoad';
            document.body.appendChild(html);
            this.tools.setCss(this.tools.getId('gy_photoBox_firstLoad'),{'top':(document.body.scrollTop || document.documentElement.scrollTop)+(document.documentElement.clientHeight/2) +'px'});
            if(typeof callback == 'function') {
                callback();
            }
        },
        windowResize:function(){
            var _that = this,
                _timer = null;
            // 函数节流
            window.onresize = function(){
                clearTimeout(_timer);
                _timer = setTimeout(function(){
                    if( _that.tools.getId('gy_photoBox')){
                        _that.setBoxCss();
                    }
                },100);
            }       
        },
        tools:function(){
            return{
                getEvent:function(e){
                    return e || window.event;
                },
                getTarget:function(e){
                    return e.target || e.srcElement;
                },
                preventDefault:function(e){
                    e.preventDefault?e.preventDefault():e.returnValue = false;
                },
                getId:function(id){
                    return document.getElementById(id);
                },
                getCss:function(node,value){
                    return node.currentStyle?node.currentStyle[value]:getComputedStyle(node,null)[value];
                },
                setCss:function(node,val){
                    for(var v in val){
                        node.style.cssText += ';'+ v +':'+val[v];
                    }
                }
            }
        }()
    }
    window.LGY_photoBox = LGY_photoBox;
})();

最终效果图:

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境