html部分:
-
<script src="mshare.js">
</script>//引进mshare.js
-
<button data-mshare="0">点击弹出原生分享面板
</button>
-
<button data-mshare="1">点击触发朋友圈分享
</button>
-
<button data-mshare="2">点击触发发送给微信朋友
</button>
js部分:
-
-
var mshare =
new mShare({
-
title:
'Lorem ipsum dolor sit.',
-
-
desc:
'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quaerat inventore minima voluptates.',
-
img:
'http://placehold.it/150x150'
-
-
$(
'button').click(
function () {
-
// 1 ==> 朋友圈 2 ==> 朋友 0 ==> 直接弹出原生
-
mshare.init(+$(
this).data(
'mshare'));
-
-
下面是mshare.js的代码分享,把这些代码新建一个js文件放进去,然后在页面中引进就ok了。
-
-
-
-
-
-
var UA = navigator.appVersion;
-
-
-
-
-
var uc = UA.split('UCBrowser/').length > 1 ? 1 : 0;
-
-
-
-
-
-
-
-
var qq = UA.split('MQQBrowser/').length > 1 ? 2 : 0;
-
-
-
-
-
var wx = /micromessenger/i.test(UA);
-
-
-
-
-
var qqVs = qq ? parseFloat(UA.split('MQQBrowser/')[1]) : 0;
-
var ucVs = uc ? parseFloat(UA.split('UCBrowser/')[1]) : 0;
-
-
-
* 获取操作系统信息 iPhone(1) Android(2)
-
-
-
var ua = navigator.userAgent;
-
-
if (/iphone|ipod/i.test(ua)) {
-
-
} else if (/android/i.test(ua)) {
-
-
-
-
-
-
-
-
-
-
var qqBridgeLoaded = false;
-
-
-
if ((qq && qqVs
< 5.4 && os == 1) || (qq && qqVs < 5.3 && os == 1)) {
-
-
-
if (qq && qqVs < 5.4 && os == 2) {
-
-
-
if (uc && ((ucVs < 10.2 && os == 1) || (ucVs < 9.7 && os == 2))) {
-
-
-
-
-
-
-
* qq浏览器下面 根据不同版本 加载对应的bridge
-
-
* @param {Function} cb 回调函数
-
-
-
-
-
-
-
-
var script = document.createElement('script');
-
script.src = (+qq === 1) ? '//3gimg.qq.com/html5/js/qb.js' : '//jsapi.qq.com/get?api=app.share';
-
-
-
* 需要等加载过 qq 的 bridge 脚本之后
-
-
-
script.onload = function () {
-
-
-
-
document.body.appendChild(script);
-
-
-
-
-
-
-
-
function ucShare(config) {
-
// ['title', 'content', 'url', 'platform', 'disablePlatform', 'source', 'htmlID']
-
-
// ios: kWeixin || kWeixinFriend;
-
// android: WechatFriends || WechatTimeline
-
-
-
-
-
-
-
-
-
platform = config.type == 1 ? 'WechatTimeline' : 'WechatFriends';
-
-
platform = config.type == 1 ? 'kWeixinFriend' : 'kWeixin';
-
-
-
-
shareInfo = [config.title, config.desc, config.url, platform, '', '', ''];
-
-
-
-
ucweb.startRequest && ucweb.startRequest('shell.page_share', shareInfo);
-
-
-
-
-
ucbrowser.web_share && ucbrowser.web_share.apply(null, shareInfo);
-
-
-
-
-
-
-
-
-
-
function qqShare(config) {
-
-
-
-
type = type ? ((type == 1) ? 8 : 1) : '';
-
-
var share = function () {
-
-
-
-
'description': config.desc,
-
-
'img_title': config.title,
-
-
-
-
-
-
browser.app && browser.app.share(shareInfo);
-
-
qb.share && qb.share(shareInfo);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
* @param {Object} config 配置对象
-
-
function mShare(config) {
-
-
-
this.init = function (type) {
-
if (typeof type != 'undefined') this.config.type = type;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
if (typeof module === 'object' && module.exports) {
-
-
-
-
-
好了,这样就可以直接唤起微信进行分享啦