uni-app移动端图片预览组件 movable-area 、movable-view (支持缩放,拖动效果、替换部分代码图片可直接使用)

UniApp图片预览组件

利用uni-app官方<movable-area>、<movable-view>内置视图组件 配合 uView 组件的u-popup 弹框组件共同实现封装的图片预览组件,支持手指缩放、拖动效果,替换代码中部分图片后 可以直接使用。

效果图:

组件代码:

c 复制代码
//图片预览组件代码
<template>
	<u-popup :show="showPicPreview" mode="center" @close="close" @open="open">
		<view class="picPreview">
			<view class="closePicPreview" @tap="closePicPreviewHandle()">
				<image src="../../static/img/x.png" mode=""></image> //预览组件关闭隐藏按钮
			</view>
			<movable-area scale-area>
				<movable-view direction="all" @scale="onScale" scale="true" scale-min="1" scale-max="4" :scale-value="scale">
					<image :src="previewUrl" mode="widthFix"></image> //需要展示的图片
				</movable-view>
			</movable-area>
		</view>
	</u-popup>
</template>
<script>
	export default {
		name: "photo-preview",
		props: {
			//父组件控制显示
			showPicPreview: {
				type: Boolean,
				require: true,
				default: false
			},
			//父组件传入图片url
			previewUrl: {
				type: String,
				default: ''
			},
		},
		data() {
			return {};
		},
		methods: {
			close() {},
			open() {},
			onScale() {},
			closePicPreviewHandle() {
				this.$emit('closePicPreviewHandle')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.picPreview {
		position: relative;
		width: 750rpx;
		height: 100vh;
		z-index: 99999;
		background-color: rgba(0, 0, 0, 0.2);
		.closePicPreview {
			position: absolute;
			z-index: 99;
			right: 55rpx;
			top: 145rpx;
			width: 45rpx;
			height: 45rpx;
			image {
				width: 100%;
				height: 100%;
			}
		}
		movable-view {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 100%;
		}
		movable-area {
			height: 100%;
			width: 100%;
			position: fixed;
			overflow: hidden;
		}
		movable-view image {
			width: calc(100% - 300rpx);
		}
	}
</style>



//组件的调用方法
//页面中使用图片预览组件
<template>
<PhotoPreview :showPicPreview="showPicPreview" @closePicPreviewHandle="closePicPreviewHandle()" :previewUrl="previewUrl"></PhotoPreview>
//showPicPreview:控制预览组件的显示,
//previewUrl:传入需要预览显示的图片url,
//closePicPreviewHandle : 关闭当前弹出层的事件
</template>

提示:注意,组件的要注册后再使用,切记!!

官方链接:

movable-area

相关推荐
abigale032 小时前
webpack+vite前端构建工具 -11实战中的配置技巧
前端·webpack·node.js
专注API从业者2 小时前
构建淘宝评论监控系统:API 接口开发与实时数据采集教程
大数据·前端·数据库·oracle
Joker`s smile2 小时前
Chrome安装老版本、不同版本,自制便携版本用于前端调试
前端·chrome
weixin_416639972 小时前
爬虫工程师Chrome开发者工具简单介绍
前端·chrome·爬虫
我是如子啊2 小时前
【解决“此扩展可能损坏”】Edge浏览器(chrome系列通杀))扩展损坏?一招保留数据快速修复
前端·chrome·edge
灵性花火2 小时前
Qt的前端和后端过于耦合(0/7)
开发语言·前端·qt
孤水寒月6 小时前
基于HTML的悬窗可拖动记事本
前端·css·html
祝余呀6 小时前
html初学者第一天
前端·html
脑袋大大的7 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
速易达网络8 小时前
RuoYi、Vue CLI 和 uni-app 结合构建跨端全家桶方案
javascript·vue.js·低代码