share js 分享代码

(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;
});

})();

时间: 2024-08-06 11:05:29

share js 分享代码的相关文章

IOS android平台上都可使用的js分享代码

<span style="font-size:18px;"> </span> <span style="font-size:18px;">document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() { window.shareData = { "imgUrl": "{$shareimg}", "

网页分享插件 share.js 国外常用

这两天做推广,要求实现页面分享到国外各大社交媒体的功能.自己去翻各大厂的文档的话,实现起来时间相当长. github 上找了个插件,很6. 地址: https://github.com/ellisonleao/sharer.js 支持主流的国外的社交媒体的分享. 主要支持: Twitter Facebook Linkedin Google Plus Email Whatsapp Telegram Viber Pinterest Tumblr Hackernews Reddit VK.com Bu

share.js轻松分享/邀请

GitHub地址 https://github.com/overtrue/share.js 安装 直接git clone Github npm安装 npm install social-share.js bower安装 bower install social-share.js 使用 CDN,引入 share.min.css 与 social-share.min.js 两个链接. 使用 HTML <div class="inner-meta clearfix social-share&qu

外贸建站之网站导航栏菜单高亮JS实现代码分享

外贸建站之网站导航栏菜单高亮JS实现代码分享,今天客户要给CAN Clip加一个菜单高亮的效果,原本打算用PHP代码实现,但是感觉过于复杂,JS有更好的解决方案, 这里把我们开发的代码分享给大家. 1 var urlstr = location.href; 2 var urlstatus=false; 3 var urlnum = 1; 4 $("#navbar a").each(function () { 5 if ((urlstr + '/').indexOf($(this).at

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

因为工作的需求之前也封装过一个JS分享插件,集成了我们公司常用的几个分享平台. 但是总感觉之前的结构上很不理想,样式,行为揉成一起,心里想的做的完美,实际上总是很多的偏差,所以这次我对其进行了改版. 这次的核心就是:JS只负责事件+结构,也就是把功能实现出来,具体的外观样式,则使用者自己进行定义. 以下是新版分享插件的代码: 1 (function(root){ 2 'use strict'; 3 function share(params){ 4 5 this.params = params;

js分享功能实现

分享功能初步测试,title为当前页面的title. 其他详见注释!!! <!doctype html> <html> <head> <meta http-equiv="Content-Type" Content="text/html; charset=utf-8;"> <title>分享代码测试</title> <meta name="author" content=

自己动手写js分享插件 [支持https] (可以分享QQ空间,微信,新浪微博。。。)

由于百度分享,jiathis 等分享插件在https下均会报错,就萌生了自己动手写一个分享插件的念头,其实实现起来一点都不难,以下代码都已在https网站运行通过,特附上以下代码:还请各位看官不吝赐教: 附上演示效果网址:https://www.aishandian.com/jiekuan_zhishi-979.html 动画实现效果代码 $(document).on("click", ".msb_main", function() { if($(this).has

移动端使用百度分享代码

最近看了一些关于分享自媒体的代码,最后在在项目中使用了百度的分享代码,百度分享的代码格式,如下 <div class="bdsharebuttonbox" data-tag="share_1"> <!-- 此处添加展示按钮 --> </div> <script> window._bd_share_config = { //此处添加分享具体设置 } //以下为js加载部分 with(document)0[(getElem

分享代码

将优质内容分享到社交平台,让更多用户了解,目前已经成为网站,App,微信公众号等必备的功能,本文搜集了常见的分享js代码,供查阅参考. 1.分享到微信,易信 分享到微信,易信比较简单,贴上分享链接的二维码就可以了. 二维码生成地址:http://cli.im/ (网上随便搜的) 2.分享到新浪微博 代码如下: var _shareUrl = 'http://v.t.sina.com.cn/share/share.php?&appkey=895033136'; //真实的appkey ,必选参数