我想让turn.js在id上使用class,下面的小提琴使用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的多个
/*
* 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>
如果我是朝着正确的方向走,请告诉我,或者有其他更好的方法来做这件事,因为我不是JavaScript的专家
发布于 2015-05-02 09:46:57
每个函数只有return一次。如果函数应该返回几个值,则可以返回一个对象或一个数组。
该模块只使用一个元素。模块的某些部分应该重写,这样它就可以与一个或多个元素一起工作。您正在使用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');
}());这是一个演示。
注意,我使用了.each()方法来迭代集合并调用turn方法,因为插件似乎只使用当前集合的第一个元素。
https://stackoverflow.com/questions/30000171
复制相似问题