JavaScipt + turn.js 实现图书翻页效果实例
介绍
在网站开发中,我们经常需要将一些电子书或者pdf格式的文档转化为网页形式,同时保留原有的阅读体验,这时候实现图书翻页效果就显得尤为重要。
turn.js 是一个基于 JQuery 的图书翻页插件,通过 turn.js 插件,我们可以方便快捷地实现 Web 端的图书翻页效果。
本文将讲解 turn.js 的使用方式。
实现步骤
- 下载 turn.js:从 GitHub 上下载 turn.js 文件,将 turn.js 和 turn.css 文件上传至服务器,并在页面头部引入 css 和 js 文件。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书翻页效果</title>
<link rel="stylesheet" href="path/to/turn.css">
<script src="path/to/jquery.min.js"></script>
<script src="path/to/turn.js"></script>
</head>
<body>
</body>
</html>
- 组织 HTML 结构:turn.js 将一个页面嵌入在一个 div 元素中,每个页面需要使用一个 div 元素包裹。例如下面的例子:
<div class="magazine-viewport">
<div class="container">
<div class="magazine">
<div>Page 1</div>
<div>Page 2</div>
<div>Page 3</div>
<div>Page 4</div>
</div>
</div>
</div>
- 初始化 turn.js:在页面加载完成后,使用 turn() 函数对元素进行初始化。例如:
$(window).ready(function() {
$('.magazine').turn({
display: 'double',
acceleration: true,
gradients: !$.isTouch,
elevation:50,
when: {
turned: function(e, page) {
console.log('Current view: ', $(this).turn('view'));
}
}
});
});
- 配置选项:通过配置选项可以实现更多自定义的效果。例如:
$('.magazine').turn({
display: 'double',
acceleration: true,
gradients: !$.isTouch,
elevation:50,
when: {
turned: function(e, page) {
console.log('Current view: ', $(this).turn('view'));
}
}
});
效果示例
示例一
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书翻页效果</title>
<link rel="stylesheet" href="http://localhost/jquery-turnjs/css/turn.css">
<script src="http://localhost/jquery-turnjs/js/jquery.min.js"></script>
<script src="http://localhost/jquery-turnjs/js/turn.js"></script>
</head>
<body>
<div class="magazine-viewport">
<div class="container">
<div class="magazine">
<div><img src="http://localhost/jquery-turnjs/img/1.jpg" /></div>
<div><img src="http://localhost/jquery-turnjs/img/2.jpg" /></div>
<div><img src="http://localhost/jquery-turnjs/img/3.jpg" /></div>
<div><img src="http://localhost/jquery-turnjs/img/4.jpg" /></div>
<div><img src="http://localhost/jquery-turnjs/img/5.jpg" /></div>
<div><img src="http://localhost/jquery-turnjs/img/6.jpg" /></div>
</div>
</div>
</div>
<script type="text/javascript">
$(window).ready(function() {
$('.magazine').turn({
display: 'double',
acceleration: true,
gradients: !$.isTouch,
elevation:50,
when: {
turned: function(e, page) {
console.log('Current view: ', $(this).turn('view'));
}
}
});
});
</script>
</body>
</html>
将以上代码复制到一个 html 文件中,将其中的路径信息替换为自己的本地文件路径,然后用浏览器打开该文件即可看到效果。
示例二
```html
var book = $('.magazine');
// Call the plugin
book.turn({
acceleration: true,
autoCenter: true,
gradients: true,
elevation: 50,
duration: 1000,
pages: 16,
when: {
turning: function(e, page, view) {
var book = $(this),
currentPage = book.turn('page'),
pages = book.turn('pages');
// Update the current URI
Hash.go('page/' + page).update();
// Show and hide navigation buttons
disableControls(page);
$('.thumbnails .page-'+currentPage).
parent().
removeClass('current');
$('.thumbnails .page-'+page).
parent().
addClass('current');
},
turned: function(e, page, view) {
disableControls(page);
$(this).turn('center');
if (page==1) {
$(this).turn('peel', 'br');
}
},
missing: function (e, pages) {
for (var i = 0; i < pages.length; i++) addPage(pages[i], $(this)); } }});// Zoom.js$('.magazine-viewport').zoom({ flipbook: $('.magazine'), max: function() { return largeMagazineWidth()/$('.magazine').width(); }, when: { swipeLeft: function() { $(this).zoom('flipbook').turn('next'); }, swipeRight: function() { $(this).zoom('flipbook').turn('previous'); }, resize: function(event, scale, page, pageElement) { if (scale==1) loadSmallPage(page, pageElement); else loadLargePage(page, pageElement); }, zoomIn: function () { $('.thumbnails').hide(); $('.made').hide(); $('.magazine').removeClass('animated').addClass('zoom-in'); $('.zoom-icon').removeClass('zoom-icon-in').addClass('zoom-icon-out'); if (!window.escTip && !$.isTouch) { escTip = true; $('
', {'class': 'exit-message'}).html('
').
appendTo($('body')).
delay(2000).
animate({opacity:0}, 500, function() {
$(this).remove();
});
}
},
zoomOut: function () {
$('.exit-message').hide();
$('.thumbnails').fadeIn();
$('.made').fadeIn();
$('.zoom-icon').removeClass('zoom-icon-out').addClass('zoom-icon-in');
setTimeout(function(){
$('.magazine').addClass('animated').removeClass('zoom-in');
resizeViewport();
}, 0);
}
}
});
// Zoom event
if ($.isTouch)
$('.magazine-viewport').bind('zoom.doubleTap', zoomTo);
else
$('.magazine-viewport').bind('zoom.tap', zoomTo);
// Using arrow keys to turn the page
$(document).keydown(function(e){
var previous = 37, next = 39, esc = 27;
switch (e.keyCode) {
case previous:
// left arrow
$('.magazine').turn('previous');
e.preventDefault();
break;
case next:
//right arrow
$('.magazine').turn('next');
e.preventDefault();
break;
case esc:
$('.magazine-viewport').zoom('zoomOut');
e.preventDefault();
break;
}
});
// URIs - Format #/page/1
// Hash.go('page/1').now();
function region(currentPage) {
var region = new Array();
region[1] = '14-37% 49-59%';
region[2] = '15-36% 47-59%';
region[3] = '16-35% 48-58%';
region[4] = '17-34% 47-58%';
region[5] = '18-33% 48-57%';
region[6] = '19-32% 47-57%';
region[7] = '20-31% 48-56%';
region[8] = '21-30% 47-56%';
region[9] = '22-29% 48-55%';
region[10] = '23-28% 47-55%';
return region[currentPage];
}
function addPage(page, book) {
var id, pages = book.turn('pages');
// Create a new element for this page
var element = $('
// Add the page
if (book.turn('addPage', element, page)) {
// Add the content
loadSmallPage(page, element);
}
}
function loadSmallPage(page, pageElement) {
$.getJSON('pages/page' + page + '-small.json').done(function(data) {
var element = $('
', {'class': 'small-page',
css: {width: data.width, height: data.height}
}).
html('
');
if (page%2==0)
element.addClass('even');
else
element.addClass('odd');
pageElement.
html(element);
loadImage(
element.find('div'),
data.src,
'small'
);
});
}
function loadLargePage(page, pageElement) {
$.getJSON('pages/page' + page + '-large.json').done(function(data) {
var element = $('
', {'class': 'large-page',
css: {width: data.width, height: data.height}
});
if (page%2==0)
element.addClass('even');
else
element.addClass('odd');
var pageTop = $('
', {'class': 'page-top',
css: {width: data.width, height: data.height/2}
}).
appendTo(element);
var pageBottom = $('
', {'class': 'page-bottom',
css: {width: data.width, height: data.height/2, top: data.height/2}
}).
appendTo(element);
if (book.turn('addPage', element, page)) {
loadImage(
pageTop,
data.src,
'large',
1
);
loadImage(
pageBottom,
data.src,
'large',
2
);
}
});
}
function loadImage(page, src, size, number) {
var img = $('');
img.load(function() {
$(this).css({width: '100%', height: '100%'});
$(this).appendTo(page);
page.
parent().
find('.loading').
remove();
});
img.attr('src', src);
}
function loadFlipbook(flipbook) {
if (flipbook.width()%2!=0)
flipbook.width(flipbook.width()-1);
if (flipbook.height()%2!=0)
flipbook.height(flipbook.height()-1);
$('#canvas').remove();
// Create the canvas
var canvas = $('', {id: 'canvas'});
// Append the canvas to the flipbook
flipbook.parent().append(canvas);
// Enable canvas for IE
if (typeof G_vmlCanvasManager !== 'undefined')
G_vmlCanvasManager.initElement(canvas.get(0));
var context = canvas.get(0).getContext('2d');
// Canvas manipulation
context.fillStyle = '#ffffff';
context.fillRect(0, 0, canvas.width(), canvas.height());
// Load the HTML for the book
$.getJSON('pages/page-flipbook.json', function(data) {
$.each(data, function(key, page) {
var element = $('
', {'class': 'page'}).html('
');
// Load the page
element.
load(page.url+' #container', function() {
element.find('.loading').
remove();
var img = element.find('img');
img.css({width: '100%', height: '100%'});
if (page.front) {
$('').
load(function() {
flipbook.turn('peel', 'br');
}).
attr('src', 'pages/forward-flipbot.png');
img.appendTo($('
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:javaScript+turn.js实现图书翻页效果实例代码 - Python技术站