uniapp实现书架

uniapp实现书架

来咯先看效果

小程序效果如下

app 效果如下:

文章目录


前言

哦豁,前几天遇到一个需求,做一个图书馆这样的,我看别人都是书架这种

写着写着感觉还是挺容易的,就是调试样式比较麻烦而已,app 小程序都适配


一、来看看代码吧?

我自己已经对接了接口了

html 复制代码
<template>
	<view class="container">
		<view class="head"
			:style="{ height: isScrolled ? '7.8rem' : '7rem', backgroundColor: isScrolled ? 'rgb(26 51 86 / 73%)' : '#1a3356'}">
			<view style="color:#fff;position: fixed;top: 4rem;left: 1rem;">
				电子图书馆 <br>
				<text style="font-size: 11px;">Original designer</text>
			</view>
		</view>
		<view style="width: 100%; height: 7rem;"></view>

		<scroll-view scroll-y style="height: 100vh;" class="content">
			<view class="bookshelf">
				<view v-for="(book, index) in paginatedBooks" :key="book.id"
					:class="['book-item', isInLastTwoRows(index) ? 'small-book' : '']" @click="openBook(book)">
					<text class="book-title">{{ book.name }}</text>
					<view class="book-cover-container">
						<image :src="book.coverImage" class="book-cover" />
						<view class="book-spine"></view>
					</view>

				</view>
			</view>
			<button class="prev-page-button" @click="prevPage">上一页</button>
			<button class="next-page-button" @click="nextPage">下一页</button>
		</scroll-view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		getbookList
	} from "@/api/ebook/ebook_api.js";

	export default {
		data() {
			return {
				isScrolled: false,
				innerAudioContext: null,
				books: [],
				pageNum: 1,
				pageSize: 9,
				totalPages: 1
			};
		},
		computed: {
			// 当前分页显示的数据
			paginatedBooks() {
				return this.books;
			}
		},
		mounted() {
			this.innerAudioContext = uni.createInnerAudioContext();
			this.innerAudioContext.src = 'https://linda-uni.oss-cn-guangzhou.aliyuncs.com/images/integration/no_input.mp3';
			this.fetchBooks(); // 初始化加载第一页数据
		},
		methods: {
			// 调用接口获取图书数据
			async fetchBooks() {
				try {
					const response = await getbookList({
						pageNum: this.pageNum,
						pageSize: this.pageSize
					});

					this.books = response.records;
					this.totalPages = Math.ceil(response.total / this.pageSize);

				} catch (error) {
					console.error("Failed to fetch books:", error);
				}
			},
			openBook(item) {


				const itemData = encodeURIComponent(JSON.stringify(item));
				uni.vibrateShort();
				this.innerAudioContext.play(); // 播放音频
				this.$refs.uToast.show({
					type: 'loading',
					title: '稍等,马上就好...',
					message: '稍等,马上就好...',
					iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/loading.png',
					complete() {
						uni.navigateTo({
							url: `/pages/library/ebookdetails/ebookdetails?itemData=${itemData}`,
						});
					},
				});


			},
			// 判断书籍是否在倒数两排,倒数两排将应用缩小样式
			isInLastTwoRows(index) {
				const booksPerRow = 3;
				const totalRows = Math.ceil(this.books.length / booksPerRow);
				const currentRow = Math.ceil((index + 1) / booksPerRow);
				return currentRow >= totalRows - 1;
			},
			// 点击下一页
			nextPage() {
				if (this.pageNum < this.totalPages) {
					this.pageNum += 1;
					this.fetchBooks();
				} else {
					console.log("已经是最后一页");
				}
			},
			// 点击上一页
			prevPage() {
				if (this.pageNum > 1) {
					this.pageNum -= 1;
					this.fetchBooks();
				} else {
					console.log("已经是第一页");
				}
			}
		},
		beforeDestroy() {
			if (this.innerAudioContext) {
				this.innerAudioContext.destroy();
			}
		}
	};
</script>

<style>
	page {
		background-color: #F5F3FC;
	}

	.container {
		position: relative;
		height: 100vh;
		width: 100vw;
		overflow: hidden;
	}

	.head {
		width: 100%;
		background-color: #1a3356;
		position: fixed;
		top: 0px;
		left: 0%;
		z-index: 99999;
	}

	.bookshelf {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		grid-template-rows: 160px 160px 160px;
		grid-gap: 10px;
		width: 100%;
		background-image: url('https://linda-uni.oss-cn-guangzhou.aliyuncs.com/images/and/staff_1024.jpg');
		background-size: cover;
		height: 100vh;
	}

	.book-item {
		width: 74px;
		height: 120px;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 49px;
		margin-left: 34px;
	}

	.small-book {
		width: 74px;
		height: 113px;

		margin-top: 60px;
	}

	.book-cover-container {
		position: relative;
		width: 100%;
		height: 100%;
		transform-style: preserve-3d;
	}

	.book-cover {
		width: 100%;
		height: 100%;
		border-radius: 5px;
		box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
		backface-visibility: hidden;
	}

	.book-spine {
		position: absolute;
		top: 0;
		right: -3px;
		width: 5px;
		height: 100%;
		background: linear-gradient(180deg, #ded5d0, #a49994);
		border-top-right-radius: 5px;
		border-bottom-right-radius: 5px;
		box-shadow: inset -2px 0px 5px rgba(0, 0, 0, 0.2);
		transform: rotateY(15deg);
		backface-visibility: hidden;
	}

	.book-title {
		margin-top: 5px;
		font-size: 12px;
		color: #ffffffcc;
		text-align: center;
		position: relative;
		top: -0.5rem;
		left: 0px;
		border-radius: 6px;

	}

	.next-page-button {
		width: 100px;
		align-self: center;
		padding: 10px;
		font-size: 14px;
		color: #fff;
		background-color: #fff8f547;
		border: none;
		border-radius: 5px;
		cursor: pointer;
		position: fixed;
		right: 11px;
		bottom: 72px;
	}

	.prev-page-button {
		width: 100px;
		align-self: center;
		padding: 10px;
		font-size: 14px;
		color: #fff;
		background-color: #fff8f547;
		border: none;
		border-radius: 5px;
		cursor: pointer;
		position: fixed;
		left: 36px;
		bottom: 72px;
	}

	/* 针对更大屏幕的调整 */
	@media (min-width: 375px) and (max-width: 400px) {
		.next-page-button {
			bottom: 4.5rem;
		}

		.prev-page-button {
			bottom: 4.5rem;
		}

		.book-item {
			margin-top: 49px;
		}

		.small-book {
			margin-top: 60px;
		}
	}

	@media (min-width: 401px) {
		.next-page-button {
			bottom: 6rem;
		}

		.prev-page-button {
			bottom: 6rem;
		}

		.book-item {
			margin-top: 4rem;
		}

		.small-book {
			margin-top: 6rem;
		}
	}
</style>

点进去的话就是真实的翻书效果 嘎嘎的

总结

人生就是无常,漠漠水田飞白鹭,阴阴夏木啭黄鹂。来看看小老弟的小程序

相关推荐
web150850966415 小时前
在uniapp Vue3版本中如何解决webH5网页浏览器跨域的问题
前端·uni-app
.生产的驴7 小时前
SpringBoot 对接第三方登录 手机号登录 手机号验证 微信小程序登录 结合Redis SaToken
java·spring boot·redis·后端·缓存·微信小程序·maven
汤姆yu13 小时前
基于微信小程序的乡村旅游系统
微信小程序·旅游·乡村旅游
曲辒净14 小时前
微信小程序实现二维码海报保存分享功能
微信小程序·小程序
何极光15 小时前
uniapp小程序样式穿透
前端·小程序·uni-app
User_undefined1 天前
uniapp Native.js 调用安卓arr原生service
android·javascript·uni-app
流氓也是种气质 _Cookie1 天前
uniapp blob格式转换为video .mp4文件使用ffmpeg工具
ffmpeg·uni-app
oil欧哟1 天前
🤔认真投入一个月做的小程序,能做成什么样子?有人用吗?
前端·vue.js·微信小程序
爱笑的眼睛111 天前
uniapp 极速上手鸿蒙开发
华为·uni-app·harmonyos
汤姆yu1 天前
基于微信小程序的消防隐患在线举报系统
微信小程序·小程序·消防隐患