Skip to content

Commit

Permalink
更新用法。
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Oct 19, 2015
1 parent e1a86da commit bc2b74f
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 33 deletions.
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,15 @@
HTML:

```html
<div class="sns-share">
<a href="" class="qzone"><i class="icon iconfont icon-qzone"></i></a>
<a href="" class="qq"><i class="icon iconfont icon-qq"></i></a>
<a href="" class="weibo"><i class="icon iconfont icon-weibo"></i></a>
<a href="" class="wechat"><i class="icon iconfont icon-wechat"></i></a>
<a href="" class="douban"><i class="icon iconfont icon-douban"></i></a>
</div>
<div class="share-bar"></div>

<!-- 引入sns.js -->
<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<!-- <script src="jquery.qrcode.min.js"></script> -->
<script src="jquery.qrcode.min.js"></script> <!-- for QRcode -->
<script src="share.js"></script>
<script>
$(function(){
$('.sns-share').share();
$('.share-bar').share();
});
</script>
```
Expand All @@ -49,17 +43,28 @@ HTML:

```js
var $config = {
url : '', // 网址,默认使用 window.location.href
site : '', // 来源(QQ空间会用到), 默认读取head标签:<meta name="site" content="http://overtrue" />
title : '', // 标题,默认读取 document.title
description : '', // 描述, 默认读取head标签:<meta name="description" content="PHP弱类型的实现原理分析" />
iamge : '', // 图片, 默认取网页中第一个img标签
target : '_blank' //打开方式
//...
};

$('.sns-share').share($config);
```

可用的配置有:

```js

url : '', // 网址,默认使用 window.location.href
site : '', // 来源(QQ空间会用到), 默认读取head标签:<meta name="site" content="http://overtrue" />
title : '', // 标题,默认读取 document.title 或者 <meta name="title" content="share.js" />
description : '', // 描述, 默认读取head标签:<meta name="description" content="PHP弱类型的实现原理分析" />
iamge : '', // 图片, 默认取网页中第一个img标签
target : '_blank' //打开方式
qrcodeTitle: "微信扫一扫:分享", // 微信二维码提示文字
qrcodeWidth: 100, // 二维码宽度
sites: ['qzone', 'qq', 'weibo','wechat', 'douban'], // 启用的站点
```


# License

MIT
Expand Down
42 changes: 42 additions & 0 deletions example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

<!DOCTYPE html>
<!--[if lte IE 6 ]> <html class="ie ie6 lte-ie7 lte-ie8" lang="zh-CN"> <![endif]-->
<!--[if IE 7 ]> <html class="ie ie7 lte-ie7 lte-ie8" lang="zh-CN"> <![endif]-->
<!--[if IE 8 ]> <html class="ie ie8 lte-ie8" lang="zh-CN"> <![endif]-->
<!--[if IE 9 ]> <html class="ie ie9" lang="zh-CN"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="zh-CN"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Share.js - 一键分享到微博,QQ空间,腾讯微博,人人,豆瓣</title>
<meta name="description" content="Share.js 一键分享到微博,QQ空间,腾讯微博,人人,豆瓣 by overtrue." />
<link rel="stylesheet" href="http://overtrue.me/bootstrap-theme-slim/css/bootstrap.css" media="screen">
<link rel="stylesheet" href="http://overtrue.me/bootstrap-theme-slim/css/ionicons.css" media="screen">
<link rel="stylesheet" href="http://overtrue.me/bootstrap-theme-slim/css/app.css" media="screen">
<link rel="stylesheet" href="./share.css" media="screen">
</head>
<body>
<div class="container">
<div class="page-header">
<h1 id="container">Share.js 示例</h1>
</div>
<div class="jumbotron">
<h1>大寒</h1>
<p>大寒是二十四节气之一最后一个节气。每年1月20日前后太阳到达黄经300°时为大寒。《月令七十二候集解》:“十二月中,解见前(小寒)。”《授时通考·天时》引《三礼义宗》:“大寒为中者,上形于小寒,故谓之大……寒气之逆极,故谓大寒。”这时寒潮南下频繁,是中国大部分地区一年中的最冷时期,风大,低温,地面积雪不化,呈现出冰天雪地、天寒地冻的严寒景象。这个时期,铁路、邮电、石油、海上运输等部门要特别注意及早采取预防大风降温、大雪等灾害性天气的措施。农业上要加强牲畜和越冬作物的防寒防冻。</p>
<p><a class="btn btn-primary btn-lg">更多</a></p>
</div>
<div class="shares"></div>
</div>

<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<script src="http://overtrue.me/share.js/jquery.qrcode.min.js"></script>
<script src="./share.js"></script>

<script>
$(function(){
$('.shares').share();
});
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions share.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
.sns-share-component a {
position:relative;
text-decoration:none;
margin: 3px;
display:inline-block;
}

.sns-share-component .icon{
font-size: 22px;
background: #555;
border-radius: 50%;
line-height: 100px;
margin: 10px 0;
padding: 8px;
color:#fff;
-webkit-transition: background 0.2s ease-out 0s;
Expand All @@ -48,7 +49,6 @@

.sns-share-component a .wechat-qrcode {
position:absolute;
bottom: 40px;
visibility: hidden;
-webkit-transform: translate(0, 10px);
-ms-transform: translate(0, 10px);
Expand Down
66 changes: 50 additions & 16 deletions share.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,79 @@
(function($){
$.fn.share = function ($options) {
var $image = $(document).find('img:first').attr('src');
/**
* Share.js
*
* @author overtrue <[email protected]>
* @license MIT
*
* @example
* <pre>
* $('.share-bar').share();
*
* // or
*
* $('.share-bar').share({
* sites: ['qzone', 'qq', 'weibo','wechat'],
* // ...
* });
* </pre>
*/
;(function($){
/**
* Initialize a share bar.
*
* @param {String|Object} $item target container.
* @param {Object} $options settings (optional).
*
* @return {Void}
*/
$.fn.share = function ($item, $options) {
var $image = $(document).find('img:first').prop('src');

var $defaults = {
url: window.location.href,
site: document.title,
title: $(document.head).find('[name="site-title"]').text() || document.title,
description: $(document.head).find('[name="description"]').text(),
site: $(document.head).find('[name="site"]').attr('content') || $(document.head).find('[name="Site"]').attr('content') || document.title,
title: $(document.head).find('[name="title"]').attr('content') || $(document.head).find('[name="Title"]').attr('content') || document.title,
description: $(document.head).find('[name="description"]').attr('content') || $(document.head).find('[name="Description"]').attr('content'),
image: $image ? $image : '',
target : '_blank',
qrcodeTitle: "微信扫一扫:分享",
qrcodeWidth: 100,
sites: ['qzone', 'qq', 'weibo','wechat', 'douban'],
};

var $settings = $.extend(true, $defaults, $options);

$settings.url = encodeURI($settings.url);
$settings.site = encodeURI($settings.site);
$settings.title = encodeURI($settings.title);
$settings.description = encodeURI($settings.description);

var $urls = {
qzone: "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=" + $settings.url + "&title=" + $settings.title + "&desc=" + $settings.description + "&summary=" + $settings.description + "&site=" + $settings.site + ($settings.image? $settings.image : ''),
qq: "http://share.v.t.qq.com/index.php?c=share&a=index&url=" + $settings.url + "&title=" + $settings.title + $settings.description + ($settings.image ? "&pic=" + $settings.image : ''),
qq: "http://connect.qq.com/widget/shareqq/index.html?url="+ $settings.url + "&title=" + $settings.title + "&source=" + $settings.site + "&desc=" + $settings.description,
weibo: "http://service.weibo.com/share/share.php?url=" + $settings.url + "&title=" + $settings.title + $settings.description + ($settings.image ? "&pic=" + $settings.image : ''),
wechat: 'javascript:;',
douban: "http://shuo.douban.com/!service/share?href=" + $settings.url + "&name=" + $settings.title + "&text=" + $settings.description + ( $settings.image ? "&image=" + $settings.image : '') + '&starid=0&aid=0&style=11&stime=&sig='
};

this.each(function() {
var el = $(this).addClass('sns-share-component');
var $wechat = el.find('a.wechat');
var $el = $(this).addClass('sns-share-component');

for ($i in $settings.sites) {
var $name = $settings.sites[$i];
var $link = $('<a href="" class="'+$name+'"><i class="icon iconfont icon-'+$name+'"></i></a>')
.attr('href', $urls[$name])
.attr('target', $settings.target);

for($id in $urls){
el.find('a.' + $id).attr('href', $urls[$id]).attr('target', $settings.target);
$el.append($link);
}

if (!$wechat.length) {return;};
var $wechat = $el.find('a.wechat');

if (typeof $.fn.qrcode == 'function') {

$wechat.append('<span class="wechat-qrcode"></span>');
$wechat.find('.wechat-qrcode').qrcode({ width: $settings.qrcodeWidth, height: $settings.qrcodeWidth - 10, text: $settings.url});
$wechat.find('.wechat-qrcode').css({right: - $settings.qrcodeWidth / 2, display: 'block' });
$wechat.find('.wechat-qrcode').append($settings.qrcodeTitle);
$wechat.find('.wechat-qrcode').qrcode({ width: $settings.qrcodeWidth, height: $settings.qrcodeWidth - 10, text: $settings.url})
.css({right: - $settings.qrcodeWidth / 2, display: 'block', position:'absolute', bottom: $wechat.height()})
.append($settings.qrcodeTitle);
} else {
console.error('未加载 jquery.qrcode.min.js, 无法支持微信分享');
$wechat.hide();
Expand Down

0 comments on commit bc2b74f

Please sign in to comment.