(function(){
var $doc = $(document);

var shareHandlers = {
'twitter': function(prop,shareUrl){
var D=550,A=450,C=screen.height,B=screen.width,H=Math.round((B/2)-(D/2)),G=0;
if(C>A){G=Math.round((C/2)-(A/2))}
window.open(shareUrl,'','left='+H+',top='+G+',width='+D+',height='+A+',personalbar=0,toolbar=0,scrollbars=1,resizable=1');
},
'facebook': function(prop,shareUrl){
var D=550,A=450,C=screen.height,B=screen.width,H=Math.round((B/2)-(D/2)),G=0;
if(C>A){G=Math.round((C/2)-(A/2))}
window.open(shareUrl,'','left='+H+',top='+G+',width='+D+',height='+A+',personalbar=0,toolbar=0,scrollbars=1,resizable=1');
},
'googleplus': function(prop,shareUrl){
var D=600,A=460,C=screen.height,B=screen.width,H=Math.round((B/2)-(D/2)),G=0;
if(C>A){G=Math.round((C/2)-(A/2))}
window.open(shareUrl, 'Share to Google+','left='+H+',top='+G+',width='+D+',height='+A+',menubar=no,location=no,status=no');
},
'weibo': null,
'tencentweibo': null,
'qzone': null
}

$doc.on('click', '.sns-share-list .share-btn[data-sns]', shareClickHandler);
$doc.on('click', '.djieventsbtn[data-sns]', shareClickHandler);
$doc.on('click', '.newssharebtn[data-sns]', shareClickHandler);

function shareClickHandler(e){
var $target = $(e.currentTarget),
type = $target.data('sns'),
props = {
id: $target.data('id'),
img: $target.data('img'),
title: $target.data('title'),
description: $target.data('description'),
url: $target.data('url'),
docTitle: document.title
};

//线上环境为裸协议,某些SNS平台解析错误,所以针对这种情况统一使用http协议
if(/^\/\//.test(props.img)){
props.img = 'http:' + props.img;
}

//添加平台追踪链接
props.url = addPlatform(props.url,type);

var shareUrl = getShareUrl(type, props);

if(shareHandlers[type]){
shareHandlers[type](props,shareUrl);
}else{
window.open(shareUrl, '', '');
}
}
function getShareUrl(type, origin_props){
var url = '';

var props = {
id: origin_props.id,
img: encodeURIComponent( origin_props.img ),
title: encodeURIComponent( origin_props.title ),
description: encodeURIComponent( origin_props.description ),
url: encodeURIComponent( origin_props.url ),
docTitle: document.title
};

switch(type){
case 'twitter':
url = '//twitter.com/intent/tweet?url='+ props.url +'&text='+ props.description;
break;
case 'facebook':
url = '//www.facebook.com/sharer/sharer.php?p[url]='+ props.url;
break;
case 'googleplus':
url = '//plus.google.com/share?url='+ props.url;
break;
case 'weibo':
url = 'http://service.weibo.com/share/share.php?url='+ props.url +'&title='+ props.title +' '+ props.description +'&pic='+ props.img +'&searchPic=false';
break;
case 'tencentweibo':
url = 'http://share.v.t.qq.com/index.php?c=share&a=index&url='+ props.url +'&title='+ props.description +'&pic='+ props.img;
break;
case 'qzone':
url = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+ props.url +'&title='+ props.docTitle +'&summary='+ props.description +'&pics='+ props.img;
break;
}

return url;
}

function addPlatform(url,platform){
var qsReg = /(\?[^#.]*)/,
hashReg = /(#.*)/,
hasHash = hashReg.test(url),
hash = '',
platform = platform ? platform : 'copy';

if(hasHash){
url = url.replace(hashReg, function(macthedHash){
hash = macthedHash;
return macthedHash = '';
});
}
if(qsReg.test(url)){
url = url.replace(qsReg, '$1&p='+ platform);
} else {
url += '?p='+ platform;
}

return url + hash;
}
$("#apply h3").click(function(){

$(this).next("#apply .boxs").slideToggle("slow")
.siblings("#apply .boxs:visible").slideToggle("slow");
$(this).toggleClass("current");
$(this).siblings("#apply h3").removeClass("current");
return false;
});

})();

share js 分享代码的更多相关文章

  1. js 分享代码--完整示例代码

    <div class="bdsharebuttonbox" data-tag="share_1"> <a class="bds_ms ...

  2. js分享代码

    <<!DOCTYPE html><html><head> <title></title></head> <body& ...

  3. JS实现页面分享代码share,不需要第三方接口

    在开发一个页面的时候常常会有这么一个小功能,就是分享该页面中的信息. 常见的分享代码有百度分享,JiaThis分享插件(现在貌似不能用了),bshare分享插件等,我主要分享一下自定义分享代码,如下: ...

  4. js快速分享代码

    这是一款简单易用的文章分享工具,您只需将下面的html代码拷贝到模板中就可以实现文章快速分享功能.如果您想分享你的博客.个人网站或者企业网站等等,下面是两款不错的分享工具,值得拥有! 1. <d ...

  5. 页面分享代码share

    在开发一个页面的时候常常会有这么一个小功能,就是分享该页面中的信息. 常见的分享代码有百度分享,JiaThis分享插件,bshare分享插件等,我主要分享一下自定义分享代码,如下: function ...

  6. 百度分享share.js插件

    //百度分享window._bd_share_config = { common : { bdText : '分享标题', bdDesc : '分享描述', bdUrl : '分享链接', bdPic ...

  7. share.js轻松分享/邀请

    GitHub地址 https://github.com/overtrue/share.js 安装 安装的方法很多,大家选择自己合适的进行安装就好. clone $ git clone https:// ...

  8. share.js一键分享到微博、QQ空间、QQ好友、微信、腾讯微博、豆瓣、Facebook、Twitter、Linkedin、Google+、点点等

    官网上面 https://github.com/overtrue/share.js非常详细的介绍了share.js他的使用 使用方式有:第一:使用 npm npm install social-sha ...

  9. 自己封装的一个JS分享组件

    因为工作的需求之前也封装过一个JS分享插件,集成了我们公司常用的几个分享平台. 但是总感觉之前的结构上很不理想,样式,行为揉成一起,心里想的做的完美,实际上总是很多的偏差,所以这次我对其进行了改版. ...

随机推荐

  1. mysql的mvcc(多版本并发控制)

    mysql的mvcc(多版本并发控制) 我们知道,mysql的innodb采用的是行锁,而且采用了多版本并发控制来提高读操作的性能. 什么是多版本并发控制呢 ?其实就是在每一行记录的后面增加两个隐藏列 ...

  2. cf C. Bits

    http://codeforces.com/contest/485/problem/C 利用位运算来解决这个题,从L开始,从每一位按位或,知道到达r位置,ans=ans|(1<<k);就可 ...

  3. IC芯片設計

    IC從生產目的上可以分成為通用IC(如CPU,DRAM,接口芯片等)和ASIC(ApplicationSpecificIntegreted Circuit)兩種,ASIC是因應專門用途而生產的IC.  ...

  4. Android 正则表达式验证手机和邮箱格式是否正确

    demo例子: package it.com.cc; import java.util.regex.Matcher; import java.util.regex.Pattern; import an ...

  5. BZOJ 1010 [HNOI2008]玩具装箱toy

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 7184  Solved: 2724[Submit][St ...

  6. MFC自定义消息

    本文地址:http://blog.163.com/strive_only/blog/static/893801682010101911467765/ 消息机制是windows的典型运行机制,在MFC中 ...

  7. HBase Java API使用(一)

    前言 1. 创建表:(由master完成) 首先需要获取master地址(master启动时会将地址告诉zookeeper)因而客户端首先会访问zookeeper获取master的地址 client和 ...

  8. selenium webdriver python 等待

    AJAX,即“Asynchronous Javascript And  XML”.可以实现网页的异步更新.也就是在不重新加载整个网页的情况下,对网页的某部分进行更新. 现在大多数网站都使用AJAX技术 ...

  9. js打开新的链接下载文件

    var p =params.join("&"); var a = document.createElement('a'); a.href = 'report/exportp ...

  10. jQuery.holdReady()方法用法实例

    调用此方法可以延迟jQuery的ready事件,也就是说尽管文档已经加载完成,也不会执行ready事件处理方法.可以多次调用jQuery.holdReady()方法,以延迟jQuery的ready事件 ...