<div class="bdsharebuttonbox">
<a class="bds_more" data-cmd="more"></a>
<a class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
<a class="bds_tqq" data-cmd="tqq" title="分享到騰訊微博"></a>
<a class="bds_qzone" data-cmd="qzone" title="分享到QQ空間"></a>
<a class="bds_weixin" data-cmd="weixin" title="分享到微信"></a>
<a class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
</div>
<script>
window._bd_share_config = {
"common": {
"bdSnsKey": {}, // 官方申請的 AppKey,留空使用默認
"bdText": "", // 分享內容,默認取 document.title
"bdMini": "1", // 1=迷你模式(僅圖標), 0=標準模式(圖標+文字)
"bdMiniList": false, // 迷你模式下是否顯示更多面板
"bdPic": "", // 分享圖片 URL,默認頁面首張圖
"bdStyle": "1", // 風格:0=無圖標文字, 1=圖標+文字, 2=純圖標
"bdSize": "16" // 圖標尺寸:16/24/32
},
"share": {}, // 分享到組件配置
"image": { // 圖片分享配置
"viewList": ["tsina","tqq","qzone","weixin","sqq"],
"viewText": "分享到:",
"viewSize": "16"
},
"selectShare": { // 文字選中分享
"bdContainerClass": null,
"bdSelectMiniList": ["tsina","tqq","qzone","weixin","sqq"]
}
};
// 異步加載官方 JS(帶版本緩存參數)
with(document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'https://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date()/36e5)];
</script>
| 參數 | 類型 | 默認值 | 說明 |
|---|---|---|---|
| bdText | String | document.title | 分享文案,支援模板變量 |
| bdPic | String | 首張圖片 | 分享縮略圖,建議 200x200 以上 |
| bdUrl | String | location.href | 分享鏈接地址 |
| bdDesc | String | meta description | 分享描述(微信/QQ 使用) |
| bdStyle | Number | 1 | 0=文字, 1=圖標+文字, 2=純圖標 |
| bdSize | Number | 16 | 16/24/32 像素 |
| bdMini | String | "1" | "1"=迷你, "0"=標準 |
.bdsharebuttonbox { /* 容器 */ }
.bdsharebuttonbox a { /* 通用圖標 */ }
.bdsharebuttonbox .bds_tsina { background-position: 0 0; } /* 修改圖標位置 */
.bdsharebuttonbox .bds_tsina:hover { background-position: 0 -20px; }
<!-- 完全自定義按鈕 -->
<button id="myShareBtn" class="my-btn">分享到微博</button>
<script>
document.getElementById('myShareBtn').onclick = function() {
window._bd_share_main.F.use('share/api/share', function(shareApi) {
shareApi.share({
cmd: 'tsina',
title: '自定義標題',
url: location.href,
pic: 'https://example.com/thumb.jpg'
});
});
};
</script>
wget https://bdimg.share.baidu.com/static/api/js/share.jsbdimg.share.baidu.com 替換為自身 CDN 域名// 移動端優先,支援系統原生分享面板
if (navigator.share) {
btn.addEventListener('click', () => navigator.share({
title: document.title,
text: '看看這個頁面',
url: location.href
}).catch(err => console.log('分享取消或失敗:', err)));
} else {
// 降級到百度分享或自定義浮層
initBaiduShare();
}
百度分享配置核心在 window._bd_share_config 對象,掌握 common/share/image/selectShare 四大模塊參數即可滿足 90% 定製需求。考慮到第三方服務穩定性、隱私合規、加載性能,建議: