利用JS实现点击小图弹出大图代码



42 165 56



特效描述:利用JS实现 点击小图 弹出大图代码,利用JS实现点击小图弹出大图代码

代码结构

1. HTML代码

<center><br><br><br>
<div id="imgDefault">
    <img class="imgItem" data-src="images/tooopen_sy_209068589482.jpg" src="images/photoA.jpg">
    <img class="imgItem" data-src="images/tooopen_sy_241631031241.jpg" src="images/photoB.jpg">
    <img class="imgItem" data-src="images/tooopen_sy_241630585832.jpg" src="images/photoC.jpg">
    <img class="imgItem" data-src="images/tooopen_sy_241629694524.jpg" src="images/photoD.jpg">
</div>
</center>
<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>
<div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
</div>



用户评论
大牛,别默默的看了,快登录帮我点评一下吧!:)      登录 | 注册


热门标签: 右键菜单 缩略图 文字提示框 图片相册 提示框/弹出层 图片墙 layer 百叶窗 lightbox 图片网格 图片九宫格 Tooltip工具提示框 浮动提示框 图片高亮 html5弹窗动画 图片对比 图片文字 单排图片 响应式图片 图片列表 图片拖动 图片放大镜 图片叠加 图片翻转旋转 图片延迟加载 图片淡出淡进 图片全屏 图片头像上传 二维码 图片广告 图片放大缩小 html5图片动画 带缩略图的幻灯片 地图 图片滚动 图片滑动 图片切换 图片收缩展开 旋转木马 图片轮播 自动滚动图片轮播 图标导航菜单 弹出层拖动 背景切换 窗口提示框 手风琴 图表 图片 头像截图

×
×

注册

官方QQ群

扫描上面二维码加微信群

官方QQ群

jQuery/js讨论群
群号:642649996
Css3+Html5讨论群
群号:322131262

加群请备注:从官网了解到