蓝戒博客

  • 首页
  • 研发说
  • 架构论
  • 效能录
  • AI谈
  • 随笔集
智构苍穹
融合 AI、架构与工程实践,沉淀方法论,构建可持续的技术价值。
  1. 首页
  2. 研发说
  3. 正文

百度分享自定义设置不同媒体分享不同内容的方法

2015年10月13日 7680点热度 0人点赞 0条评论

在做一个H5的移动web项目的时候,需要分享功能,之前一直是使用通用自由选择版调用,并且是在pc的web端分享使用。这次的H5项目时是第一次在手机端使用,为了更好地用户体验,很多webapp都是使用第三方的jssdk分享,但是这些jssdk要求都是在相应的IOS和Android的外壳下运行的移动web,因此无法满足这次的项目需求,最终还是选择了百度分享,分享的样式部分参考了美拍的移动web端的页面效果,分享的js使用百度分享的专业开发版。

share_ico

页面效果截图如下:
bdshare

 

百度默认的分享样式不是我们的想要的效果,所以需要自己调整一下样式如下:

css样式:

/*--------------分享---------------*/
.module-share-1,.module-share-2{height:195px;width:100%;background:#2b2931;position:fixed;bottom:-195px;left:0;text-align:center;z-index:12}.module-share-2{height:248px;bottom:-248px}.module-share-1-show{-webkit-transition:-webkit-transform .3s ease-in,opacity .4s;transition:transform .3s ease-in,opacity .4s;-webkit-transform:translate3d(0,-195px,0);transform:translate3d(0,-195px,0);opacity:1}.module-share-hide{-webkit-transition:-webkit-transform .3s ease-in,opacity .4s;transition:transform .3s ease-in,opacity .4s;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:0}.module-share-2-show{-webkit-transition:-webkit-transform .3s ease-in,opacity .4s;transition:transform .3s ease-in,opacity .4s;-webkit-transform:translate3d(0,-248px,0);transform:translate3d(0,-248px,0);opacity:1}.module-share-h3{font-size:14px;color:#807f83;height:45px;line-height:45px}.module-share-wrap{width:100%;padding:0 5px}.module-share-content{height:77px;width:100%;overflow:hidden}.module-share-list{height:77px;position:relative;margin:0 auto}.module-share-list a{padding:0 8px 0 5px;width:80px;height:77px;position:relative;display:inline-block;text-align:center;color:#fff}.module-share-list p{position:absolute;left:3px;bottom:3px;width:70px}.module-wx-tip-i,.module-wx-tip-a{position:fixed;left:0;right:0;top:0;z-index:12;background-color:#2b2931;background-size:auto 100%;background-repeat:no-repeat;background-position:right top;color:#959498}.module-wx-tip-i{height:160px;background-image:url(../img/wx-tip-i.jpg?1)}.module-wx-tip-i-div,.module-wx-tip-a-div{position:absolute;bottom:20px;right:87px;font-size:14px}.module-wx-tip-a{height:120px;background-image:url(../img/wx-tip-a.jpg?1)}.module-wx-tip-a-div{font-size:13px;right:97px;bottom:30px}.module-share-btn-wrap{padding:15px 20px}.module-share-cancel,.module-share-repost{display:block;height:43px;line-height:43px;padding:0 20px;background:#625e6b;color:#fff;border-radius:7px;font-size:16px}.module-share-repost{background:#fa4d9f;margin:0 0 10px}
.m-icon-qq{background: url(../img/ico_qq@2x.png) center center no-repeat !important; background-size:contain !important;}
.m-icon-qzone{background: url(../img/ico_qzone@2x.png) center center no-repeat; background-size:contain !important;}
.m-icon-wb{background: url(../img/ico_wb@2x.png) center center no-repeat; background-size:contain !important;}
.m-icon-share{width: 50px; height: 50px;}
.micon{ display: inline-block;}
.share_btn{ width:100%; opacity:.4; text-shadow:none;}
.share_icos{ position:relative; display:inline-block;}
.bdshare-button-style0-32 a{width:50px !important; height:50px !important; float:none !important; font-size: 14px; padding:0 !important; line-height:50px !important; margin:0 5px !important;}
.bdshare-button-style0-32 .bds_sqq{background: url(../img/ico_qq@2x.png) center center no-repeat !important; background-size:contain !important;}
.bdshare-button-style0-32 .bds_qzone{background: url(../img/ico_qzone@2x.png) center center no-repeat !important; background-size:contain !important;}
.bdshare-button-style0-32 .bds_tsina{background: url(../img/ico_wb@2x.png) center center no-repeat !important; background-size:contain !important;}
.bdsharebuttonbox{ text-align:center;}
.bds_wx{background: url(../img/ico_wx@2x.png) center center no-repeat !important; background-size:contain !important;}

页面的弹出层使用的是bootstrap的模态窗如下:

html代码:

<!--share Modal -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog">
<div class="module-share-1 module-share-1-show" id="shareModule" data-page-class="page-blur" data-animate-class="module-share-1-show,module-share-hide">
<h3 class="module-share-h3">分享到...</h3>
<div class="module-share-wrap box">
<div class="module-share-content">
<div class="bdsharebuttonbox" id="moduleMediaShareList" data-tag="share_1"><a class="share_icos bds_qzone" data-cmd="qzone">
<i class="micon m-icon-share m-icon-qzone"></i></a>QQ空间
<a class="share_icos bds_tsina" data-cmd="tsina">
<i class="micon m-icon-share m-icon-wb"></i></a>微博
<a class="share_icos bds_sqq" data-cmd="sqq">
<i class="micon m-icon-share m-icon-qq"></i></a>QQ好友
<a class="share_icos bds_wx disno" onclick="_system._guide(true)">
<i class="micon m-icon-share m-icon-wx"></i></a>微信

</div>
</div>
</div>
<div class="module-share-btn-wrap box"><button class="close share_btn" type="button" data-dismiss="modal"><span class="module-share-cancel js-btn" id="shareModuleCancel">取消</span></button></div>
</div>
</div>

此处的微信分享只有在微信里打开才显示,这个方法 onclick="_system._guide(true)" 是引导微信分享。

小伙伴们不要着急,下面才是本文分享的重点,自定义设置不同媒体分享不同内容的方法:

下面是分享的项目中源码,由于是要获取分享标题是动态内容,采用的是ajax请求返回的数据,因此需要在返回成功后success:function(data)调用百度分享的方法,这里使用的是百度的百度分享的专业开发版。

百度分享专业版API文档:http://share.baidu.com/code/advance

点击分享时对分享内容动态修改的api方法如下:

onBeforeClick function function(cmd,config){} 在用户点击分享按钮时执行代码,更改配置。
cmd为分享目标id,config为当前设置,返回值为更新后的设置。

分享目标id

分享媒体id对应表

名称 ID
一键分享 mshare
QQ空间 qzone
新浪微博 tsina
人人网 renren
腾讯微博 tqq
百度相册 bdxc
开心网 kaixin001
腾讯朋友 tqf
百度贴吧 tieba
豆瓣网 douban
搜狐微博 tsohu
百度新首页 bdhome
QQ好友 sqq
和讯微博 thx
百度云收藏 bdysc
美丽说 meilishuo
蘑菇街 mogujie
点点网 diandian
花瓣 huaban
堆糖 duitang
和讯 hx
飞信 fx
有道云笔记 youdao
麦库记事 sdo
轻笔记 qingbiji
人民微博 people
新华微博 xinhua
邮件分享 mail
我的搜狐 isohu
摇篮空间 yaolan
若邻网 wealink
天涯社区 ty
Facebook fbook
Twitter twi
linkedin linkedin
复制网址 copy
打印 print
百度个人中心 ibaidu
微信 weixin
股吧 iguba

根据分享的目标id判断分享的媒体类型,分别设置分享的自定义内容

onBeforeClick: function(cmd,config){
if(cmd == "qzone"){
return{
bdText:bds_qzone,
bdDesc:bds_qzone
}
}else if(cmd == "tsina"){
return{
bdText:bds_tsina,
bdDesc:bds_tsina
}
}else if(cmd == "sqq"){
return{
bdText:bds_sqq,
bdDesc:bds_sqq
}
}

具体的项目中分享代码如下:

js分享代码:

<!-- share -->
<script>
$(function(){
var showvId = _nameStorage.getItem("vId");
var showauthorId = _nameStorage.getItem("authorId");
$.ajax({
type:"get",
url:path + "smallPhotography/channel/watchVideo.action",
data:{"videoId":showvId,"authorId":showauthorId},
contentType: "application/x-www-form-urlencoded; charset=utf8",
dataType: "json",
success:function(data){
var bds_qzone = '['+data.resultInfo.vTitle+']更多相关摄影技巧,尽在摄影•开课吧';
var bds_tsina = '我在开课吧看视频《['+data.resultInfo.vTitle+']》['+path+'html/show.html?showvId=' + showvId + "&showauthorId=" + (showauthorId == null ? "" : showauthorId)+']#专注前沿摄影技巧,就在<摄影•开课吧>#';
var bds_sqq = data.resultInfo.vTitle + '-摄影•开课吧';
var bdTextC = data.resultInfo.vTitle + '-摄影•开课吧';

window._bd_share_config = {
//此处添加分享具体设置
common : {
bdText : bdTextC,
bdDesc : bdTextC,
bdUrl : path+'html/show.html?showvId=' + showvId + "&showauthorId=" + (showauthorId == null ? "" : showauthorId),
bdPic : data.resultInfo.vCoverUrl,
onBeforeClick: function(cmd,config){
if(cmd == "qzone"){
return{
bdText:bds_qzone,
bdDesc:bds_qzone
}
}else if(cmd == "tsina"){
return{
bdText:bds_tsina,
bdDesc:bds_tsina
}
}else if(cmd == "sqq"){
return{
bdText:bds_sqq,
bdDesc:bds_sqq
}
}

}
},
share : [{
"tag" : "share_1",
"bdSize" : 32
}],
image : [{
viewType : 'list',
viewPos : 'top',
viewColor : 'black',
viewSize : '32',
}]
}
//以下为js加载部分
with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
}
});
});
</script>
<script type="text/javascript">
//判断微信
$(function(){
if(isWeiXin()){
$(".bds_wx").removeClass("disno");
}
});

function isWeiXin(){
var ua = window.navigator.userAgent.toLowerCase();
if(/MicroMessenger/i.test(ua)){
return true;
}else{
return false;
}
}
</script>

标签: web技术 解决方案
最后更新:2025年9月13日

cywcd

我始终相信,技术不仅是解决问题的工具,更是推动思维进化和创造价值的方式。从研发到架构,追求极致效能;在随笔中沉淀思考,于 AI 中对话未来。

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

cywcd

我始终相信,技术不仅是解决问题的工具,更是推动思维进化和创造价值的方式。从研发到架构,追求极致效能;在随笔中沉淀思考,于 AI 中对话未来。

最新 热点 随机
最新 热点 随机
狂揽 32 万 Star,让 AI 智能体帮你“逆向一切”:深度拆解 GitHub 现象级神器 morluto/rea 与 6 大最佳实践 OpenCode 曝 1.4% 用户密钥失窃,揭秘黑客“对抗性蒸馏”黑产! Claude 全面开放 OTel 追踪、Cohere 鲸吞 500 漏洞猎手:AI 智能体彻底告别“黑盒裸奔”! 不用显卡、普通 CPU 140 毫秒出结果!Mistral 突发开源 209M 时序模型 Timestral,横扫三大权威榜单 Meta Muse 被“开源复刻”?深度拆解 5000 星 OpenMuse:架构很香,但这 3 个暗坑必须看清 智谱 GLM-Legal 反超 Claude 与 Kimi:18B 激活参数凭什么啃下最严苛法律评测?
不讲废话的 AI 估值 2 亿美元!全面拆解“系统一”决策模型 Jev:70ms 极速响应与工业级实战白嫖 10 亿 Token 与云端虚拟机!Meta Muse 国内成功注册保姆级指南:手把手教你跳过排队请3天休13天!我用火山引擎 Seed-2.1-pro搓了个《国庆旅行模拟器》小游戏宿敌破天荒联手!OpenAI 与 Anthropic 互相“查水表”,Google 证实 Agent 越狱渗透:AI 正在失控边缘?波及 13.4 万智能体!首个零点击 RCE 漏洞 Plugin4Shell 爆发:你锁定的插件正在偷偷黑掉你的电脑阿里开源 OpenCodeReview:给大模型套上“工程缰绳”的代码审查神器
AI + Skills:从「会聊天」到「能干活」的关键一跃 告别高昂Token费!用这个开源神作,把免费Windows Copilot秒变OpenAI标准API Vibe Coding:当“写代码”变成“说需求” 别再手写爬虫了!AI 时代偷偷霸榜的“网页外挂” BrowserAct 到底有多绝? 为什么你狂学上百个“搞钱套路”依然一事无成?如何建立战略思维破局 OpenCode 曝 1.4% 用户密钥失窃,揭秘黑客“对抗性蒸馏”黑产!
最近评论
cywcd 发布于 4 周前(09月22日) 《牛来跑酷》游戏链接地址已更新: 1. Gemini 3.7 Flash 版 牛来跑酷: htt...
渔夫 发布于 12 个月前(11月05日) 学到了,感谢博主分享
沙拉小王子 发布于 9 年前(11月30日) 适合vue入门者学习,赞一个
沙拉小王子 发布于 9 年前(11月30日) 适合vue入门者学习,赞一个
cywcd 发布于 10 年前(04月27日) 请参考一下这篇文章http://www.jianshu.com/p/fa4460e75cd8

COPYRIGHT © 2025 蓝戒博客_智构苍穹-专注于大前端领域技术生态. ALL RIGHTS RESERVED.

京ICP备12026697号-2