@WebforSelf

Почему не работает скрипт scale.js?

Для проекта потребовалось чтоб блок уменьшался пропорционально на мониторе, подключил скрипт scale.js

С одним блоком работает нормально, но если подключать конструкцией ниже, то scaleable-wrapper1 уже не работает,
Как подключить чтоб оба блока работали. Получается эти скрипты у меня находятся в custom.js подключаю блоки отдельно на страницах , по одному.

Uncaught Error: null element
    at PageScaleJs (scale.js?ver=1.1.3:19)
    at custom.js?ver=1.1.1:54


Получается если захожу на страницу где расположен только scaleable-wrapper1, то он ругается что scaleable-wrapper
null

Ругается на этот if
function PageScaleJs(el, options) {
    if (!el) {
      throw new Error("null element");
    }


Cам scale.js
spoiler

/* eslint wrap-iife: 0, no-var: 0 */
/* global define */
(function () {
  var prefix = ["", "O", "ms", "Webkit", "Moz"];
  function transform(el, param) {
    for (var i = 0, iLen = prefix.length; i < iLen; i++) {
      var prop = prefix[i] + "transform";
      el.style[prop] = param || "";
    }
  }

  function setSize(el, width, height) {
    el.style.width = width ? "" + width + "px" : "";
    el.style.height = height ? "" + height + "px" : "";
  }

  function PageScaleJs(el, options) {
    if (!el) {
      throw new Error("null element");
    }
    if (el === document.body) {
      throw new Error("element mustn't be body elements");
    }
    if (el.parentNode === document.body) {
      throw new Error("element must be wrapped by container");
    }
    if (!(this instanceof PageScaleJs)) {
      return new PageScaleJs(el, options);
    }
    this.init(el, options);
    return this;
  }

  PageScaleJs.prototype.init = function (el, options) {
    var opts = options || {};
    this.sizes = [];
    this.el = el;
    this.className = opts.className || "page-scale-js";
    if (opts.width && opts.height) {
      this.addRange(opts.width, opts.height, { from: opts.from, to: opts.to });
    }
    if (opts.ranges) {
      for (var i = 0, iLen = opts.ranges.length; i < iLen; i++) {
        var item = opts.ranges[i];
        this.addRange(item.width, item.height, {
          from: item.from,
          to: item.to,
        });
      }
    }
    var handleId;
    var self = this;
    var resize = function () {
      handleId = null;
      var scale = self.onResize();
      opts.onChangeScale && opts.onChangeScale(scale);
    };
    this._onResize = function () {
      !handleId && clearTimeout(handleId);
      handleId = setTimeout(resize, opts.timeout || 500);
    };
    window.addEventListener("resize", this._onResize);
    resize();
  };

  PageScaleJs.prototype.addRange = function (width, height, opts) {
    if (!width || !height) {
      throw new Error("Needs correct arguments width adn height");
    }
    opts || (opts = {});
    this.sizes.push({
      from: opts.from || width,
      to: opts.to,
      width: width,
      height: height,
    });
  };

  PageScaleJs.prototype.getSizeItem = function () {
    var w = window.innerWidth;
    for (var i = 0, item = null, iLen = this.sizes.length; i < iLen; i++) {
      item = this.sizes[i];
      if (item.from >= w) {
        if (!item.to || item.to <= w) {
          return {
            index: i,
            width: typeof item.width === "function" ? item.width() : item.width,
            height:
              typeof item.height === "function" ? item.height() : item.height,
          };
        }
      }
    }
  };

  PageScaleJs.prototype.destroy = function () {
    window.removeEventListener("resize", this._onResize);
  };

  PageScaleJs.prototype.onResize = function () {
    var item = this.getSizeItem();
    var parentNode = this.el.parentNode;
    if (item) {
      var fixWidth = item.width;
      var fixHeight = item.height;
      var scale = window.innerWidth / fixWidth;
      var height = fixHeight * scale;
      var ratio = fixHeight / fixWidth;
      if (height < window.innerHeight) {
        height = window.innerHeight;
        ratio = window.innerHeight / (fixWidth * scale);
      }

      var x = (fixWidth - window.innerWidth) / -2;
      var y = x * ratio;
      transform(
        this.el,
        "scale(" +
          scale +
          ") translate(" +
          x / scale +
          "px, " +
          y / scale +
          "px)"
      );
      setSize(this.el, fixWidth, height / scale);
      setSize(parentNode, fixWidth * scale, height);
      if (this.className) {
        parentNode.classList.add(this.className);
        parentNode.setAttribute("data-page-scale", item.index);
      }
      return scale;
    } else {
      transform(this.el);
      setSize(this.el);
      setSize(parentNode);
      if (this.className) {
        parentNode.classList.remove(this.className);
        parentNode.removeAttribute("data-page-scale");
      }
      return 1;
    }
  };

  if (typeof exports !== "undefined") {
    if (typeof module !== "undefined" && module.exports) {
      exports = module.exports = PageScaleJs;
    }
    exports.PageScaleJs = PageScaleJs;
  } else if (typeof define === "function" && define.amd) {
    define("PageScaleJs", [], function () {
      return PageScaleJs;
    });
  } else {
    window.PageScaleJs = PageScaleJs;
  }
})(this);



PageScaleJs(document.getElementById("scaleable-wrapper"), {
  width: 2000, //original width of you site-layer (like original width of image)
  height: 2600, //original heigth of you site-layer (like original height of image)
  //   from: 1200, //start width interval (by default equal width parametr)
  //   to: 1000, //end width interval (by default equal 0)
  onChangeScale: function (scale) {
    //receive value of scale factor
  },
});

PageScaleJs(document.getElementById("scaleable-wrapper1"), {
  width: 2000, //original width of you site-layer (like original width of image)
  height: 1600, //original heigth of you site-layer (like original height of image)
  //   from: 1200, //start width interval (by default equal width parametr)
  //   to: 1000, //end width interval (by default equal 0)
  onChangeScale: function (scale) {
    //receive value of scale factor
  },
});
  • Вопрос задан
  • 65 просмотров
Пригласить эксперта
Ответы на вопрос 1
Seasle
@Seasle Куратор тега JavaScript
Может потому, что у Вас нету элемента, соответствующего селектору #scaleable-wrapper1?
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы