当前位置:首页 > 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的过渡模式:

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()
  }
}

模板中绑定事件:

vue实现小说翻页效果

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

分页算法处理长文本

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

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
}

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

相关文章

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-…

如何实现翻页式h5

如何实现翻页式h5

翻页式H5的实现方法 翻页式H5通常指通过滑动或点击切换页面的交互形式,常用于营销活动、产品展示等场景。以下是几种常见的实现方式: 使用HTML5和CSS3实现基础翻页 通过CSS3的transf…

vue实现预览效果

vue实现预览效果

实现预览效果的常见方法 在Vue中实现预览效果可以通过多种方式完成,以下列举几种典型场景和对应的实现方案: 图片/文件预览 使用<input type="file">结合FileRead…

vue实现滑动翻页

vue实现滑动翻页

Vue 实现滑动翻页的方法 使用 touch 事件监听 在 Vue 组件中监听 touchstart、touchmove 和 touchend 事件,通过计算滑动距离判断翻页方向。 <tem…

vue实现玻璃效果

vue实现玻璃效果

实现玻璃效果的方法 在Vue中实现玻璃效果(毛玻璃或磨砂玻璃)可以通过CSS的backdrop-filter属性结合Vue的动态样式绑定来实现。以下是具体步骤: 使用CSS backdrop-fi…

vue 实现toggle效果

vue 实现toggle效果

使用 v-show 实现 Toggle 通过 v-show 指令可以控制元素的显示与隐藏,适合频繁切换的场景。v-show 仅切换 CSS 的 display 属性,不会销毁和重建 DOM。 <…