turn.js异步加载实现翻书效果


Posted in Javascript onJuly 25, 2019

本文实例为大家分享了turn.js异步加载实现翻书效果的具体代码,供大家参考,具体内容如下

1、阅读翻书js

/**
 * 电子翻书
 */
//var width = 1080;
//var height = 1680;
 
var width = "10rem";
var height = "15.2rem";
 
window.onload = function () {
 
 //预加载
 //loading(18,1);
 initData();
}
 
function getQueryString(name) {
 var result = window.location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
 if (result == null || result.length < 1) {
  return "";
 }
 return result[1];
}
 
function initData(){
 var book = getQueryString("bookId");
 var count = getQueryString("pageCount");
 loading_img_url = new Array();
 for (var i = 0; i < count; i++) {
 loading_img_url.push((i+1) + ".png");
 }
 var page = getQueryString("pageNum");
 if(!page){
 page =1;
 }
 loading(book,count,page);
}
 
var date_start;
var date_end;
date_start = getNowFormatDate();
//加载图片
var loading_img_url = [];
 
//加载页面
function loading(book,count,page) {
 var numbers = 0;
 var length = loading_img_url.length;
 //var resUrl = ctxStatic+"/modules/intelligentquery/img/3/";
 var resUrl = website + "/lawcase/bookScreenshot?bookId=" + book + "&page=";
 var jsUrl = ctxStatic+"/modules/front/guide/vertical/";
 var bookId = book;
  var img = new Image();
  img.src = resUrl + page;
  //img.src = resUrl + pageNum + ".png";
  img.onerror = function () {
   numbers += (1 / length) * 100;
  }
  img.onload = function () {
   numbers += (1 / length) * 100;
   $('.number').html(parseInt(numbers) + "%");
   console.log(numbers);
   if (Math.round(numbers)) {
    //$('.number').hide();
    date_end = getNowFormatDate();
    var loading_time = date_end - date_start;
    //预加载图片
    $(function progressbar() {
     //拼接图片
     $('.shade').hide();
     var tagHtml = "";
     var imgUrl = resUrl + page;
     //var imgUrl = resUrl + (pageNum) + ".png";
     if (pageNum == 1) {
      tagHtml += "<div id='first'><img src='"+imgUrl+"' /></div>";
     } else if (pageNum == length) {
      tagHtml += "<div id='first'><img src='"+imgUrl+"' /></div>";
     } else {
      tagHtml += "<div><img src='"+imgUrl+"' /></div>";
     }
     $(".flipbook").append(tagHtml);
     var w = $(".graph").width();
     $(".flipbook-viewport").show();
    });
 
 
    //配置turn.js
    function loadApp() {
     var w = width;
     var h = height;
     $('.flipboox').width(w).height(h);
     $('.flipbook').turn({
      width: w,
      height: h,
      elevation: 50,
      pages: count,
      display: 'single',
      gradients: true,
      autoCenter: true,
      when: {
       turning: function (e, page, view) {
        var total = $(".flipbook").turn("pages");//总页数
        $("#currentPage").html(page);
        $("#pageCount").html("/"+total);
        if (page == 1) {
         $(".btnImg").css("display", "none");
         $(".mark").css("display", "block");
        } else {
         $(".btnImg").css("display", "block");
         $(".mark").css("display", "none");
        }
        if (page == length) {
         $(".nextPage").css("display", "none");
        } else {
         $(".nextPage").css("display", "block");
        }
       },
       turned: function (e, page, view) {
        var total = $(".flipbook").turn("pages");//总页数
        $("#currentPage").html(page);
        $("#pageCount").html("/"+total);
        // 判断翻页按钮点击事件以及状态样式
        if(page >= total){
         $("#next").addClass("btn-invalid").removeAttr('onclick');
        }else{
         $("#next").removeClass("btn-invalid").attr("onclick","next();");
        }
        if(page == 1){
         $("#prev").addClass("btn-invalid").removeAttr('onclick');
         $("#indexPage").css("display","none");
        }else{
         $("#prev").removeClass("btn-invalid").attr("onclick","prev();");
         $("#indexPage").css("display","flex");
        }
       },
       missing: function (e, pages) {
        for (var i = 0; i < pages.length; i++) {
         addPage(pages[i], $(this),bookId);
        }
       }
 
 
      }
     })
  var cpage = getQueryString("pageNum");
  $(".flipbook").turn('page', cpage);
    }
    yepnope({
     test: Modernizr.csstransforms,
     yep: [jsUrl+'js/turn.js'],
     complete: loadApp
    });
   }
  }
}
 
function getNowFormatDate() {
 var date = new Date();
 var seperator1 = "";
 var seperator2 = "";
 var month = date.getMonth() + 1;
 var strDate = date.getDate();
 if (month >= 1 && month <= 9) {
  month = "0" + month;
 }
 if (strDate >= 0 && strDate <= 9) {
  strDate = "0" + strDate;
 }
 var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
  + "" + date.getHours() + seperator2 + date.getMinutes()
  + seperator2 + date.getSeconds();
 return currentdate;
}
 
//异步加载
function addPage(page, book,bookId) {
 //var resUrl = ctxStatic+"/modules/intelligentquery/img/3/";
 var resUrl = website + "/lawcase/bookScreenshot?bookId=" + bookId + "&page=";
 var imgUrl = resUrl + (page);
 var tagHtml = "";
 if (page == 1) {
  tagHtml += "<div id='first'><img src='"+imgUrl+"' /></div>";
 } else if (page == length) {
  tagHtml += "<div id='end'><img src='"+imgUrl+"' /></div>";
 } else {
  tagHtml += "<div><img src='"+imgUrl+"' /></div>";
 }
 
 // Check if the page is not in the book
 if (!book.turn('hasPage', page)) {
  // Create an element for this page
  var element = $('<div />').html('');
  // Add the page
  book.turn('addPage', element, page);
  element.html(tagHtml);
 }
}

2、阅读页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="front" value="${frontPath}${pageContext.request.contextPath}/f"/>
<c:set var="ctxStatic" value="${pageContext.request.contextPath}/static"/>
<c:set var="website" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
 <meta name="format-detection" content="telephone=no">
 <meta name="apple-mobile-web-app-capable" content="yes"/>
 <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
 <title>阅读</title>
 <script src="${ctxStatic}/plugins/jquery-3.2.1.min.js"></script>
 <script src="${ctxStatic}/plugins/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
 <script src="${ctxStatic}/modules/front/guide/vertical/common/js/common.js"></script>
 <script type="text/javascript" src="${ctxStatic}/modules/front/guide/vertical/js/modernizr.2.5.3.min.js"></script>
 
 <script type="text/javascript" src="${ctxStatic}/modules/front/guide/vertical/js/main.js"></script>
 <script src="${ctxStatic}/modules/front/guide/vertical/common/js/flexible.js"></script>
 
 
 <link href="${ctxStatic}/modules/front/guide/vertical/css/app-base.css" rel="external nofollow" rel="stylesheet">
 <style type="text/css">
 .flipbook img{
 width:10rem;
 height:15.2rem;
 }
 .book-wrapper{
 background-image: url(''); 
 }
 </style>
 
</head>
<body>
 <div class="flex-container">
  <header class="banner2">
   <h1>阅读</h1>
  </header>
  <div class="page-content judicial-wrapper book-wrapper">
   <!-- 书本区域 -->
   <div class="flipbook-viewport book-box boox-details" style="display:none;">
    <div class="previousPage"></div>
    <div class="nextPage"></div>
    <div class="return"></div>
    <div class="container">
     <div class="flipbook">
     </div>
    </div>
   </div>
  </div>
  <!-- 悬浮菜单 -->
  <nav class="menu-right">
   <div class="paging-box">
    <ul>
     <li>
      <a href="javascript:;" id="goBack" class="i-orange"> <i class="icon-undo2"></i></a>
     </li>
     <li>
      <a href="javascript:;" id="prev" class="prev-page" οnclick="prev()"> <i class="icon-arrow-up2"></i></a>
     </li>
     <li class="paging-item">
      <p id="pageNum"><span id="currentPage">0</span><span id="pageCount">/0</span></p>
     </li>
     <li>
      <a href="javascript:;" id="next" class="next-page" οnclick="next()"> <i class="icon-arrow-down2"></i></a>
     </li>
     <li>
      <div class="skip-page">
       <span>跳至</span>
       <input id="skip-page-num" type="text" name="skip-toPage">
       <div id="softkey"></div>
       <span>页</span>
      </div>
     </li>
     <li>
      <a href="${front}/guide/vertical/index" class="color-home"> <i class="icon-homepage_fill"></i></a>
     </li>
    </ul>
   </div>
  </nav>
 </div>
</body>
</html>
 
<script>
 
 var ctxStatic = "${ctxStatic}";
 var website = "${website}";
 
 //上一页
 function prev(){
  var currentPage = $(".flipbook").turn("page");
  $(".flipbook").turn('page', currentPage - 1);
 }
 // 下一页
 function next() {
  var currentPage = $(".flipbook").turn("page");
  $(".flipbook").turn('page', currentPage + 1);
 }
 
 var temp_couter = 0;
 // 模拟数字键盘
 var softkey = document.getElementById("softkey");
 var input1 = document.getElementById("skip-page-num");
 $('#skip-page-num').focus(function(){
  new KeyBoard(input1,softkey);
 });
 
 //跳页
 function _global_keyboard_close_btn_callback(value){
  var pageNum = parseInt(value);
  var total = parseInt(getQueryString("pageCount"));
  if(pageNum <= 1){
   pageNum = 1;
  }else if(pageNum >= total){
   pageNum = total;
  }
 
  $("#skip-page-num").val(pageNum);
  $(".flipbook").turn('page', pageNum);
 }
 
</script>
<script src="${ctxStatic}/modules/front/guide/vertical/common/virtualkeyboard/keyboard.js"></script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript基本概念初级讲解论坛贴的学习记录
Feb 22 Javascript
利用JQuery+EasyDrag 实现弹出可拖动的Div,同时向Div传值,然后返回Div选中的值
Oct 24 Javascript
JQuery操作Select的Options的Bug(IE8兼容性视图模式)
Apr 21 Javascript
用js实现小球的自由移动代码
Apr 22 Javascript
Jquery实现的简单轮播效果【附实例】
Apr 19 Javascript
js遍历json的key和value的实例
Jan 22 Javascript
AngularJS select设置默认值的实现方法
Aug 25 Javascript
详解VUE 对element-ui中的ElTableColumn扩展
Mar 28 Javascript
vue 表单验证按钮事件交由父组件触发的方法
Dec 17 Javascript
JS实现简单日历特效
Jan 03 Javascript
JavaScript函数柯里化实现原理及过程
Dec 02 Javascript
让JavaScript代码更加精简的方法技巧
Jun 01 Javascript
js 实现ajax发送步骤过程详解
Jul 25 #Javascript
MockJs结合json-server模拟后台数据
Aug 26 #Javascript
微信小程序 调用微信授权窗口相关问题解决
Jul 25 #Javascript
mock.js模拟前后台交互
Jul 25 #Javascript
解决Vue打包后访问图片/图标不显示的问题
Jul 25 #Javascript
使用VueCli3+TypeScript+Vuex一步步构建todoList的方法
Jul 25 #Javascript
vue使用代理解决请求跨域问题详解
Jul 24 #Javascript
You might like
php防盗链的常用方法小结
2010/07/02 PHP
php 操作数组(合并,拆分,追加,查找,删除等)
2012/07/20 PHP
php面象对象数据库操作类实例
2014/12/02 PHP
laravel 实现上传图片到本地和前台访问示例
2019/10/21 PHP
javascript 混合的构造函数和原型方式,动态原型方式
2009/12/07 Javascript
JQuery 动态扩展对象之另类视角
2010/05/25 Javascript
JS 面向对象之神奇的prototype
2011/02/26 Javascript
JavaScript代码复用模式实例分析
2012/12/02 Javascript
文本框中禁止非数字字符输入比如手机号码、邮编
2013/08/19 Javascript
Javascript改变CSS样式(局部和全局)
2013/12/18 Javascript
js中array的sort()方法使用介绍
2014/02/20 Javascript
jquery插件推荐 jquery.cookie
2014/11/09 Javascript
js学习总结_轮播图之渐隐渐现版(实例讲解)
2017/07/17 Javascript
Vue2.5通过json文件读取数据的方法
2018/02/27 Javascript
node.js学习笔记之koa框架和简单爬虫练习
2018/12/13 Javascript
layui-tree实现Ajax异步请求后动态添加节点的方法
2019/09/23 Javascript
Vue Object.defineProperty及ProxyVue实现双向数据绑定
2020/09/02 Javascript
python去掉字符串中重复字符的方法
2014/02/27 Python
Django1.7+python 2.78+pycharm配置mysql数据库教程
2014/11/18 Python
Python标准库内置函数complex介绍
2014/11/25 Python
Python3实现的回文数判断及罗马数字转整数算法示例
2019/03/27 Python
python使用MQTT给硬件传输图片的实现方法
2019/05/05 Python
Python 虚拟空间的使用代码详解
2019/06/10 Python
python使用Pandas库提升项目的运行速度过程详解
2019/07/12 Python
python3 写一个WAV音频文件播放器的代码
2019/09/27 Python
有关Tensorflow梯度下降常用的优化方法分享
2020/02/04 Python
python词云库wordCloud使用方法详解(解决中文乱码)
2020/02/17 Python
基于python requests selenium爬取excel vba过程解析
2020/08/12 Python
Python 通过正则表达式快速获取电影的下载地址
2020/08/17 Python
CSS3使用transition属性实现过渡效果
2018/04/18 HTML / CSS
基于Html5实现的react拖拽排序组件示例
2018/08/13 HTML / CSS
南京某软件公司的.net面试题
2015/11/30 面试题
孝老爱亲事迹材料
2014/12/24 职场文书
python生成随机数、随机字符、随机字符串
2021/04/06 Python
详解Java实践之建造者模式
2021/06/18 Java/Android
详解Anyscript开发指南绕过typescript类型检查
2022/09/23 Javascript