2

i want to make turn.js use class insted on id the below fiddle uses id so am trying with a class

fiddle :- http://jsfiddle.net/kmturley/GGea5/1/

i tried with this

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];
      }
    }

that plugin works if i use return document.getElementsByClassName(id)[0]; but was not able to do with this return document.getElementsByClassName(id)[i] so that it applies to multiple having the same class

/*
 * 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');
}());
html,
body {
  height: 100%;
}
.book {
  width: 50%;
  height: 50%;
}
.book img {
  width: 100%;
  height: 100%;
}
<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>

Please let me know if i am going in the right direction or is there any other better way to do this as i am not expert in JavaScript

Vitorino fernandes
  • 15,794
  • 3
  • 20
  • 39
  • `return document.getElementsByClassName(ele);` ? – Royi Namir May 02 '15 at 08:39
  • What exactly should be returned? All the selected elements? If yes, why don't you return the collection? – Ram May 02 '15 at 08:41
  • @Vohuman i have updated the question please check and do give me any suggestions in which direction i should go – Vitorino fernandes May 02 '15 at 09:14
  • Now it's clear what you are you trying to achieve! Some parts of the module should be rewritten so it will work with 1 or more elements. Luckily you are using jQuery and you can do it easily. `this.el` should be changed to something like `this.collection = $(selector);` and `resize` method should iterate through the collection. I'll reopen the question. – Ram May 02 '15 at 09:33

1 Answers1

1

Each function only returns once. If the function should return several values, you can return an object or an array.

The module works with only 1 element. Some parts of the module should be rewritten so it will work with 1 or more elements. You are using jQuery and the modification can be done easily.

(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.

Note that I have used the .each() method for iterating through the collection and calling the turn method as it seems the plugin only use the first element of the current collection.

Ram
  • 143,282
  • 16
  • 168
  • 197