我不确定这是否已经讨论过,我尝试搜索问题列表但找不到与之相关的任何内容。
我需要使用turn.js绑定大型HTML内容。我的问题是,使用转js,我将不得不将HTML拆分为单独的div标签作为页面。是否有一种方法在turn.js中绑定div上的内容,它会根据绑定的内容自动包装到不同的页面?
或者有没有办法知道需要将多少数据绑定到每个页面才能使这个场景有效。
答案 0 :(得分:4)
这是一个将内容拆分为页面的解决方案,而不是使用turn.js创建一本书。
此解决方案的逻辑是检查下一个内容是否可以在当前页面中,或者我们需要创建一个新页面并将内容放在那里。
此代码"阅读"来自特定div的html并做魔术;)
另外,您可以使用jsbin中的代码。
var width = 400,
height = 400,
padding = 20;
// create a tester div. in this `div` we will put the contents and check if we need a new page
var div = $('<div />').css({
// divide it by 2 because each page is half from the book
width: width / 2
}).appendTo(document.body);
var index = 0;
var pages = [];
// get all content from the input html
var contents = $('#text').contents();
while (index < contents.length) {
var content = contents.eq(index).clone();
div.append(content);
// check whether the contents exceed the page, if so, remove this content from the page
if (div.height() > height) {
content.remove();
// create a new page
pages.push(div.clone());
// reset the tester div's html to check the next content
div.html('');
}
// if this is the last content, push it to a new page and break
else if (index == contents.length - 1) {
pages.push(div.clone());
div.remove();
break;
}
// go to the next content
else {
index++;
}
}
var book = $('#book');
for (var i = 0; i < pages.length; i++) {
//book.after(pages[i].clone());
//book.after('<hr />');
book.append(pages[i]);
}
// init the plugin
book.turn({
width: width,
height: height,
gradients: true,
acceleration: true
});
&#13;
.sample-flipbook .page {
line-height:1 !important;
font-size:inherit !important;
}
&#13;
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="http://www.turnjs.com/lib/turn.min.js"></script>
<link href="http://www.turnjs.com/samples/basic/css/basic.css" rel="stylesheet" />
<div id="book" class="sample-flipbook"></div>
<div id="text">
<p>Lorem ipsum dolor sit amet, sed probatus dissentias cu. Ex liber error vim. Habeo mollis cu qui, eu cum graeco scripta nostrum, est et delenit suscipit. Eius meliore iudicabit per in, pro numquam fabellas id.
</p>
<p>blablabla</p>
<p>blablabla1</p>
<p>blablabla2</p>
<p>blablabla4</p>
<p>blablabla5</p>
<p>blablabla6</p>
<p>blablabla7</p>
<p>
Ponderum gubergren adversarium pri ad. Mea ne veri scribentur. Nam populo conclusionemque te. Ad albucius voluptatum vix, cum id dicta facilis petentium. His no rebum vivendo. Per esse illum nihil eu, eos affert ceteros ne.
</p>
<p>
At mea nostro oportere reprimique. Vim veri facilisi deterruisset in, maiorum referrentur id mea. Vel eligendi euripidis ullamcorper eu. Vix eu veri primis sententiae, sumo eligendi conclusionemque ad his. Ea quando luptatum rationibus eam, et dico aliquid eloquentiam his. Mea primis intellegebat ne, ea regione equidem ullamcorper usu.
</p><p>
Mel in natum recusabo aliquando, tollit probatus qui in. Ex labore pertinax oportere ius, nobis iudico cu quo. Malis dicunt moderatius eum ex, quaeque consetetur duo ne. Ea veri inimicus mei, vim eu constituam consequuntur.
</p>
<p>
Per id ancillae efficiantur. Eam platonem recteque euripidis et. Usu tota dolore tibique id, id libris molestiae mel. Cu odio illud appareat mei. Quis vitae ne usu. Ut eos magna prima.</p>
<p>Lorem ipsum dolor sit amet, sed probatus dissentias cu. Ex liber error vim. Habeo mollis cu qui, eu cum graeco scripta nostrum, est et delenit suscipit. Eius meliore iudicabit per in, pro numquam fabellas id.
</p>
<p>
Ponderum gubergren adversarium pri ad. Mea ne veri scribentur. Nam populo conclusionemque te. Ad albucius voluptatum vix, cum id dicta facilis petentium. His no rebum vivendo. Per esse illum nihil eu, eos affert ceteros ne.
</p>
<p>
At mea nostro oportere reprimique. Vim veri facilisi deterruisset in, maiorum referrentur id mea. Vel eligendi euripidis ullamcorper eu. Vix eu veri primis sententiae, sumo eligendi conclusionemque ad his. Ea quando luptatum rationibus eam, et dico aliquid eloquentiam his. Mea primis intellegebat ne, ea regione equidem ullamcorper usu.
</p><p>
Mel in natum recusabo aliquando, tollit probatus qui in. Ex labore pertinax oportere ius, nobis iudico cu quo. Malis dicunt moderatius eum ex, quaeque consetetur duo ne. Ea veri inimicus mei, vim eu constituam consequuntur.
</p>
<p>
Per id ancillae efficiantur. Eam platonem recteque euripidis et. Usu tota dolore tibique id, id libris molestiae mel. Cu odio illud appareat mei. Quis vitae ne usu. Ut eos magna prima.</p>
</div>
&#13;
说这个解决方案是你的方向很重要。您最需要添加更多代码才能完全符合您的需求,但我最大程度上为您做了。
答案 1 :(得分:1)
根据文档(http://www.turnjs.com/docs/Method:_addPage),您可以添加dinamically页面。因此,当页面加载时,您可以自定义拆分页面并以dinamically方式添加它们。这样的事情。
想象一下 HTML :
<div class="page">
<h2>Title</h2>
<p>Content</p>
</div>
<div class="page">
<h2>Title</h2>
<p>Content</p>
</div>
<div class="page">
<h2>Title</h2>
<p>Content</p>
</div>
<div class="page">
<h2>Title</h2>
<p>Content</p>
</div>
<div id="flipbook"></div>
我们这里有4个页面,但是所有页面都在同一页面中,最后一个元素是构建flipbook的地方。因此,当我们加载页面时,让我们将它们拆分为 Javascript :
$(document).ready(function() {
// initialize the flipbook
$("#flipbook").turn({page: 1, acceleration: true, gradients: true});
// first we declare an array to store pages contents
var pagesArray = [];
$('.page').each(function() {
// iterate through all pages and store the HTML inside
pagesArray.push($(this).html());
});
// iterate the array and add pages dinamically
for(var i in pagesArray) {
// add the element within a div with the content that store before
var element = $("<div />").html(pagesArray[i]);
// add the page with the counter + 1 (first page is 1 not 0)
$("#flipbook").turn("addPage", element, (i+1));
}
});
就是这样。
告诉我们这是否符合您的要求。如果没有,请与我们分享您的html结构,以查看如何解决问题。