0

我正在尝试稍微更改插件,这是原型功能,我已经标记了我添加的行。

好的,所以我的问题发生在我添加的 img-load 函数中。我已经用一个包围了旧代码,以确保脚本等到图像加载完毕。问题是,加载函数内部的“this”与外部的“this”没有连接。我试过给加载函数一个参数,但显然它不起作用或者我做错了什么。

你知道一种简单的方法来继承“这个”吗?我真的不知道还能做什么。

Plugin.prototype._fade = function(number) {
  var $element, currentSlide, next, slidesControl, value,
    _this = this;
  $element = $(this.element);
  this.data = $.data(this);
  if (!this.data.animating && number !== this.data.current + 1) {
    $.data(this, "animating", true);
    currentSlide = this.data.current;
    if (number) {
      number = number - 1;
      value = number > currentSlide ? 1 : -1;
      next = number;
    } else {
      value = this.data.direction === "next" ? 1 : -1;
      next = currentSlide + value;
    }
    if (next === -1) {
      next = this.data.total - 1;
    }
    if (next === this.data.total) {
      next = 0;
    }

    this._setActive(next);
    slidesControl = $(".slidesjs-control", $element);



    var nxtImg = $(slidesControl.children(":eq(" + next + ")")).find("img:eq(0)"); // added
    if ( nxtImg.attr("longdesc") !== undefined ) {  // added
      nxtImg.attr("src", nxtImg.attr("longdesc")); // added
      nxtImg.removeAttr("longdesc"); // added
    } // added


    nxtImg.load(function(){ // added
      slidesControl.children(":eq(" + next + ")").css({
        display: "block",
        left: 0,
        zIndex: 0
      });
      this.options.callback.start(currentSlide + 1);
      if (this.options.effect.fade.crossfade) {
        return slidesControl.children(":eq(" + this.data.current + ")").stop().fadeOut(this.options.effect.fade.speed, (function() {
          slidesControl.children(":eq(" + next + ")").css({
            zIndex: 10
          });
          $.data(_this, "animating", false);
          $.data(_this, "current", next);
          return _this.options.callback.complete(next + 1);
        }));
      } else {
        slidesControl.children(":eq(" + next + ")").css({
          display: "none"
        });
        return slidesControl.children(":eq(" + currentSlide + ")").stop().fadeOut(this.options.effect.fade.speed, (function() {
          slidesControl.children(":eq(" + next + ")").stop().fadeIn(_this.options.effect.fade.speed).css({
            zIndex: 10
          });
          $.data(_this, "animating", false);
          $.data(_this, "current", next);
          return _this.options.callback.complete(next + 1);
        }));
      }
    }); // added



  }
};
4

1 回答 1

1

您在闭包变量“_this”中捕获 this。这不是工作吗?

于 2013-05-14T14:33:17.297 回答