最近CTO給我分配了一個移動端H5開發的任務,主要功能是需要實現翻書效果,我聽過主要需求後,當時是呀!!!接下來自己嘗試使用 fullPage.js和Swiper來實現翻書效果,結果效果都不是非常的理想,後來想起自己曾經做過PC版的翻書效果,當時使用的是Turn.js ,查過其相關API後,整個人突 ...
最近CTO給我分配了一個移動端H5開發的任務,主要功能是需要實現翻書效果,我聽過主要需求後,當時是呀!!!接下來自己嘗試使用 fullPage.js和Swiper來實現翻書效果,結果效果都不是非常的理想,後來想起自己曾經做過PC版的翻書效果,當時使用的是Turn.js ,查過其相關API後,整個人突然豁然開朗呀,使用Turn.js 完全可以解決當前我接手這個項目的所有需求呀。現在將個人的學習總結如下,若有不正確的地方,歡迎讀者給與批評指正!
Turn.js的官方網址: http://www.turnjs.com/
下麵是我這個項目上線後的效果:
看過實際項目後,各位看官是不是已經迫不及待的想知道這個項目是如何實現,看官莫急,接下來我就詳細的介紹下我的開發過程:
1、需要引入的腳本文件
1 <link rel="stylesheet" type="text/css" href="css/basic.css"/> 2 <script type="text/javascript" src="js/jquery.js"></script> 3 <script type="text/javascript" src="js/modernizr.2.5.3.min.js"></script> 4 <script type="text/javascript" src="js/main.js"></script>
2、html部分代碼
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/> 7 <meta name="format-detection" content="telephone=no"> 8 <meta name="apple-mobile-web-app-capable" content="yes"/> 9 <meta name="apple-mobile-web-app-status-bar-style" content="black"/> 10 <title>Turn.js 實現翻書效果</title> 11 <link rel="stylesheet" type="text/css" href="css/basic.css"/> 12 <script type="text/javascript" src="js/jquery.js"></script> 13 <script type="text/javascript" src="js/modernizr.2.5.3.min.js"></script> 14 <script type="text/javascript" src="js/main.js"></script> 15 </head> 16 <body> 17 <div class="shade"> 18 <div class="sk-fading-circle"> 19 <div class="sk-circle1 sk-circle"></div> 20 <div class="sk-circle2 sk-circle"></div> 21 <div class="sk-circle3 sk-circle"></div> 22 <div class="sk-circle4 sk-circle"></div> 23 <div class="sk-circle5 sk-circle"></div> 24 <div class="sk-circle6 sk-circle"></div> 25 <div class="sk-circle7 sk-circle"></div> 26 <div class="sk-circle8 sk-circle"></div> 27 <div class="sk-circle9 sk-circle"></div> 28 <div class="sk-circle10 sk-circle"></div> 29 <div class="sk-circle11 sk-circle"></div> 30 <div class="sk-circle12 sk-circle"></div> 31 </div> 32 <div class="number"></div> 33 </div> 34 <div class="flipbook-viewport" style="display:none;"> 35 <div class="previousPage"></div> 36 <div class="nextPage"></div> 37 <div class="return"></div> 38 <img class="btnImg" src="./image/btn.gif" style="display: none"/> 39 <div class="container"> 40 <div class="flipbook"> 41 </div> 42 </div> 43 </div> 44 <script> 45 //自定義仿iphone彈出層 46 (function ($) { 47 //ios confirm box 48 jQuery.fn.confirm = function (title, option, okCall, cancelCall) { 49 var defaults = { 50 title: null, //what text 51 cancelText: '取消', //the cancel btn text 52 okText: '確定' //the ok btn text 53 }; 54 55 if (undefined === option) { 56 option = {}; 57 } 58 if ('function' != typeof okCall) { 59 okCall = $.noop; 60 } 61 if ('function' != typeof cancelCall) { 62 cancelCall = $.noop; 63 } 64 65 var o = $.extend(defaults, option, {title: title, okCall: okCall, cancelCall: cancelCall}); 66 67 var $dom = $(this); 68 69 var dom = $('<div class="g-plugin-confirm">'); 70 var dom1 = $('<div>').appendTo(dom); 71 var dom_content = $('<div>').html(o.title).appendTo(dom1); 72 var dom_btn = $('<div>').appendTo(dom1); 73 var btn_cancel = $('<a href="#"></a>').html(o.cancelText).appendTo(dom_btn); 74 var btn_ok = $('<a href="#"></a>').html(o.okText).appendTo(dom_btn); 75 btn_cancel.on('click', function (e) { 76 o.cancelCall(); 77 dom.remove(); 78 e.preventDefault(); 79 }); 80 btn_ok.on('click', function (e) { 81 o.okCall(); 82 dom.remove(); 83 e.preventDefault(); 84 }); 85 86 dom.appendTo($('body')); 87 return $dom; 88 }; 89 })(jQuery); 90 91 //上一頁 92 $(".previousPage").bind("touchend", function () { 93 var pageCount = $(".flipbook").turn("pages");//總頁數 94 var currentPage = $(".flipbook").turn("page");//當前頁 95 if (currentPage >= 2) { 96 $(".flipbook").turn('page', currentPage - 1); 97 } else { 98 } 99 }); 100 // 下一頁 101 $(".nextPage").bind("touchend", function () { 102 var pageCount = $(".flipbook").turn("pages");//總頁數 103 var currentPage = $(".flipbook").turn("page");//當前頁 104 if (currentPage <= pageCount) { 105 $(".flipbook").turn('page', currentPage + 1); 106 } else { 107 } 108 }); 109 //返回到目錄頁 110 $(".return").bind("touchend", function () { 111 $(document).confirm('您確定要返迴首頁嗎?', {}, function () { 112 $(".flipbook").turn('page', 1); //跳轉頁數 113 }, function () { 114 }); 115 }); 116 </script> 117 </body> 118 </html>View Code
3、主要js實現部分
1 /** 2 * Created by ChengYa on 2016/6/18. 3 */ 4 5 //判斷手機類型 6 window.onload = function () { 7 //alert($(window).height()); 8 var u = navigator.userAgent; 9 if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手機 10 } else if (u.indexOf('iPhone') > -1) {//蘋果手機 11 //屏蔽ios下上下彈性 12 $(window).on('scroll.elasticity', function (e) { 13 e.preventDefault(); 14 }).on('touchmove.elasticity', function (e) { 15 e.preventDefault(); 16 }); 17 } else if (u.indexOf('Windows Phone') > -1) {//winphone手機 18 } 19 //預載入 20 loading(); 21 } 22 23 var date_start; 24 var date_end; 25 date_start = getNowFormatDate(); 26 //載入圖片 27 var loading_img_url = [ 28 "./image/0001.jpg", 29 "./image/0002.jpg", 30 "./image/0003.jpg", 31 "./image/0004.jpg", 32 "./image/0005.jpg", 33 "./image/0006.jpg", 34 "./image/0007.jpg", 35 "./image/0008.jpg", 36 "./image/0009.jpg", 37 "./image/0010.jpg", 38 "./image/0011.jpg", 39 "./image/0012.jpg", 40 "./image/0013.jpg", 41 "./image/0014.jpg", 42 "./image/0015.jpg", 43 "./image/0016.jpg", 44 "./image/0017.jpg", 45 "./image/0018.jpg", 46 "./image/0019.jpg", 47 "./image/0020.jpg", 48 "./image/0021.jpg", 49 "./image/0022.jpg", 50 "./image/0023.jpg", 51 "./image/0024.jpg", 52 "./image/0025.jpg", 53 "./image/0026.jpg", 54 "./image/0027.jpg", 55 "./image/0028.jpg", 56 "./image/0029.jpg", 57 "./image/0030.jpg", 58 "./image/0031.jpg", 59 "./image/0032.jpg", 60 "./image/0033.jpg", 61 "./image/0034.jpg", 62 "./image/0035.jpg", 63 "./image/0036.jpg", 64 "./image/0037.jpg", 65 "./image/0038.jpg", 66 "./image/0039.jpg", 67 "./image/0040.jpg", 68 "./image/0041.jpg", 69 ]; 70 71 //載入頁面 72 function loading() { 73 var numbers = 0; 74 var length = loading_img_url.length; 75 76 for (var i = 0; i < length; i++) { 77 var img = new Image(); 78 img.src = loading_img_url[i]; 79 img.onerror = function () { 80 numbers += (1 / length) * 100; 81 } 82 img.onload = function () { 83 numbers += (1 / length) * 100; 84 $('.number').html(parseInt(numbers) + "%"); 85 console.log(numbers); 86 if (Math.round(numbers) == 100) { 87 //$('.number').hide(); 88 date_end = getNowFormatDate(); 89 var loading_time = date_end - date_start; 90 //預載入圖片 91 $(function progressbar() { 92 //拼接圖片 93 $('.shade').hide(); 94 var tagHtml = ""; 95 for (var i = 1; i <= 41; i++) { 96 if (i == 1) { 97 tagHtml += ' <div id="first" style="background:url(image/00' + (i < 10 ? '0' + i : i) + '.jpg) center top no-repeat;background-size:100%"></div>'; 98 } else if (i == 41) { 99 tagHtml += ' <div id="end" style="background:url(image/00' + (i < 10 ? '0' + i : i) + '.jpg) center top no-repeat;background-size:100%"></div>'; 100 } else { 101 tagHtml += ' <div style="background:url(image/00' + (i < 10 ? '0' + i : i) + '.jpg) center top no-repeat;background-size:100%"></div>'; 102 } 103 } 104 $(".flipbook").append(tagHtml); 105 var w = $(".graph").width(); 106 $(".flipbook-viewport").show(); 107 }); 108 //配置turn.js 109 function loadApp() { 110 var w = $(window).width(); 111 var h = $(window).height(); 112 $('.flipboox').width(w).height(h); 113 $(window).resize(function () { 114 w = $(window).width(); 115 h = $(window).height(); 116 $('.flipboox').width(w).height(h); 117 }); 118 $('.flipbook').turn({ 119 // Width 120 width: w, 121 // Height 122 height: h, 123 // Elevation 124 elevation: 50, 125 display: 'single', 126 // Enable gradients 127 gradients: true, 128 // Auto center this flipbook 129 autoCenter: true, 130 when: { 131 turning: function (e, page, view) { 132 if (page == 1) { 133 $(".btnImg").css("display", "none"); 134 $(".mark").css("display", "block"); 135 } else { 136 $(".btnImg").css("display", "block"); 137 $(".mark").css("display", "none"); 138 } 139 if (page == 41) { 140 $(".nextPage").css("display", "none"); 141 } else { 142 $(".nextPage").css("display", "block"); 143 } 144 }, 145 turned: function (e, page, view) { 146 console.log(page); 147 var total = $(".flipbook").turn("pages