uni-app 自定义图片按钮

背景

最近想用uni-app写个简单的APP,为增加特效,想使用类似游戏里边的图片按钮,按下缩小,松手还原,然后可以触发对应事件。但是找了一圈发现好像并没有原生支持,所以就自己写了一个,这里记录一下。

代码实现

1. 图片按钮代码组件定义:imgButton.vue

<template>
	<view>
		<image :src="props.src" @touchstart="handleMouseDown" @mousedown="handleMouseDown" @touchend="handleMouseUp" @mouseup="handleMouseUp"
			:style="{width:'100%', height:'100%',margin: 'auto',  transform: scale}"
			@contextmenu.prevent="preventDefault" :draggable="false" />
	</view>
</template>

<script setup>
	import {
		ref,
		defineEmits
	} from 'vue';
	const props = defineProps(['src'])
	const emit = defineEmits(['up', 'down'])
	const scale = ref('scale(1)')
	// 按住鼠标或者屏幕
	const handleMouseDown = () => {
		scale.value = 'scale(0.9)'
		emit('down')
	}
	// 松开鼠标或者屏幕
	const handleMouseUp = () => {
		scale.value = 'scale(1)'
		emit('up')
	}

	const preventDefault = (event) => {
		event.preventDefault()
	}
</script>

<style>

</style>

2. 测试代码

<template>
	<view class="container">
		<!-- 运动控制 -->
		<uni-section title="运动控制" type="line" padding style="background-color: transparent;">
			<uni-grid :column="3" :highlight="false" border-color="green" :show-border="false"
				style="width: 80%;margin: 0 auto;">
				<uni-grid-item></uni-grid-item>
				<uni-grid-item><img-button src="../../static/WX20240507-105057@2x.png"
						style="width: 100%;height: 100%;transform: rotate(-90deg);margin: auto;" @down="log('down1')"
						@up="log('up1')"></img-button></uni-grid-item>
				<uni-grid-item></uni-grid-item>

				<uni-grid-item><img-button src="../../static/WX20240507-105057@2x.png"
						style="width: 100%;height: 100%;transform: rotate(180deg);margin: auto;" @down="log('down2')"
						@up="log('up2')"></img-button></uni-grid-item>
				<uni-grid-item></uni-grid-item>
				<uni-grid-item><img-button src="../../static/WX20240507-105057@2x.png"
						style="width: 100%;height: 100%;transform: rotate(0deg);margin: auto;" @down="log('down3')"
						@up="log('up3')"></img-button></uni-grid-item>

				<uni-grid-item></uni-grid-item>
				<uni-grid-item><img-button src="../../static/WX20240507-105057@2x.png"
						style="width: 100%;height: 100%;transform: rotate(90deg);margin: auto;" @down="log('down4')"
						@up="log('up4')"></img-button></uni-grid-item>
				<uni-grid-item></uni-grid-item>
			</uni-grid>
		</uni-section>
		<view>{{ logText }}</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import imgButton from './imgButton'
	const logText = ref('111')
	const log = (msg) => {
		logText.value = msg
	}
</script>

<style>
	.container {
		padding: 10rpx;
		font-size: 14rpx;
		line-height: 24rpx;
		overflow: hidden;
	}

</style>

运行效果

在这里插入图片描述
在这里插入图片描述

  • 28
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app是一个跨平台的开发框架,可以用于开发多个平台的应用程序。在uni-app中,可以通过自定义底部导航栏来实现个性化的界面设计和功能需求。 以下是一种实现自定义底部导航栏的方法: 1. 首先,创建一个自定义组件,用于显示底部导航栏的每个按钮。可以使用`uni-icons`组件来显示图标,并添加相应的点击事件。 ```vue <template> <view> <view class="tabbar"> <view class="tabbar-item" @click="navigateTo('home')"> <uni-icons type="home"></uni-icons> <text>首页</text> </view> <view class="tabbar-item" @click="navigateTo('discover')"> <uni-icons type="discover"></uni-icons> <text>发现</text> </view> <view class="tabbar-item" @click="navigateTo('mine')"> <uni-icons type="mine"></uni-icons> <text>我的</text> </view> </view> </view> </template> <script> export default { methods: { navigateTo(page) { uni.navigateTo({ url: `/pages/${page}/${page}` }); } } } </script> <style> .tabbar { display: flex; justify-content: space-around; align-items: center; height: 100px; background-color: #f5f5f5; } .tabbar-item { display: flex; flex-direction: column; align-items: center; } </style> ``` 2. 在需要显示底部导航栏的页面中,引入自定义组件,并在页面的底部位置使用该组件。 ```vue <template> <view> <!-- 页面内容 --> </view> <custom-tabbar></custom-tabbar> </template> <script> import CustomTabbar from '@/components/CustomTabbar.vue'; export default { components: { CustomTabbar } } </script> ``` 通过以上步骤,你可以实现一个自定义的底部导航栏,并在不同的页面之间进行切换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值