uniapp实现分享弹窗功能

在业务需求中,我们总是遇见需要点击按钮实现分享页面弹出分享界面的功能,所以我就简单实现了一下这个功能。

效果图:

 Share组件:

<template>
	<view class="share">
		<view :class="{'share-box': shareState}" @click="handleHiddenShare">
		</view>
		<view class="share-item" :class="{'share-show': shareState}">
			<view class="share-to">
				<text>分享到</text>
			</view>
			<view class="content">

				<view class="block">
<button class="wechat" open-type="share">
					<image src="/static/img/wechat.png" mode="aspectFill"></image>
			
					<text>微信</text>
		</button>
				</view>
				<view class="block" @click="toUrl()">
					<button class="moment">
					<image src="/static/img/moment.png" mode="aspectFill"></image>					
					<text>朋友圈</text>
</button>
				</view>
			</view>
			<view class="cancel" @click.stop="handleHiddenShare">
				<text>取消</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			carInfo: []
		},
		data() {
			return {
				shareState: false,
			};
		},
		onShareAppMessage(res) {
			return {
				title: '页面分享的标题',
				path: '/pages/my/my',
				imageUrl: '/static/imgs/mylogo.png'
			}
		},
		methods: {
			toUrl() {
				uni.navigateTo({
					url: '/pages/share/poster?carId=' + this.carInfo.car_id,
				})
				this.handleHiddenShare()
			},
			// 显示分享
			handleShowShare() {
				this.shareState = true;
			},
			// 隐藏分享
			handleHiddenShare() {
				this.shareState = false;
			}
		}
	}
</script>

<style lang="less">
	button::after{border: initial;}
	.share {
		width: 100%;
		height: 100%;
	}

	.share-box {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0rpx;
		left: 0rpx;
		bottom: 0rpx;
		right: 0rpx;
		background-color: rgba(0, 0, 0, 0.4);
		transition: .3s;
		z-index: 999;
	}

	.block>button {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: white;
		border-style: none;
		width: 300rpx;
		height: 180rpx;
		// outline:none;
		// opacity: 0;
	}

	// 进入分享动画
	.share-show {
		transition: all 0.3s ease;
		transform: translateY(0%) !important;
	}

	// 离开分享动画
	.share-item {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		height: auto;
		background-color: #FFFFFF;
		transition: all 0.3s ease;
		transform: translateY(100%);
		z-index: 1999;

		.share-to {
			width: 100%;
			height: 3rem;
			display: flex;
			justify-content: center;
			align-items: center;

			&::after {
				content: '';
				width: 240rpx;
				height: 0rpx;
				border-top: 1px solid #E4E7ED;
				-webkit-transform: scaleY(0.5);
				transform: scaleY(0.5);
				margin-left: 30rpx;
			}

			&::before {
				content: '';
				width: 240rpx;
				height: 0rpx;
				border-top: 1px solid #E4E7ED;
				-webkit-transform: scaleY(0.5);
				transform: scaleY(0.5);
				margin-right: 30rpx;
			}
		}

		.content {
			width: 100%;
			height: auto;
			display: flex;
			flex-wrap: wrap;

			.block {
				width: 50%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				height: 180rpx;

				image {
					width: 80rpx;
					height: 80rpx;
				}

				text {
					margin-top: 16rpx;
					font-size: 28rpx;
					color: #606266;
				}
			}
		}

		.cancel {
			width: 100%;
			height: 3rem;
			display: flex;
			justify-content: center;
			align-items: center;
			border-top: 1rpx solid #E4E7ED;
		}
	}
</style>

父组件:

通过点击事件触发Share组件的显示函数来改变Share组件的分享状态

<template>
	<div>
		<button @click="showShare">
			分享
		</button>
		<Share ref="refShare" :carInfo='carInfo'></Share>
	</div>
</template>
<script>
	import Share from '@/components/share.vue'
	export default{
		components:{
			Share
		},
		data(){
			return{
				
			}
		},
		methods:{
			showShare() {
				this.$refs.refShare.handleShowShare();
			},
		}
	}
</script>

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用uniapp分享组件,然后调用微信的分享API。具体步骤如下: 1. 在manifest.json文件中添加微信分享的权限: ```json "mp-weixin": { "appid": "your_appid", "permission": { "scope.userLocation": { "desc": "你的位置信息将用于小程序位置接口的效果展示" }, "scope.userInfo": { "desc": "你的用户信息将用于小程序内的用户身份识别" }, "scope.userLocationBackground": { "desc": "你的位置信息将在后台运行时持续获取,用于小程序接口的效果展示" }, "scope.writePhotosAlbum": { "desc": "你的相册将用于小程序的效果展示" }, "scope.camera": { "desc": "你的摄像头将用于小程序的效果展示" } }, "usingComponents": { "share-button": "/components/share-button/share-button" } } ``` 2. 在页面中引入分享组件: ```vue <template> <div> <share-button :title="title" :desc="desc" :imgUrl="imgUrl" :link="link" @success="onShareSuccess" @cancel="onShareCancel" > <button>分享到微信</button> </share-button> </div> </template> <script> import shareButton from "@/components/share-button/share-button.vue"; export default { components: { shareButton }, data() { return { title: "分享标题", desc: "分享描述", imgUrl: "分享图片链接", link: "分享链接" }; }, methods: { onShareSuccess(res) { console.log("分享成功", res); }, onShareCancel(res) { console.log("分享取消", res); } } }; </script> ``` 3. 在分享组件中调用微信分享API: ```vue <template> <button @click="showShareMenu"> <slot></slot> </button> </template> <script> export default { methods: { showShareMenu() { uni.showShareMenu({ withShareTicket: true, success: res => { console.log("打开分享菜单成功", res); }, fail: err => { console.log("打开分享菜单失败", err); } }); }, onShareSuccess(res) { this.$emit("success", res); }, onShareCancel(res) { this.$emit("cancel", res); }, onShareAppMessage() { return { title: this.title, desc: this.desc, imageUrl: this.imgUrl, path: this.link, success: this.onShareSuccess, cancel: this.onShareCancel }; } } }; </script> ``` 这样就可以实现uniapp中弹出微信分享窗口了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值