我创建了一个基本的turn.js flipbook。当我单击链接元素(#clickme
)时,我想模拟屏幕动画,其中用户在剥离时单击(mousedown
)并在翻页后释放(mouseup
)。到目前为止,这是我的代码:
$("#flipbook").turn({
width: 700*2,
height: 938,
'display': 'double',
duration: 3000
});
//Create two empty span elements over the peel start and end points
var rect = document.getElementById("flipbook").getBoundingClientRect();
$("#flipStart").css("top", rect.bottom - 50);
$("#flipStart").css("left", rect.right - 50);
$("#flipEnd").css("top", rect.bottom - 50);
$("#flipEnd").css("left", rect.left);
//Simulate the flip with click
$("#clickme").click(function() {
var targetNode1 = document.getElementById("flipStart");
var targetNode2 = document.getElementById("flipEnd");
if (targetNode1) {
triggerMouseEvent (targetNode1, "mouseover");
triggerMouseEvent (targetNode1, "mousedown");
}
if (targetNode2) {
triggerMouseEvent (targetNode2, "mouseover");
triggerMouseEvent (targetNode2, "mouseup");
triggerMouseEvent (targetNode2, "click");
}
else
console.log ("*** Target node not found!");
function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);
}
});
当我运行时,没有任何事情发生。我认为mousedown
事件没有被触发但mouseup
确实触发了。任何人都知道我做错了什么?
这是HTML:
<body style="overflow: hidden">
<div id="clickme"><a href="#">Click</a></div>
<div id="flipbook" style="margin: auto; position: absolute">
<div id="page1"><p>Page 1</p></div>
<div id="page2"><p>Page 2</p></div>
<div id="page3"><p>Page 3</p></div>
<div id="page4"><p>Page 4</p></div>
<div id="page5"><p>Page 5</p></div>
</div>
<div id="flipStart" style="position:absolute"><span style="display:block; width: 50px; height: 50px; margin: auto; z-index: 90;"></span> </div>
<div id="flipEnd" style="position: absolute"><span style="display: block; width: 50px; height: 50px; margin: auto; z-index: 90"></span> </div>
</body>
答案 0 :(得分:2)
关于您的代码的一些建议:
现在,关于你的要求,我不确定你在尝试使用该代码,但是如果你&#34; want to simulate a manual page flip when I click on a link element
&#34;您可能需要查看Turn.js的next,previous和peel方法。这是一个有效的代码和一个JSFiddle来帮助您入门:
<强> HTML 强>
<div id="flipbook">
<div class="hard"> Turn.js </div>
<div class="hard"></div>
<div> Page 1 </div>
<div> Page 2 </div>
<div> Page 3 </div>
<div> Page 4 </div>
<div class="hard"></div>
<div class="hard"></div>
</div>
<div id="controls">
<a id="previous" href="#">Previous</a>
<a id="next" href="#">Next</a>
</div>
<强> JS 强>
$("#flipbook").turn({
width: 400,
height: 300
});
var num_pages = $("#flipbook").turn("pages");
$("#previous").mousedown(function(){
var page = $("#flipbook").turn("page");
if(page == 2 || page == 3 || page == num_pages){
$("#flipbook").turn("peel", "l");
}
else{
$("#flipbook").turn("peel", "tl");
}
}).mouseup(function(){
$("#flipbook").turn("previous");
});
$("#next").mousedown(function(){
var page = $("#flipbook").turn("page");
if(page == 1 || page == num_pages - 2 || page == num_pages - 1){
$("#flipbook").turn("peel", "r");
}
else{
$("#flipbook").turn("peel", "tr");
}
}).mouseup(function(){
$("#flipbook").turn("next");
});
<强> CSS 强>
body{
overflow:hidden;
background-color:#D5E8EA;
}
#flipbook{
width:400px;
height:300px;
margin: auto;
position: absolute
}
#flipbook .page{
width:400px;
height:300px;
background-color:#DED7D7;
line-height:300px;
font-size:20px;
text-align:center;
}
#flipbook .hard{
background:#ccc !important;
font-weight:bold;
}