当前位置: 首页 > news >正文

uniapp h5端和app端 使用 turn.js

前提:添加页后,添加页与当前页会重叠在一起,不知道为什么,没有找到解决办法

1.h5端

<template>
	<view class="container">
		<view id="flipbook">
			<view class="page page1">Page 1</view>
			<view class="page page2">Page 2</view>
			<view class="page page3">Page 3</view>
			<view class="page page4">Page 4</view>
			<view class="page fixed">Page 5</view>
		</view>
	</view>
</template>

<script>
	//npm install jquery turn.js
	import $ from 'jquery';
	import 'turn.js';
	export default {
		data() {
			return {

			}
		},
		mounted() {
			// 确保 DOM 加载完成
			this.$nextTick(() => {
				// 初始化翻书效果
				$('#flipbook').turn({
					width: 400, // 书本宽度
					height: 300, // 书本高度
					autoCenter: true, // 自动居中
					display: 'single',
				});
			});
		},
		methods: {

		}
	}
</script>

<style>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

#flipbook {
  width: 400px;
  height: 300px;
  background-color: #f0f0f0;
}

.page {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fff;
  border: 1px solid #ccc;
  box-sizing: border-box;
  font-size: 20px;
  color: #333;
}
</style>

2.APP 端简单使用

<template>
	<view class="container">
		<!-- 使用 renderjs 的容器 -->
		<view id="flipbook" :change:prop="renderjs.initTurnjs" :prop="{ pages: 4 }">
			<view class="page page1">Page 16+66</view>
			<view class="page page2">Page 277</view>
			<view class="page page3">Page 388</view>
			<view class="page page4">Page 4</view>
		</view>
	</view>
</template>

<script module="renderjs" lang="renderjs">
	export default {
		mounted() {
			// 在 renderjs 中初始化 turn.js
			//this.initTurnjs();
		},
		methods: {
			initTurnjs() {
				// 动态加载 jQuery 和 turn.js
				const loadScript = (src, callback) => {
					const script = document.createElement('script');
					script.src = src;
					script.onload = callback;
					document.head.appendChild(script);
				};
				console.log(2222);
				// 加载 jQuery
				loadScript('http://192.168.1.95:9300/statics/jquery-3.5.1.min.js', () => {
					// 加载 turn.js
					console.log(333);
					loadScript('http://192.168.1.95:9300/statics/turn.min.js', () => {
						// 初始化 turn.js
						console.log(444);
						$('#flipbook').turn({
							width: 300,
							height: 300,
							autoCenter: true,
							display: 'single',
							pages: 4,
							when: {
								turned: function(event, page, view) {
									console.log('Turned to page', page);
									// 在这里可以添加加载新页面的逻辑
								}
							}
						});
					});
				});
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100vh;
	}

	#flipbook {
		width: 300px;
		height: 300px;
		background-color: #f0f0f0;
	}

	.page {
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #fff;
		border: 1px solid #ccc;
		box-sizing: border-box;
		font-size: 20px;
		color: #333;
	}
</style>

3.app端 添加页

<template>
	<view >
		<view id="onePage" v-show="false">
			<view class="content">
				<view class="title">
					{{content.title}}
				</view>
				<view class="details">
					{{content.details}}
				</view>
			</view>
		</view>
		
		<button @click="add">添加一页</button>
		<view class="container">
			<!-- 使用 renderjs 的容器  -->
			<view id="flipbook" :change:listSize="renderjs.initTurnjs" :listSize="listSize"  :addEventRandom="addEventRandom" :change:addEventRandom="renderjs.addEvent">
				<view class="page">欢迎</view>
			</view>
		</view>
	</view>
</template>


<script>
	import Vue from 'vue'
	export default {
		
		components: {
			
		},
		data() {
			return {	
				addEventRandom:null,
				content:{
					title:0,
					details:'未初始化'
				},
				listSize:1,
				currentSize:1
			}
		},
		created() {
		},
		onLoad(option) {
			
		},
		onShow() {
			
		},
		methods: {
			add(){
				//本次添加几页(总页数要是偶数,否则翻到最后一页不能向前翻)
				var addnum=0;
				
				if(this.currentSize % 2 === 0){
					//偶数
					addnum = 2;
					this.currentSize = this.currentSize + 2;
				}else{
					//奇数
					addnum = 1;
					this.currentSize = this.currentSize + 1;
				}
				
				let count = 0;
				const intervalId = setInterval(() => {
				  ++count;
				  
				  //填充数据
				  this.content.title = '标题'+Math.floor(1000 + Math.random() * 9000);
				  this.content.details = '内容'+Math.floor(1000 + Math.random() * 9000);
				  
				  //通知加一页
				  this.addEventRandom = Math.floor(1000 + Math.random() * 9000);
				  
				  if (count === addnum) {
				    clearInterval(intervalId); // 停止定时器
				  }
				}, 1000);
			}
		}	
	}
</script>


<script module="renderjs" lang="renderjs">
	export default {
		data() {
			return {
				turn:null,
				pages:1
			}
		},
		mounted() {
			// 在 renderjs 中初始化 turn.js
			//this.initTurnjs();
		},
		methods: {
			initTurnjs(newValue, oldValue) {
				var that=this;
				this.pages = newValue;
				// 动态加载 jQuery 和 turn.js
				const loadScript = (src, callback) => {
					const script = document.createElement('script');
					script.src = src;
					script.onload = callback;
					document.head.appendChild(script);
				};
				// 加载 jQuery
				loadScript('http://192.168.1.95:9300/statics/jquery-3.5.1.min.js', () => {
					// 加载 turn.js
					loadScript('http://192.168.1.95:9300/statics/turn.min.js', () => {
						// 初始化 turn.js
						that.turn = $('#flipbook').turn({
							width: 300,
							height: 300,
							autoCenter: true,
							display: 'single',
							acceleration:true,
							pages: this.pages,
							when: {
								turned: function(event, page, view) {
									console.log('Turned to page', page);
								},
								first(){
									//console.log('当前页面是第一页');
								},
								last(){
									//console.log('当前页面是最后页');
								}
							}
						});
						//console.log(that.turn);
					});
				});
			},
			addEvent(newValue, oldValue){
				//调试
				//console.log(888);
				//console.log($('#onePage').html());
				
				
				const newPage = $('<div>').addClass('page').html($('#onePage').html());
				this.turn.turn('addPage', newPage, this.pages + 1); // 添加到末尾
				
				this.pages = this.pages + 1
				$("#flipbook").turn("pages", this.pages);							
				
				//调试
				//console.log($('#flipbook').html());	
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100vh;
	}

	#flipbook {
		width: 300px;
		height: 300px;
		background-color: #f0f0f0;
	}

	.page {
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #fff;
		border: 1px solid #ccc;
		box-sizing: border-box;
		font-size: 20px;
		color: #333;
	}
</style>

 两个js文件我也上传百度云盘作为备份

通过网盘分享的文件:uniapp使用turn.js
链接: https://pan.baidu.com/s/199ncUbcdcKaPNK9p-hnoRQ?pwd=hikp 提取码: hikp

jquery.js 是在网上找的

turn.js是在github下载的

Turn.js: The page flip effect in HTML5

GitHub - blasten/turn.js: The page flip effect for HTML5

app端的效果视频

通过网盘分享的文件:6542f4649d16ffe6b78bdf194547af58.mp4
链接: https://pan.baidu.com/s/1LGAGEm7KpibZ2QJTAYczZg?pwd=n37g 提取码: n37g

相关文章:

  • 基于大语言模型的推荐系统(1)
  • 数据库索引:优点、缺点及常见类型
  • 鸿蒙-自定义布局-实现一个可限制行数的-Flex
  • MySQL优化
  • Pandas库:(二)数组对象的索引
  • Python 高级特性-生成器
  • 计算机组成原理——输入/输出系统(十七)
  • 【个人开源】——从零开始在高通手机上部署sd(二)
  • 边缘安全加速(Edge Security Acceleration)
  • Redis 的线程安全问题解析:为什么 Redis 是线程安全的?
  • Unity游戏制作中的C#基础(3)加减乘除算术操作符,比较运算符,逻辑与,或运算符
  • 【opencv】图像基本操作
  • 机器学习 - 参数平均感知器
  • STM32MP157A单片机移植Linux驱动深入版
  • 视频mp4垂直拼接 水平拼接
  • 高级系统架构师--第七章:安全架构设计理论与实践
  • Nginx Embedded Variables 嵌入式变量解析(4)
  • Gradio全解11——使用transformers.agents构建Gradio UI(4)
  • Windows 权限结构和原理:深入浅出
  • 基于flask+vue框架的的医院预约挂号系统i1616(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 《求是》杂志发表习近平总书记重要文章《锲而不舍落实中央八项规定精神,以优良党风引领社风民风》
  • 著名词作家陈哲逝世,代表作《让世界充满爱》《同一首歌》等
  • 市场监管总局召开平台企业支持个体工商户发展座谈会
  • 走进“双遗之城”,领略文武风采:沧州何以成文旅新贵
  • 《新时代的中国国家安全》白皮书(全文)
  • 来伊份深夜回应“粽子中吃出疑似创可贴”:拿到实物后会查明原因