当前位置:首页 > VUE

vue实现小说翻页效果

2026-02-22 04:31:40VUE

Vue实现小说翻页效果

基础翻页逻辑

通过v-model绑定当前页码,结合计算属性动态渲染章节内容。示例代码:

<template>
  <div class="reader-container">
    <div class="content">{{ currentContent }}</div>
    <button @click="prevPage">上一页</button>
    <button @click="nextPage">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      totalPages: 10,
      chapters: [
        { id: 1, text: '第一章内容...' },
        { id: 2, text: '第二章内容...' }
      ]
    }
  },
  computed: {
    currentContent() {
      return this.chapters[this.currentPage - 1]?.text || '内容加载中'
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++
    }
  }
}
</script>

平滑滚动动画

使用CSS过渡实现翻页动画效果:

.content {
  transition: transform 0.3s ease;
}
.slide-left {
  transform: translateX(-100%);
}
.slide-right {
  transform: translateX(100%);
}

配合Vue的过渡模式:

methods: {
  nextPage() {
    document.querySelector('.content').classList.add('slide-left')
    setTimeout(() => {
      this.currentPage++
      document.querySelector('.content').classList.remove('slide-left')
    }, 300)
  }
}

触摸屏支持

添加触摸事件处理实现手势翻页:

data() {
  return {
    touchStartX: 0
  }
},
methods: {
  handleTouchStart(e) {
    this.touchStartX = e.touches[0].clientX
  },
  handleTouchEnd(e) {
    const touchEndX = e.changedTouches[0].clientX
    const diffX = touchEndX - this.touchStartX

    if (diffX > 50) this.prevPage()
    else if (diffX < -50) this.nextPage()
  }
}

模板中绑定事件:

<div 
  @touchstart="handleTouchStart"
  @touchend="handleTouchEnd"
></div>

阅读进度持久化

使用localStorage保存阅读进度:

created() {
  const savedPage = localStorage.getItem('lastReadPage')
  if (savedPage) this.currentPage = parseInt(savedPage)
},
watch: {
  currentPage(newVal) {
    localStorage.setItem('lastReadPage', newVal)
  }
}

分页算法处理长文本

对于未分章的文本内容,可按字数自动分页:

vue实现小说翻页效果

function paginateText(text, charsPerPage = 1500) {
  const pages = []
  for (let i = 0; i < text.length; i += charsPerPage) {
    pages.push(text.slice(i, i + charsPerPage))
  }
  return pages
}

标签: 翻页效果
分享给朋友:

相关文章

vue实现拖拉效果

vue实现拖拉效果

实现拖拽效果的基本方法 在Vue中实现拖拽效果可以通过原生HTML5的拖拽API或第三方库如vuedraggable来完成。以下是两种常见方法的详细说明。 使用HTML5拖拽API HTML5提供了…

vue翻页实现

vue翻页实现

Vue 翻页实现方法 在 Vue 中实现翻页功能通常需要结合分页组件和数据处理逻辑。以下是几种常见的实现方式: 使用 Element UI 分页组件 Element UI 提供了现成的分页组件 &l…

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 Vue.js 提供了多种方式来实现动态效果展示,以下是一些常见的方法: 使用 Vue 的过渡和动画系统 Vue 内置了 <transition> 和 <…

vue实现轮播效果

vue实现轮播效果

使用Swiper插件实现轮播 安装Swiper及相关依赖: npm install swiper vue-awesome-swiper 组件代码示例: <template> <…

vue实现筛选效果

vue实现筛选效果

Vue 实现筛选效果的方法 基于计算属性的筛选 在 Vue 中可以通过计算属性实现数据的动态筛选。定义一个计算属性,根据用户输入的筛选条件返回过滤后的数据列表。 <template>…

vue实现拖动效果

vue实现拖动效果

使用 vuedraggable 库实现拖动 vuedraggable 是基于 Sortable.js 的 Vue 组件库,提供简单易用的拖拽功能。安装方式如下: npm install vuedra…