如何实现网页分享到微信,微博,空间

南楼画角 提交于 2020-04-08 03:53:25

客观一点来说,实现分享功能的并不是我们,而是手机自带了这些分享功能,但是需要我们在中间做一个桥梁,将分享的条件准备好,具体步骤如下

先拿分享到微信为例:

1.手机上自带有分享到微信这个功能,但是分享到微信是需要二维码的,所以我们所要做的工作就是在PC端或者移动端生成一个二维码就行了。

js 有专门生成二维码的库:https://cdn.bootcss.com/jquery.qrcode/1.0/jquery.qrcode.min.js 可以在bootcdn 上搜到

以cdn为例子,生成cdn的二维码

var path = "https://www.bootcdn.cn/jquery/";

$("#qrcode").qrcode({
text: path, //设置二维码内容
render: "table", //设置渲染方式
width: 256, //设置宽度,默认生成的二维码大小是 256×256
height: 256, //设置高度
typeNumber: -1, //计算模式
background: "#ffffff", //背景颜色
foreground: "#000000" //前景颜色
}

);

其中#qrcode为一个盛放二维码的容器,这个自己定义就行,目的就是为了让二维码有一个在前端页面显示的位置

这样就已经完成了  扫描以后 手机上右上角有一个分享的功能  点击就会有提示分享到微信了

2.分享到微博,空间

这个以分享到微博为例

 

//新浪微博分享部分
var ShareTip = function() {

}
//分享到腾讯微博
ShareTip.prototype.sharetoqq = function(content, url, picurl) {
var shareqqstring = 'http://v.t.qq.com/share/share.php?title=' + content + '&url=' + url + '&pic=' + picurl;
window.open(shareqqstring, 'newwindow', 'height=100,width=100,top=100,left=100');
}
//分享到新浪微博
ShareTip.prototype.sharetosina = function(title, url, picurl) {
var sharesinastring = 'http://v.t.sina.com.cn/share/share.php?title=' + title + '&url=' + url + '&content=utf-8&sourceUrl=' + url + '&pic=' + picurl;
window.open(sharesinastring, 'newwindow', 'height=400,width=400,top=100,left=100');
}
//分享到QQ空间
ShareTip.prototype.sharetoqqzone = function(title, url, picurl) {
var shareqqzonestring = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?summary=' + title + '&url=' + url + '&pics=' + picurl;
window.open(shareqqzonestring, 'newwindow', 'height=400,width=400,top=100,left=100');
}

$(".a-weibo").click(function() {
var share1 = new ShareTip();
share1.sharetosina("从构建分布式秒杀系统聊聊限流特技", window.location.href, "");

})

调用一下现有的方法就可以了。

复制这些代码 然后利用事件触发这段脚本就可以了

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!