Dshare是专属大前端的分享模块,浩子码。
首先,大前端用过Bshare、百度分享等分享插件。有以下感触:
- 庞大,加载的图片很大,代码很多;
- 给网站新增3个以上的请求;
- 有的自带分享统计,对80%的站点来说没有必要,加载的js可以省了;
对此,有了一个想法,写一个适合大众基本要求的分享模块,让站长具有最高自定义权力,于是,Dshare诞生了(其实也就是一点点js,被我忽悠大了,哈哈)。
Dshare分享模块代码:
//Dshare
dshare();
function dshare() {
var thelink = encodeURIComponent(document.location),
thetitle = encodeURIComponent(document.title.substring(0,60)),
windowName = '分享到',
param = getParamsOfShareWindow(600, 560),
//各大SNS站点的分享机制,可自定义
ds_tqq = 'http://v.t.qq.com/share/share.php?title=' + thetitle + '&url=' + thelink + '&site=',
ds_qzone = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + thelink + '&title=',
ds_tsina = 'http://v.t.sina.com.cn/share/share.php?url=' + thelink + '&title=' + thetitle,
ds_douban = 'http://www.douban.com/recommend/?url=' + thelink + '&title=' + thetitle,
ds_renren = 'http://share.renren.com/share/buttonshare?link=' + thelink + '&title=' + thetitle,
ds_kaixin = 'http://www.kaixin001.com/repaste/share.php?rurl=' + thelink + '&rcontent=' + thelink + '&rtitle=' + thetitle,
ds_facebook = 'http://facebook.com/share.php?u=' + thelink + '&t=' + thetitle,
ds_twitter = 'http://twitter.com/share?url=' + thelink + '&text=' + thetitle;
$('.dshare').each(function() {
$(this).attr('title',windowName + $(this).text());
$(this).click(function() {
var httpUrl = eval($(this).attr('class').substring($(this).attr('class').lastIndexOf('ds_')));
window.open(httpUrl, windowName, param);
});
});
function getParamsOfShareWindow(width, height) {
return ['toolbar=0,status=0,resizable=1,width=' + width + ',height=' + height + ',left=',(screen.width-width)/2,',top=',(screen.height-height)/2].join('');
}
}
jQuery实现,有需要的可以转为js版本
html部分:
<div>分享到:<a>腾讯微博</a><a>QQ空间</a><a>新浪微博</a><a>人人网</a><a>开心网</a><a>豆瓣</a><a>Facebook</a><a>Twitter</a></div>
css部分:
.dshare{padding-left:20px;margin-right:8px;height:16px;line-height:16px;display:inline-block;cursor:pointer;background:url(../img/share.png) no-repeat}
.ds_tqq{background-position:0 0}
.ds_tsina{background-position:0 -16px}
.ds_douban{background-position:0 -32px}
.ds_facebook{background-position:0 -48px}
.ds_twitter{background-position:0 -64px}
.ds_renren{background-position:0 -112px}
.ds_kaixin{background-position:0 -128px}
.ds_qzone{background-position:0 -96px}
所需图片:
http://www.daqianduan.com/wp-content/themes/d4/img/share.png
记得改成你所需要的
如何使用,我就不多说了,代码注释的很详细!!
PS:只是简单的实现基本需求,更多交互有待你的参与。
转载请注明:大前端 » 自码分享到SNS模块-Dshare 更简洁 更友好
主机点评网(Hostucan.cn)
华强北在线招聘前端开发工程师
携程UED招聘前端等职位
很不错呀,收藏了!
为什么我用到网站上就没有效果呢?
在需要显示的地方调用这个函数了吗? 还有就是图片是否上传?
我再试试看,谢谢!
请问高手,bShare的一键通是怎么做的,能帮示范一下吗?网上找不着啊!
没怎么研究过Bshare,不能解答你的问题,so sorry
好文!我正要找的
这个要收下啊。
严重期待中…
好久没来了,变化真大,没想留句话的
,看到那句“严重鄙视飘过不留毛的鸟”
其实更期待D4呀。呵呵
大前端的jquery效果贼棒!特喜欢
研究这个费脑子啊
现在这代码基本了解一点 点
http://www.cnblogs.com/trance/archive/2011/07/27/2118407.html
我也写了一个简单的,增加了自定义标题和链接功能。楼主可以看看
灰常感谢 收下了
还不错啊 前几天也写了一个类似的东西