码迷,mamicode.com
首页 > Web开发 > 详细

share js 分享代码

时间:2016-04-18 20:26:42      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

(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 分享代码

标签:

原文地址:http://www.cnblogs.com/zhuliming-java/p/5405509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!