使用到的接口(测试时需要登录,网址和图片必须是公网的,不能localhost,QQ图片不能太宽,太宽标题描述会undefiend):
1.分享到QQ空间接口:
?url=你的网址
&sharesource=qzone
&title=你的分享标题
&pics=你的分享图片
&summary=你的分享描述信息
2.分享给QQ好友接口:
?url=你的分享网址
&sharesource=qzone
&title=你的分享标题
&pics=你的分享图片地址
&summary=你的分享描述
&desc=你的分享简述
3.分享到新浪微博接口:
service.weibo.com/share/share…
?url=你的分享网址
&sharesource=weibo
&title=你的分享标题
&pic=你的分享图片
&appkey=你的key需要在新浪微博开放平台中申请
一键分享代码参考如下:
html
<div>分享到:</div>
- <div οnclick="shareTo('qzone')">
- <img src="zixuephp.net/static/imag…>
- </div>
- <div οnclick="shareTo('qq')">
- <img src="zixuephp.net/static/imag…
- </div>
- <div οnclick="shareTo('sina')">
- <img src="zixuephp.net/static/imag…
- </div>
- <div οnclick="shareTo('wechat')">
- <img src="zixuephp.net/static/imag…
- </div>
js
-
function shareTo(types){
-
var title,imageUrl,url,description,keywords;
-
//获取文章标题
-
title = document.title;
-
//获取网页中内容的第一张图片地址作为分享图
-
//imageUrl = document.images[0].src;
-
imageUrl = document.getElementById("pcdetails").getElementsByTagName("img")[0];
-
//当内容中没有图片时,设置分享图片为网站logo
-
if(typeof imageUrl == 'undefined'){
-
imageUrl = 'https://'+window.location.host+'/static/images/logo.png';
-
} else {
-
imageUrl = imageUrl.src;
-
}
-
//获取当前网页url
-
url = document.location.href;
-
//获取网页描述
-
description = document.querySelector('meta[name="description"]').getAttribute('content');
-
//获取网页关键字
-
keywords = document.querySelector('meta[name="keywords"]').getAttribute('content');
-
//qq空间接口的传参
-
if(types=='qzone'){
-
window.open('sns.qzone.qq.com/cgi-bin/qzs…);
-
}
-
//新浪微博接口的传参
-
if(types=='sina'){
-
window.open('service.weibo.com/share/share…');
-
}
-
//qq好友接口的传参
-
if(types == 'qq'){
-
window.open('connect.qq.com/widget/shar…);
-
}
-
//生成二维码给微信扫描分享
-
if(types == 'wechat'){
-
//在线二维码(服务器性能限制,仅测试使用,屏蔽非大陆ip访问)
-
window.open('zixuephp.net/inc/qrcode_…);
-
}
-
}
使用说明:
纯js使用时id改成自己站点的id选择器来获取。如果调试不成功,可以尝试本站中的分享功能,分享时会打开新窗口,那条链接是最终要分享的,已经拼接好的参数链接,可以复制进行比对参考。
最终分享链接示例:
1.分享到qq空间:
2.分享到qq好友:
3.分享到新浪微博:
4.分享到微信(建议jquery.qrcode.js生成二维码方式)
jquery生成二维码方式预览:
jquery.qrcode.js生成二维码demo.html
下载代码:
分享效果点击本站文章分享功能,效果图如下: