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>

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

总结

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

相关推荐
摇头的金丝猴1 小时前
uniapp vue3 使用echarts-gl 绘画3d图表
前端·uni-app·echarts
小远yyds3 小时前
跨平台使用高德地图服务
前端·javascript·vue.js·小程序·uni-app
qq22951165025 小时前
uniapp+vue加油服务系统 微信小程序
vue.js·微信小程序·uni-app
重生之我是菜鸡程序员6 小时前
uniapp 使用vue/pwa
javascript·vue.js·uni-app
2401_844139037 小时前
Java爱情交友婚恋系统小程序源码
微信·微信小程序·小程序·微信公众平台·交友·微信开放平台
qq22951165027 小时前
asp.net+uniapp养老助餐管理系统 微信小程序
微信小程序·uni-app·asp.net
qq22951165028 小时前
微信小程序 uniapp网络记账设计个人理财系统
微信小程序·小程序·uni-app
二狗mao15 小时前
Uniapp的学习
前端·vue.js·uni-app
azhou的代码园19 小时前
基于SpringBoot+微信小程序+协同过滤算法+二维码订单位置跟踪的农产品销售平台-新
spring boot·算法·微信小程序
苹果电脑的鑫鑫21 小时前
uni-app接口调用以及封装
前端·javascript·uni-app