当前位置:首页 > VUE

vue实现书本翻页效果

2026-02-25 05:37:09VUE

实现思路

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

vue实现书本翻页效果

基础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>

高级优化方案

添加阴影效果增强立体感

vue实现书本翻页效果

.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);
  });
}

移动端适配

添加触摸事件支持

<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 实现翻页的方法 在 Vue 中实现翻页功能通常可以通过以下方法完成: 使用计算属性分页 通过计算属性对数据进行分页处理,结合页码和每页显示数量动态计算当前页数据。 data() { r…

vue实现题目翻页

vue实现题目翻页

Vue实现题目翻页功能 使用v-for和计算属性实现分页 在Vue中可以通过计算属性对题目列表进行分页处理,结合v-for渲染当前页的题目。定义currentPage和pageSize控制分页逻辑。…

vue中实现翻页功能

vue中实现翻页功能

使用 v-pagination 组件 Vue 的 v-pagination 组件(来自 Vuetify)提供了一种简单的方式实现翻页功能。安装 Vuetify 后,可以直接使用该组件。 <te…

vue实现滑动翻页

vue实现滑动翻页

Vue 实现滑动翻页的方法 使用 Touch 事件监听 在 Vue 中可以通过监听 touchstart、touchmove 和 touchend 事件来实现滑动翻页效果。需要在组件中定义相关的事件处…

vue实现仿真翻页

vue实现仿真翻页

Vue实现仿真翻页效果 实现仿真翻页效果通常需要结合CSS 3D变换和动画,以下是一个完整的实现方案: 基础HTML结构 <template> <div class="flip…

php实现翻页

php实现翻页

PHP实现翻页功能 翻页功能是Web开发中常见的需求,可以通过PHP结合数据库查询实现。以下是几种常见的实现方式: 基于LIMIT的简单分页 $perPage = 10; // 每页显示数量 $…