js图片画廊点击弹出遮罩背景大图切换代码

来源:https://www.sucaihuo.com/js/3751.html 2018-06-16 11:25浏览(937) 收藏

一款简单实用的js图片画廊点击弹出遮罩背景大图切换代码,点击小图弹出大图幻灯片展示,左右按钮控制图片上一张下一张进行切换。
js图片画廊点击弹出遮罩背景大图切换代码
分类:图片代码 > 缩略图 难易:初级
查看演示 下载资源: 25 下载资源 下载积分: 20 积分

js代码

<script>
(function(){
    var LightBox = function(options){
        this.imgListParent = document.getElementById(options.imgListParent);   //图片列表的父元素
        this.imgItemClass = options.imgItemClass;   //图片的className
        this.idx = 0;  //图片的索引,初始值为0
        this.isShowPage = options.isShowPage || false;    //是否显示分页,默认不显示
        this.init();
    };
    //初始化
    LightBox.prototype.init = function(){
        this.renderDOM();
        this.imgListClick();
        this.nextBtnClick();
        this.prevBtnClick();
        this.closeBtnClick();
    };
    //渲染弹窗
    LightBox.prototype.renderDOM = function(){
        var imgModule = document.createElement("div");
        imgModule.id = "imgModule";

        var oHtml = "";
        oHtml += '<div class="mask"></div>';
        oHtml +=    '<div class="lightBox">';
        oHtml +=        '<div class="lightBoxContent">';
        oHtml +=            '<img src="images/loading.gif" alt="" id="imgLoader"/>';
        oHtml +=            '<img alt="" id="imgLight"/>';
        oHtml +=        '</div>';
        oHtml +=        '<span class="btn lightBoxSprite" id="lightBoxPrev"></span>';
        oHtml +=        '<span class="btn lightBoxSprite" id="lightBoxNext"></span>';
        oHtml +=        '<span class="closeBtn lightBoxSprite" id="closeBtn"></span>';
        oHtml +=        '<div class="lightBoxPagination" id="lightBoxPagination"></div>';
        oHtml +=    '</div>';

        imgModule.innerHTML = oHtml;
        document.body.appendChild(imgModule);
    };
    //分页
    LightBox.prototype.pagination = function(idx){
        var imgList = this.getByClass(this.imgListParent, this.imgItemClass);
        var pagination = document.getElementById("lightBoxPagination");
        var page = "";

        for(var i = 0; i < imgList.length; i++){
            if(idx == i){
                page += '<span class="current"></span>';
            }else{
                page += '<span></span>';
            }            
        }
        if(this.isShowPage){
            pagination.innerHTML = page;
        }        
    };
    //点击图片弹出弹窗
    LightBox.prototype.imgListClick = function(){
        var imgList = this.getByClass(this.imgListParent, this.imgItemClass);
        var imgModule = document.getElementById("imgModule");
        var self = this;

        for(var i = 0; i < imgList.length; i++){
            imgList[i].index = i;

            imgList[i].onclick = function(){
                imgModule.style.display = "block";
                var src = this.getAttribute("data-src");
                self.idx = this.index;

                self.imgLoad(src);
                self.pagination(self.idx);
            }
        }
    };
    //上一张
    LightBox.prototype.prevBtnClick = function(){
        var prevBtn = document.getElementById("lightBoxPrev");
        var self = this;

        prevBtn.onclick = function(){
            var imgList = self.getByClass(self.imgListParent, self.imgItemClass);
            
            self.idx--;

            if(self.idx < 0){  
                self.idx = imgList.length - 1;  
            }

            var src = imgList[self.idx].getAttribute("data-src");
            self.imgLoad(src);
            self.pagination(self.idx);
        }
    };
    //下一张
    LightBox.prototype.nextBtnClick = function(){
        var nextBtn = document.getElementById("lightBoxNext");
        var self = this;
        
        nextBtn.onclick = function(){
            var imgList = self.getByClass(self.imgListParent, self.imgItemClass);

            self.idx++;            

            if(self.idx >= imgList.length){  
                self.idx = 0;  
            }

            var src = imgList[self.idx].getAttribute("data-src");
            self.imgLoad(src);
            self.pagination(self.idx);
        }
    };
    //图片预加载
    LightBox.prototype.imgLoad = function(src, callback){
        var imgLight = document.getElementById("imgLight");
        var loader = document.getElementById("imgLoader");
        loader.style.display = "block";
        // imgLight.src = "";

        var img = new Image();
        img.onload = function(){
            loader.style.display = "none";
            imgLight.src = src;
        };
        img.src = src;
    };
    //关闭弹窗
    LightBox.prototype.closeBtnClick = function(){
        var closeBtn = document.getElementById("closeBtn");
        var imgModule = document.getElementById("imgModule");
        
        closeBtn.onclick = function(){
            imgModule.style.display = "none"; 
        }
    };
    //封装获取元素函数
    LightBox.prototype.getByClass = function(oParent, oClass){
        var oEle = oParent.getElementsByTagName("*");
        var oResult = [];

        for(var i = 0; i < oEle.length; i++){
            if(oEle[i].className == oClass){
                oResult.push(oEle[i]);
            }
        }
        return oResult;
    };
    window.LightBox = LightBox;
})();

new LightBox({
    imgListParent: "imgDefault",
    imgItemClass: "imgItem",
    isShowPage: true
});
</script>
评论2
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 3007547952

  • 头像 椅子
    06-17 16:44
    u***゜
    看起来不错,可以学习下这个
  • 头像 沙发
    06-17 11:39
    u***0
    感谢分享 正好在学习
1 2