自码分享到SNS模块-Dshare 更简洁 更友好

2011-08-04    分类:JavaScript    18人评论4,457次浏览

Dshare是专属大前端的分享模块,浩子码。

首先,大前端用过Bshare、百度分享等分享插件。有以下感触:

  1. 庞大,加载的图片很大,代码很多;
  2. 给网站新增3个以上的请求;
  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 更简洁 更友好

您还可以继续浏览 DshareJavaScript前端工具前端技巧 的文章

相关文章

15访客评论 ,博主回复2

  1. 很不错呀,收藏了!

    lft61804-11 15:05 回复
  2. 为什么我用到网站上就没有效果呢?

    郁闷中11-14 12:49 回复
    • 在需要显示的地方调用这个函数了吗? 还有就是图片是否上传?

      浩子11-15 09:00 回复
      • 我再试试看,谢谢!

        郁闷中11-15 09:02 回复
  3. 请问高手,bShare的一键通是怎么做的,能帮示范一下吗?网上找不着啊!

    • 没怎么研究过Bshare,不能解答你的问题,so sorry

      浩子11-10 16:17 回复
  4. 好文!我正要找的

  5. 这个要收下啊。 :lol:

    星尔09-06 13:35 回复
  6. 严重期待中…

    John09-02 22:36 回复
  7. 好久没来了,变化真大,没想留句话的 :evil: ,看到那句“严重鄙视飘过不留毛的鸟” :?:

    格子08-30 11:35 回复
  8. 其实更期待D4呀。呵呵

    Kimcool08-19 10:58 回复
  9. 大前端的jquery效果贼棒!特喜欢

    滑触线08-13 14:37 回复
  10. 研究这个费脑子啊

    保健08-09 11:28 回复
  11. 现在这代码基本了解一点 点

    华源08-09 11:27 回复
  12. http://www.cnblogs.com/trance/archive/2011/07/27/2118407.html
    我也写了一个简单的,增加了自定义标题和链接功能。楼主可以看看

    Trance08-05 11:26 回复
  13. 灰常感谢 收下了 :mrgreen:

    iSayme08-04 18:17 回复
  14. 还不错啊 前几天也写了一个类似的东西

我来说说

*

*

取消