简介 目录 评价 推荐
  • 破茧而出 2017-06-24
    为什么我在IE浏览器下连图片都出来不了呢?
    已采纳 qq_MJ_7 的回答

    估计是你的jquery版本太高了,2.0以上的版本不兼容IE8以下的

    1回答·1524浏览
  • lisa_0025 2016-11-22
    问什么在IE7 下页面会报错,ie8,9,10 都正常?

    是不是你的JQ版本太低?

    1回答·1458浏览
  • 冰诺bn 2016-09-22
    求源码 谢谢了
    (function ($) {
    
      var LightBox = function () {
        var self = this;
    
        this.popupMask = $("<div id='mask'></div>");
        this.popupWin = $("<div id='popup'></div>");
        this.bodyNode = $(document.body);
    
        // 组名及数据变量
        this.groupName = null;
        this.groupData = [];
    
        this.renderDOM();
    
        // 获取弹出框区域
        this.picView = this.popupWin.find(".img"); // 图片区域
        this.pic = this.picView.find("img");
        this.prevBtn = this.picView.find(".btn-prev");
        this.nextBtn = this.picView.find(".btn-next");
        this.loader = this.picView.find(".img-loader");
        this.picInfo = this.popupWin.find(".info"); // 标题区域
        this.picCaption = this.picInfo.find(".info-des");
        this.picIndex = this.picInfo.find(".info-index");
        this.closeBtn = this.picInfo.find(".close");
    
        this.b_state = true;
        this.timer = null;
    
        // 事件委托
        this.bodyNode.delegate(".js_lightbox", "click", function (ev) {
          ev.stopPropagation();
    
          var curGroupName = $(this).attr("data-group");
    
          if (curGroupName != self.groupName) {
            self.groupName = curGroupName;
    
            self.getGroup();
          }
    
          self.intialPop($(this));
        });
    
        // 点击遮罩及关闭按钮时
        this.popupMask.click(function () {
          $(this).fadeOut();
          self.popupWin.fadeOut();
        });
    
        this.closeBtn.click(function () {
          self.popupMask.fadeOut();
          self.popupWin.fadeOut();
        });
    
    
        // 左右按钮事件
        this.prevBtn.hover(function () {
          if (!$(this).hasClass("disabled") && self.groupData.length > 1) {
            $(this).addClass("btn-prev__show");
          }
        }, function () {
          if (!$(self).hasClass("disabled") && self.groupData.length > 1) {
            $(this).removeClass("btn-prev__show");
          }
        }).click(function (ev) {
          if (!$(this).hasClass("disabled") && self.b_state) {
            self.b_state = false;
    
            ev.stopPropagation();
    
            self.goTo("prev");
          }
        });
    
        this.nextBtn.hover(function () {
          if (!$(this).hasClass("disabled") && self.groupData.length > 1) {
            $(this).addClass("btn-next__show");
          }
        }, function () {
          if (!$(self).hasClass("disabled") && self.groupData.length > 1) {
            $(this).removeClass("btn-next__show");
          }
        }).click(function (ev) {
          if (!$(this).hasClass("disabled") && self.b_state) {
            self.b_state = false;
    
            ev.stopPropagation();
    
            self.goTo("next");
          }
        });
    
    
        // 根据窗口调整图片大小
        $(window).resize(function () {
          window.clearTimeout(self.timer);
    
          self.timer = setTimeout(function () {
            self.picLoadSize(self.groupData[self.zIndex].src);
          }, 500);
        });
      };
    
      LightBox.prototype = {
        goTo: function (direction) { // 左右切换图片
          if (direction === "next") {
            this.zIndex ++;
    
            if (this.zIndex === this.groupData.length - 1) {
              this.nextBtn.addClass("disabled").removeClass("btn-next__show");
            }
    
            if (this.zIndex > 0) {
              this.prevBtn.removeClass("disabled");
            }
    
            var imgURL = this.groupData[this.zIndex].src;
    
            this.picLoadSize(imgURL);
          } else {
            this.zIndex --;
    
            if (this.zIndex === 0) {
              this.prevBtn.addClass("disabled").removeClass("btn-next__show");
            }
    
            if (this.zIndex != this.groupData.length - 1) {
              this.nextBtn.removeClass("disabled");
            }
    
            var imgURL = this.groupData[this.zIndex].src;
    
            this.picLoadSize(imgURL);
          }
        },
    
        picLoadSize: function (picURL) { // 加载图片并获取尺寸
          var self = this;
    
          this.pic.css({width: "auto", height: "auto"}).hide();
          this.picInfo.hide();
    
          this.preLoadImg(picURL, function () {
            self.pic.attr("src", picURL);
    
            var picWidth = self.pic.width();
            var picHeight = self.pic.height();
    
            self.changeImg(picWidth, picHeight);
          });
        },
    
        changeImg: function (picWidth, picHeight) { // 改变各项值及显示图片
          var self = this;
          var clietnWidth = $(window).width();
          var clietnHeight = $(window).height();
    
          var scale = Math.min(clietnWidth / picWidth, clietnHeight / picHeight, 1);
    
          picWidth = picWidth * scale;
          picHeight = picHeight * scale;
    
          this.picView.animate({
            width: picWidth,
            height: picHeight
          });
    
          this.popupWin.animate({
            width: picWidth,
            height: picHeight,
            marginLeft: -(picWidth / 2),
            top: (clietnHeight - picHeight) / 2
          }, function () {
            self.pic.css({
              width: picWidth,
              height: picHeight
            }).fadeIn();
    
            self.picInfo.fadeIn();
    
            self.b_state = true;
          });
    
          this.picIndex.text("Image "+(this.zIndex + 1)+" of "+this.groupData.length+"");
          this.picCaption.text(this.groupData[this.zIndex].caption);
        },
    
        preLoadImg: function (picURL, fn) { // 图片加载函数
          var oImg = new Image();
    
          if (!!window.ActiveXObject) {
            oImg.onreadystatechage = function () {
              if (this.readyState == "complete") {
                fn();
              }
            };
          } else {
            oImg.onload = function () {
              fn();
            };
          }
    
          oImg.src = picURL;
        },
    
        showMaskAndPop: function (src, curId) { // 显示弹窗部分区域
          var self = this;
          var winWidth = $(window).width();
          var winHeight = $(window).height();
          var viewHeight = winHeight / 2;
    
          this.pic.hide();
          this.picInfo.hide();
    
          this.popupMask.fadeIn();
    
          this.picView.css({
            width: winWidth / 2,
            height: winHeight / 2
          });
    
          this.popupWin.fadeIn();
          this.popupWin.css({
            width: winWidth / 2,
            height: winHeight / 2,
            marginLeft: -(winWidth / 2) / 2,
            top: -viewHeight
          }).animate({
            top: (winHeight - viewHeight) / 2
          }, function () {
            self.picLoadSize(src);
          });
    
    
          // 存下当前元素在数组中的位置
          this.zIndex = this.getIndex(curId);
    
          var dataLen = this.groupData.length;
    
          if (dataLen > 1) {
            if (this.zIndex === 0) {
              this.prevBtn.addClass("disabled");
              this.nextBtn.removeClass("disabled");
            } else if (this.zIndex == dataLen - 1) {
              this.prevBtn.removeClass("disabled");
              this.nextBtn.addClass("disabled");
            } else {
              this.prevBtn.removeClass("disabled");
              this.nextBtn.removeClass("disabled");
            }
          }
        },
    
        intialPop: function (curObj) { // 初始化弹窗
          var sourceSrc = curObj.attr("data-source");
          var curId = curObj.attr("data-id");
    
          this.showMaskAndPop(sourceSrc, curId);
        },
    
        getGroup: function () { // 同一组数据获取
          var self = this;
          var groupList = this.bodyNode.find("*[data-group="+this.groupName+"]");
    
          this.groupData.length = 0;
    
          groupList.each(function () {
            self.groupData.push({
              src: $(this).attr("data-source"), 
              id: $(this).attr("data-id"),
              caption: $(this).attr("data-caption")
            });
          });
        },
    
        renderDOM: function () { // 渲染 DOM
          var str = '<div class="container">\
              <a class="img" href="#">\
                <img src="#" alt="#">\
                <span class="btn btn-prev"></span>\
                <span class="btn btn-next"></span>\
              </a>\
              <div class="info">\
                <span class="info-des">Click the right half of the image to move forward.</span>\
                <span class="info-index">Image 0 of 0</span>\
                <a href="#" class="close"></a>\
              </div>\
            </div>';
    
          this.popupWin.html(str);
    
          this.bodyNode.append(this.popupMask);
          this.bodyNode.append(this.popupWin);
        },
    
        getIndex: function (curId) { // 存下当前元素在数组中的位置
          var index = 0;
    
          $(this.groupData).each(function (i) {
            if (this.id === curId) {
              return false;
            } else {
              index ++;
            }
          });
    
          return index;
        }
      };
    
      window["LightBox"] = LightBox;
    
    })(jQuery);


    1回答·1665浏览
  • Sweet_Gao 2016-04-27
    IE 8-10 图片加载不出来
    已采纳 Ateem 的回答

    (-。-;)额这个怎么调试?一大堆看着犯晕。。。代码看过去好像没啥问题,readystate中”S“应该大写,不过应该不是这个问题,你看一下报错信息,自己调试一下,如果没有报错信息,可能跟图片的先后加载浏览器的渲染有关,解决了望告知下,菜鸟能力有限没帮上忙,不好意思啦,哦,还有提个小建议:把自己这些代码放在github或者codepen这类代码托管上,方便大家帮忙调试吧

    5回答·2591浏览
  • com990ok 2016-01-27
    遮罩层存在时,window.resize才执行,这方法是否可行?

    何必这么麻烦

    1回答·1575浏览
  • Timon58 2015-11-20
    IE8下不能正常使用

    Jquery2.0以上版本不再支持IE8及以下版本

    2回答·1677浏览
  • elaine2 2015-09-22
    IE下bug

    已解决,需要.png格式的图片,练习时找的一张jpg的图片~~~

    1回答·1524浏览
  • blackrian 2015-08-11
    keyup问题

    具体描述下呢,下面课程应该给解决掉了

    2回答·1469浏览
  • WaTer9527 2015-08-01
    加了DD_belatedPNG后,IE6向上或向下切换只能点击一次
    已采纳 一只帅蚂蚁 的回答

    好,跟踪看下。。

    1回答·1508浏览
  • 轻鸿 2015-07-31
    0回答·1016浏览
数据加载中...
开始学习 免费