(function ($) { $.fn.share = function ($options) { var $head = $(document.head); var $defaults = { url: getsharedurl(), site_url: location.origin, source: $head.find('[name=site], [name=site]').attr('content') || document.title, title: $head.find('[name=title], [name=title]').attr('content') || document.title, description: $head.find('[name=description], [name=description]').attr('content') || '', image: $('img:first').prop('src') || '', imageselector: undefined, weibokey: '', wechatqrcodedialogid: 'share_weixin_qrcode_dialog_qr', //微信二维码弹窗的id wechatqrcodetitle: '微信扫一扫:分享', wechatqrcodehelper: '

微信里点“发现”,扫一下

二维码便可将本文分享至朋友圈。

', wechatqrcodesize: 200, mobilesites: [], sites: ['weibo', 'qq', 'wechat', 'tencent', 'douban', 'qzone'], disabled: [], initialized: false }; var $globals = $.extend({}, $defaults, $options); var $templates = { qzone: 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url={{url}}&title={{title}}&desc={{description}}&summary={{summary}}&site={{source}}&pics={{image}}', qq: 'http://connect.qq.com/widget/shareqq/index.html?url={{url}}&title={{title}}&source={{source}}&desc={{description}}&pics={{image}}', tencent: 'http://share.v.t.qq.com/index.php?c=share&a=index&title={{title}}&url={{url}}&pic={{image}}', weibo: 'https://service.weibo.com/share/share.php?url={{url}}&title={{title}}&pic={{image}}&appkey={{weibokey}}', //weibo: 'https://service.weibo.com/share/share.php?url={{url}}&title={{title}}&appkey={{weibokey}}&searchpic=false', //如需要去掉微博分享图片的请把微博链接替换成这个 wechat: 'javascript:;', douban: 'http://shuo.douban.com/!service/share?href={{url}}&name={{title}}&text={{description}}&image={{image}}&starid=0&aid=0&style=11' }; var $arialabels = { qzone: "qq空间", qq: "qq", tencent: "腾讯微博", weibo: "微博", wechat: "微信", douban: "豆瓣" }; this.each(function () { if ($(this).data('initialized')) { return true; } var $data = $.extend({}, $globals, $(this).data()); if ($data.imageselector) { $data.image = $($data.imageselector).map(function () { return $(this).prop('src'); }).get().join('||'); } var $container = $(this).addclass('share-component social-share'); createicons($container, $data); createwechat($container, $data); $(this).data('initialized', true); }); function createicons($container, $data) { var $sites = getsites($data); $data.mode == 'prepend' ? $sites.reverse() : $sites; if (!$sites.length) { return; } $.each($sites, function (i, $name) { var $url = makeurl($name, $data); var $link = $data.initialized ? $container.find('.icon-' + $name) : $(''); if (!$link.length) { return true; } $link.prop('aria-label', "分享到 " + $arialabels[$name]); $link.prop('href', $url); if ($name === 'wechat') { $link.prop('tabindex', -1); } else { $link.prop('target', '_blank'); } if (!$data.initialized) { $data.mode == 'prepend' ? $container.prepend($link) : $container.append($link); } }); } function createwechat($container, $data) { var $wechat = $container.find('a.icon-wechat'); if (!$wechat.length) { return; } $("body").append('
x

' + $data.wechatqrcodetitle + '

' + $data.wechatqrcodehelper + '
'); //加入微信分享点击事件 $('a.icon-wechat').click(function () { $("#" + $data.wechatqrcodedialogid).show(); }); $("#" + $data.wechatqrcodedialogid + " .close").click(function () { $("#" + $data.wechatqrcodedialogid).hide(); }); // if ($wechat.offset().top < 100) { // $wechat.find('.wechat-qrcode').addclass('bottom'); // } } function getsites($data) { if ($data['mobilesites'].length === 0 && $data['sites'].length) { $data['mobilesites'] = $data['sites']; }; var $sites = (ismobilescreen() ? $data['mobilesites'] : ($data['sites'].length ? $data['sites'] : [])).slice(0); var $disabled = $data['disabled']; if (typeof $sites == 'string') { $sites = $sites.split(/\s*,\s*/); } if (typeof $disabled == 'string') { $disabled = $disabled.split(/\s*,\s*/); } if (runninginwechat()) { $disabled.push('wechat'); } // remove elements $disabled.length && $.each($disabled, function (i, el) { var removeitemindex = $.inarray(el, $sites); if (removeitemindex !== -1) { $sites.splice(removeitemindex, 1); } }); return $sites; } function makeurl($name, $data) { var $template = $templates[$name]; $data['summary'] = $data['description']; for (var $key in $data) { if ($data.hasownproperty($key)) { var $camelcasekey = $name + $key.replace(/^[a-z]/, function ($str) { return $str.touppercase(); }); var $value = encodeuricomponent($data[$camelcasekey] === undefined ? $data[$key] : $data[$camelcasekey]); $template = $template.replace(new regexp('{{' + $key.touppercase() + '}}', 'g'), $value); } } return $template; } function runninginwechat() { return /micromessenger/i.test(navigator.useragent); } function ismobilescreen() { return $(window).width() <= 768; } function getsharedurl() { var hrefurl = location.href; var query = location.search; if (query) { if (query.indexof('sessionverify=') > -1) { return location.origin + location.pathname; } } return hrefurl; } }; // domready after initialization $(function () { $('.share-component,.social-share').share(); }); })(jquery);