make turn.js使用id的类

时间:2015-05-02 08:37:45

标签: javascript jquery

我希望turn.js使用classid以下小提琴使用id上进行操作,所以我正在尝试使用class

小提琴: - http://jsfiddle.net/kmturley/GGea5/1/

我试过这个

function cls() {
      var size = document.getElementsByClassName(id).length;
      console.log(size)
      for (var i = 0; i < size; i++) {
        //return document.getElementsByClassName(id)[i];
        return document.getElementsByClassName(id)[0];
      }
    }

如果我使用return document.getElementsByClassName(id)[0];但该插件无法使用return document.getElementsByClassName(id)[i],则该插件可以正常工作,因此适用于具有相同class的多个

&#13;
&#13;
/*
 * Turn.js responsive book
 */

/*globals window, document, $*/

(function() {
  'use strict';

  var module = {
    ratio: 1.38,
    init: function(id) {
      var me = this;

      // if older browser then don't run javascript
      if (document.addEventListener) {
        function cls() {
          var size = document.getElementsByClassName(id).length;
          console.log(size)
          for (var i = 0; i < size; i++) {
            //return document.getElementsByClassName(id)[i];
            return document.getElementsByClassName(id)[0];

          }

        }
        this.el = cls();
        this.resize();
        this.plugins();

        // on window resize, update the plugin size
        window.addEventListener('resize', function(e) {
          var size = me.resize();
          $(me.el).turn('size', size.width, size.height);
        });
      }
    },
    resize: function() {
      // reset the width and height to the css defaults
      this.el.style.width = '';
      this.el.style.height = '';

      var width = this.el.clientWidth,
        height = Math.round(width / this.ratio),
        padded = Math.round(document.body.clientHeight * 0.9);

      // if the height is too big for the window, constrain it
      if (height > padded) {
        height = padded;
        width = Math.round(height * this.ratio);
      }

      // set the width and height matching the aspect ratio
      this.el.style.width = width + 'px';
      this.el.style.height = height + 'px';

      return {
        width: width,
        height: height
      };
    },
    plugins: function() {
      // run the plugin
      $(this.el).turn({
        gradients: true,
        acceleration: true
      });
    }
  };

  module.init('book');
}());
&#13;
html,
body {
  height: 100%;
}
.book {
  width: 50%;
  height: 50%;
}
.book img {
  width: 100%;
  height: 100%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://www.turnjs.com/lib/turn.min.js"></script>

<div class="book">
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/01.jpg" alt="" />
  </div>
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/02.jpg" alt="" />
  </div>
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/03.jpg" alt="" />
  </div>
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/04.jpg" alt="" />
  </div>
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/05.jpg" alt="" />
  </div>
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/06.jpg" alt="" />
  </div>
</div>

<div class="book">
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/01.jpg" alt="" />
  </div>
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/02.jpg" alt="" />
  </div>
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/03.jpg" alt="" />
  </div>
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/04.jpg" alt="" />
  </div>
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/05.jpg" alt="" />
  </div>
  <div class="page">
    <img src="https://raw.github.com/blasten/turn.js/master/demos/magazine/pages/06.jpg" alt="" />
  </div>
</div>
&#13;
&#13;
&#13;

如果我正朝着正确的方向前进,或者还有其他更好的方法可以让我知道,因为我不是JavaScript专家

1 个答案:

答案 0 :(得分:1)

每个功能只有return一次。如果函数应返回多个值,则可以返回一个对象或数组。

该模块仅使用1个元素。应该重写模块的某些部分,以便它可以使用1个或更多元素。您正在使用jQuery,并且可以轻松完成修改。

(function () {
    'use strict';

    var module = {
        ratio: 1.38,
        init: function (selector) {
            var me = this;
            this.collection = $(selector);
            this.plugins();
            $(window).on('resize', function (e) {
                me.collection.each(function () {
                    var size = me.resize(this);
                    $(this).turn('size', size.width, size.height);
                });
            }).resize();
        },
        resize: function (el) {
            // reset the width and height to the css defaults
            el.style.width = '';
            el.style.height = '';

            var width = el.clientWidth,
                height = Math.round(width / this.ratio),
                padded = Math.round(document.body.clientHeight * 0.9);

            // if the height is too big for the window, constrain it
            if (height > padded) {
                height = padded;
                width = Math.round(height * this.ratio);
            }

            // set the width and height matching the aspect ratio
            el.style.width = width + 'px';
            el.style.height = height + 'px';

            return {
                width: width,
                height: height
            };
        },
        plugins: function () {
            // run the plugin
            this.collection.each(function () {
                $(this).turn({
                    gradients: true,
                    acceleration: true
                });
            });
            // hide the body overflow
            document.body.className = 'hide-overflow';
        }
    };

    module.init('.book');
}());

Here is a demo.

请注意,我使用.each()方法迭代集合并调用turn方法,因为插件似乎只使用当前集合的第一个元素。