当前位置:首页 > 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
}

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

相关文章

react如何添加效果

react如何添加效果

React 添加动画效果的方法 使用 CSS 过渡动画 在 React 组件中直接通过 className 或 style 属性应用 CSS 过渡效果。定义 CSS 类包含 transition 属性…

vue如何实现翻页

vue如何实现翻页

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

vue swiper 实现翻页

vue swiper 实现翻页

Vue 中使用 Swiper 实现翻页效果 安装 Swiper 依赖 确保项目已安装 swiper 和 vue-awesome-swiper(适用于 Vue 2/3): npm install s…

vue实现小说翻页

vue实现小说翻页

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

vue实现分页效果

vue实现分页效果

使用Element UI实现分页 安装Element UI库后,可以直接使用el-pagination组件。需要在Vue项目中引入并注册Element UI。 <template>…

vue实现聊天效果

vue实现聊天效果

Vue 实现聊天效果 在 Vue 中实现聊天效果,可以通过组件化设计、数据绑定和事件处理来完成。以下是关键步骤和代码示例。 数据结构和状态管理 使用 Vue 的 data 或 ref(Composi…