当前位置:网站首页>uniapp 实现微信小程序全局分享及自定义分享按钮样式
uniapp 实现微信小程序全局分享及自定义分享按钮样式
2022-07-03 09:09:00 【drhrht】
uniapp 实现微信小程序的全局 转发给好友/分享到朋友圈 的功能。主要使用 Vue.js 的 全局混入 概念。
下面直接上 实现步骤和代码:
创建全局分享内容文件
1.创建一个全局分享的 js 文件。示例文件路径为:@/common/share.js ,在该文件中定义全局分享的内容:
export default {
data() {
return {
// 默认的全局分享内容
share: {
title: '全局分享的标题',
path: '/pages/home/home', // 全局分享的路径,比如 首页
imageUrl: '/static/imgs/fenxiang-img.png', // 全局分享的图片(可本地可网络)
}
}
},
// 定义全局分享
// 1.发送给朋友
onShareAppMessage(res) {
return {
title: this.share.title,
path: this.share.path,
imageUrl: this.share.imageUrl,
}
},
//2.分享到朋友圈
onShareTimeline(res) {
return {
title: this.share.title,
path: this.share.path,
imageUrl: this.share.imageUrl,
}
},
}
引入并全局注册该文件
2.在项目的 main.js 文件中引入该 share.js 文件并使用Vue.mixin() 方法将之全局混入:
// 导入并挂载全局的分享方法
import share from '@/common/share.js'
Vue.mixin(share)
下面来看一下全局的分享效果:
自定义页面分享内容
3.如果在特定页面需要自定义分享内容,也仍旧可以使用页面的onShareAppMessage() 和onShareTimeline() 方法自定义分享的内容,全局的分享会被页面定义的分享内容覆盖。示例如下:
onLoad() {},
// 自定义此页面的转发给好友(已经有全局的分享方法,此处会覆盖全局)
onShareAppMessage(res) {
return {
title: '页面分享的标题',
path: '/pages/my/my',
imageUrl: '/static/imgs/mylogo.png'
}
},
// 自定义页面的分享到朋友圈
onShareTimeline(res) {
return {
title: '页面分享的标题',
path: '/pages/my/my',
imageUrl: '/static/imgs/mylogo.png'
}
},
注:onShareAppMessage() 和onShareTimeline() 方法是和onLoad ,methods 等方法同级的。
实现自定义分享按钮样式的效果
效果展示:
解析:以上两种风格都是比较常见的微信小程序分享按钮自定义样式,然而实际我们并非是对 按钮进行这样的自定义样式,而是将这样的样式做到 上或者直接是一个 image 图片,而要实现点击它触发分享,我们只需要在它的上面盖一个 按钮,这个按钮当然就是用来触发我们的分享功能的,而我们只需要让它的透明度为完全透明就好了,这样一搭配起来,我们效果上就好像实现了对分享按钮的自定义一般。示例代码如下:
1.HTML 代码如下
<!-- 一键分享 -->
<view class="share">
<u-image src="/static/imgs/yjfx.png" width="110" height="110"></u-image>
<u-button id="shareBtn" open-type="share" ></u-button>
</view>
2.CSS 代码如下(这里采用的是 SCSS 的写法)
.share {
width: 110rpx;
height: 110rpx;
border-radius: 50%;
position: absolute;
top: 50%;
right: 29rpx;
#shareBtn {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
opacity: 0;
}
}
这样我们就实现了 “自定义分享按钮” 的效果了,核心之处就在与放一个分享按钮到我们自定义样式部位的上方,让分享按钮完全盖住它并且让这个分享按钮完全透明即可。
边栏推荐
- NR PUCCH format0 sequence generation and detection mechanism
- Starting from 0, use pnpm to build a demo managed by monorepo
- How MySQL modifies null to not null
- UCI and data multiplexing are transmitted on Pusch - determine the bit number of harqack, csi1 and csi2 (Part II)
- LeetCode每日一题(1856. Maximum Subarray Min-Product)
- Common software open source protocols
- 一款开源的Markdown转富文本编辑器的实现原理剖析
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
- Global KYC service provider advance AI in vivo detection products have passed ISO international safety certification, and the product capability has reached a new level
- 1922. Count Good Numbers
猜你喜欢
Design and development of biological instruments
Common software open source protocols
Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
Win10 install elk
[CSDN]C1训练题解析_第四部分_Web进阶
UCI and data multiplexing are transmitted on Pusch (Part V) -- polar coding
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 5 --blinker_ MIOT_ MULTI_ Outside (lighting technology app + Xiaoai classmate control socket multiple jacks)
STM32 serial port usart1 routine
Hudi quick experience (including detailed operation steps and screenshots)
Nr-prach:prach format and time-frequency domain
随机推荐
The number of weak characters in the game (1996)
Quickly use markdown to edit articles
STM32 port multiplexing and remapping
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)
CEF下载,编译工程
Matlab reads hexadecimal numbers and converts them into signed short
Leetcode daily question (2305. fair distribution of cookies)
Convert IP address to int
Stm32-hal library learning, using cubemx to generate program framework
UCI and data multiplexing are transmitted on Pusch - Part I
C language enumeration type
MYSQL数据库底层基础专栏
Flink learning notes (IX) status programming
Vscode Arduino installation Library
Hudi quick experience (including detailed operation steps and screenshots)
Electronic product design
Nr--- Pusch I: sorting out the agreement process
解决Editor.md上传图片获取不到图片地址问题
Jestson Nano 从tftp服务器下载更新kernel和dtb
Make the most basic root file system of Jetson nano and mount NFS file system on the server