当前位置:首页 > VUE

vue实现书本翻页效果

2026-02-25 05:37:09VUE

实现思路

使用CSS 3D变换和过渡效果模拟书本翻页的立体感,结合Vue的数据绑定和事件处理实现交互控制。核心在于将翻页动作分解为多个阶段,通过JavaScript动态调整CSS属性。

基础HTML结构

<template>
  <div class="book-container">
    <div class="book">
      <div 
        class="page" 
        v-for="(page, index) in pages" 
        :key="index"
        :class="{ 'active': currentPage === index }"
      >
        {{ page.content }}
      </div>
    </div>
    <button @click="flipPage(-1)">上一页</button>
    <button @click="flipPage(1)">下一页</button>
  </div>
</template>

CSS关键样式

.book-container {
  perspective: 1500px;
  margin: 50px auto;
}

.book {
  position: relative;
  width: 300px;
  height: 400px;
  transform-style: preserve-3d;
}

.page {
  position: absolute;
  width: 100%;
  height: 100%;
  background: white;
  border: 1px solid #ccc;
  box-shadow: 0 0 5px rgba(0,0,0,0.1);
  transform-origin: left center;
  transition: transform 1s ease;
  backface-visibility: hidden;
}

.page.active {
  transform: rotateY(0deg);
}

.page.flipping {
  transform: rotateY(-180deg);
  z-index: 10;
}

Vue组件逻辑

<script>
export default {
  data() {
    return {
      currentPage: 0,
      pages: [
        { content: '第一页内容' },
        { content: '第二页内容' },
        { content: '第三页内容' }
      ],
      isFlipping: false
    }
  },
  methods: {
    flipPage(direction) {
      if (this.isFlipping) return;

      const newPage = this.currentPage + direction;
      if (newPage < 0 || newPage >= this.pages.length) return;

      this.isFlipping = true;
      const currentPageEl = document.querySelectorAll('.page')[this.currentPage];
      currentPageEl.classList.add('flipping');

      setTimeout(() => {
        currentPageEl.classList.remove('flipping');
        this.currentPage = newPage;
        this.isFlipping = false;
      }, 1000);
    }
  }
}
</script>

高级优化方案

添加阴影效果增强立体感

.page.flipping {
  box-shadow: 
    -10px 0 20px rgba(0,0,0,0.2),
    inset -10px 0 10px rgba(0,0,0,0.1);
}

实现连续翻页动画

async flipMultiplePages(targetPage) {
  const steps = Math.abs(targetPage - this.currentPage);
  const direction = targetPage > this.currentPage ? 1 : -1;

  for (let i = 0; i < steps; i++) {
    await this.flipPageAsync(direction);
  }
}

flipPageAsync(direction) {
  return new Promise(resolve => {
    this.flipPage(direction);
    setTimeout(resolve, 1000);
  });
}

移动端适配

添加触摸事件支持

vue实现书本翻页效果

<div 
  class="page"
  @touchstart="touchStart"
  @touchmove="touchMove"
  @touchend="touchEnd"
></div>
touchStart(e) {
  this.startX = e.touches[0].clientX;
},
touchMove(e) {
  const moveX = e.touches[0].clientX;
  const diff = this.startX - moveX;
  if (Math.abs(diff) > 50) {
    this.flipPage(diff > 0 ? 1 : -1);
  }
}

标签: 翻页书本
分享给朋友:

相关文章

vue实现仿真翻页

vue实现仿真翻页

Vue 实现仿真翻页效果 实现仿真翻页效果可以通过 CSS 3D 变换和 Vue 的过渡动画结合完成。以下是一个完整的实现方案: 基础 HTML 结构 <template> <…

vue实现小说翻页

vue实现小说翻页

Vue实现小说翻页功能 实现小说翻页功能通常需要处理文本分页、翻页动画和用户交互。以下是基于Vue的实现方案: 文本分页处理 需要将长文本分割成适合页面显示的段落。可以使用计算属性或方法动态分割文本…

react如何实现按键翻页

react如何实现按键翻页

实现按键翻页的基本思路 在React中实现按键翻页,通常需要监听键盘事件,根据按键类型(如左右箭头)更新当前页码或数据索引。核心步骤包括:绑定键盘事件、处理按键逻辑、更新组件状态。 监听键盘事件 使…

react实现翻页切换效果

react实现翻页切换效果

使用React实现翻页切换效果 React中实现翻页切换效果可以通过多种方式完成,包括使用CSS动画、第三方库或自定义组件。以下是几种常见方法: 基于CSS过渡的翻页效果 利用React状态管理和C…

php实现翻页

php实现翻页

实现基本翻页功能 在PHP中实现翻页功能通常需要结合MySQL数据库查询和前端HTML展示。基本思路是通过LIMIT和OFFSET参数控制每页显示的数据量。 // 数据库连接 $conn = new…

php 实现翻页

php 实现翻页

实现基本分页功能 使用PHP的LIMIT子句结合MySQL实现分页是最常见的方法。需要计算当前页码和每页显示的记录数。 // 每页显示数量 $perPage = 10; // 当前页码(从URL参数…