JavaScript 移动端电子书翻页效果实现方案

作者:闭包旅人时间:2026-10-23 10:56:49浏览:459

本文介绍如何在移动端实现电子书翻页效果。核心思路是将后端返回的长文本根据屏幕高度切割为多页,并引入 turn.js 插件为每页添加逼真的翻书动画。文章提供了完整的 HTML 结构与关键 CSS 样式配置,适合需要开发移动端阅读应用的开发者参考。

移动端电子书翻页效果概述

本文详细解析了如何在 JavaScript 环境下为移动端设备实现电子书的翻页交互效果。文中通过具体的代码示例进行了详尽的阐述,该方案对于从事相关功能开发或学习前端技术的读者具有较高的参考价值。

效果展示

移动端电子书翻页效果概述

核心实现流程

首先,后端接口需返回一段完整的纯文本数据。

其次,前端逻辑需获取当前设备的屏幕高度,并依据该高度将长文本智能分割为 n 个独立的页面区块。

最后,引入 turn.js 插件处理上述分页数据,为每一页配置并渲染翻书动画效果。

代码实现与配置

代码实现


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>手机端书本翻页效果</title>
    <style type="text/css">
      * {
        padding: 0;
        margin: 0;
      }
 
      html,
      body {
        height: 100%;
        width: 100%;
      }
 
      #magazine {
        width: 100%;
        height: 100%;
        position: relative;
        overflow: hidden;
         
      }
 
      #pages {
        width: 100%;
        height: 100%;
        position: relative;
        z-index: 1;
      }
      #pages div.turn-page{
        background: #fff;
      }
      #content{
        height: 0;
        overflow: hidden;
        width: 100%;
      }
      #contentText{
        width: 100%;
      }
       
      /* 这里是内容的样式,修改时候,一起修改 */
      div.turn-page,#contentText{
        white-space: pre-wrap;
        box-sizing: border-box;
        padding: 0 10px;
      }
       
       
      #alert{
        position: absolute;
        bottom: 40px;
        left: 50%;
        transform: translateX(-50%);
        background: rgba(0,0,0,0.6);
        border-radius: 4px;
        color: #fff;
        z-index: 10;
        font-size: 12px;
        padding: 6px 10px;
        display: none;
      }
    </style>
  </head>
  <body>
 
    <div id="magazine">
      <div id="pages"></div>
      <div id="content">
        <div id="contentText"></div>
      </div>
    </div>
    <div id="alert"></div>
    <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="js/turn.js"></script>
     
    <script type="text/javascript">
      var writeStr = ""
       
      //模拟请求文本数据
      $.get("./js/data.txt",function(data){
        initPage(data);
      })
       
 
      function initPage(writeStr){
        if(!writeStr){
          return ;
        }
       
        var $wrap = $("#magazine");
        var $page = $("#pages");
        var w =$page.width(); //窗口的宽度
        var h = $page.height(); //窗口的高度
        console.log(h)
        var $content = $("#contentText");
         
        $content.html(writeStr);
        var len = writeStr.length; //总长度
        var cH = $content.height(); //总高度
        var pageStrNum; //每页大概有多少个字符
        if(cH > h){
          pageStrNum = (h / cH )*len; //每页大概有多少个字符
          var obj = overflowhiddenTow($content,writeStr,h);
          $page.append('<div>'+obj.curr+'</div>');
          while(obj.next && obj.next.length > 0){
            obj = overflowhiddenTow($content, obj.next,h);
            $page.append('<div>'+obj.curr+'</div>');
          }
        }else{
          return ;
        }
         
        //文字切割算法
        function overflowhiddenTow($texts, str , at) {
          var throat = pageStrNum;
          var tempstr = str.substring(0, throat);
          var len = str.length;
          $texts.html(tempstr);
          //取的字节较少,应该增加
          while ($texts.height() < at && throat < len) {
            throat = throat + 2;
            tempstr = str.substring(0, throat);
            $texts.html(tempstr);
          }
          //取的字节较多,应该减少
          while ($texts.height() > at && throat > 0) {
            throat = throat - 2;
            tempstr = str.substring(0, throat);
            $texts.html(tempstr);
          }
           
          return {
            curr:str.substring(0,throat),
            next:str.substring(throat)
          }
         
        }
         
        $page.turn({
          width: w,
          height: h,
          elevation: 50,
          display: 'single',
          gradients: true,
          autoCenter: true,
          when: {
            start: function() {},
            turning: function(e, page, view) {},
            turned: function(e, page, view) {
               
            }
          }
        });
         
         
         
        var moveObj = null;
        var endObj = null;
         
        function getPoint(e) {
          var obj = e;
          if (e.targetTouches && e.targetTouches.length > 0) {
            obj = e.targetTouches[0];
          }
          return obj;
        }
         
         
        $wrap.on("touchstart mousedown", function(e) {
          var obj = getPoint(e);
          moveObj = {
            x: obj.clientX
          };
        });
        $wrap.on("touchmove mousemove", function(e) {
          var obj = getPoint(e);
          endObj = {
            x: obj.clientX
          };
        });
         
         
        $wrap.on("touchend mouseup", function(e) {
          if (moveObj && endObj) {
            var mis = endObj.x - moveObj.x;
            if (Math.abs(mis) > 30) {
              var pageCount = $page.turn("pages"); //总页数
              var currentPage = $page.turn("page"); //当前页
              if (mis > 0) {
                if (currentPage > 1) {
                  $page.turn('page', currentPage - 1);
                } else {
                  console.log("已经是第一页")
                  showAlert('已经是第一页');
                }
              } else {
                if (currentPage < pageCount) {
                  $page.turn('page', currentPage + 1);
                } else {
                  console.log("最后一页");
                  showAlert('已经是最后一页');
                }
              }
         
            }
         
          }
          moveObj = null;
          endObj = null;
        });
         
        var $alert = $("#alert");
        var timer = null;
        function showAlert(msg){
          clearTimeout(timer);
          $alert.text(msg);
          $alert.fadeIn();
          timer = setTimeout(function(){
            $alert.fadeOut();
          },1000)
        }
      }
    </script>
  </body>
</html>

以上即为本文分享的全部内容,希望上述方案能对各位的学习或工作提供切实帮助,欢迎持续关注本站获取更多技术资源。

作者最新文章