微信中一些常用的js方法匯總
更新時間:2015年03月12日 08:59:16 投稿:hebedich
本文給大家匯總了一下在我們?nèi)粘i_發(fā)微信項目的過程中,經(jīng)常需要用到的一些js方法,都是些使用頻率很高,而且非常簡單的方法,這里推薦給大家。
1.網(wǎng)頁圖片集左右滑動查看圖片,如下樣例:
js效果
復制代碼 代碼如下:
var pictures = [];
angular.forEach(pitctures,function(k,i){
pictures[i] = k.imgPath;
});
$scope.previewPics = function(currentUrl){
if (typeof window.WeixinJSBridge != 'undefined') {
//微信圖片集查看
WeixinJSBridge.invoke('imagePreview', {
'current':currentUrl, //當前地址
'urls':pictures //組
});
} else {
alert( "請在微信中查看", null, function () {});
}
}
頁面元素:
復制代碼 代碼如下:
<div class="infoPics">
<div class="picImg" ng-repeat="picture in info.infoContent.pitctures">
<img ng-src="{{picture.imgPath}}" ng-click="previewPics(picture.imgPath)">
</div>
</div>
2.微信窗口關(guān)閉事件,實例如下:
復制代碼 代碼如下:
WeixinJSBridge.invoke('closeWindow',{},function(res){
//alert(res.err_msg);
});
3.分享網(wǎng)頁鏈接至朋友、朋友圈、微博
復制代碼 代碼如下:
var lineLink = 'http://../..',
imgUrl = 'http://../..',
shareTitle = '頁面標題',
descContent='內(nèi)容簡介',
appid = '';
//判斷是否支持微信js
if(typeof WeixinJsBridge == 'undefined'){
if(document.addEventListener){
document.addEventListener('WeixinJsBridgeReady',onBridgeReady,false);
}else if(document.attachEvent){
document.attachEvent('WeixinJsBridgeReady',onBridgeReady);
document.attachEvent('onWeixinJsBridgeReady',onBridgeReady);
}
}else{
onBridgeReady();
}
function onBridgeReady (){
WeixinJsBridgeReady.on('menu:share:appmessage',wx_shareFriend);//分享朋友
WeixinJsBridgeReady.on('menu:share:timeline',wx_shareTimeline);//分享到朋友圈
WeixinJsBridgeReady.on('menu:share:weibo',wx_shareWeibo);//分享朋友
}
function wx_shareFriend (){
WeixinJsBridge.invoke('sendAppMessage',{
"appid":appid,
"img_url":imgurl,
"img_width":'640',
"img_height":'500',
"link":lineLink,
"desc":descContent,
"title":shareTitle
},function(res){
console.log(res.err_msg);
}
});
}
function wx_shareTimeline (){
WeixinJsBridge.invoke('sendTimeline',{
"appid":appid,
"img_url":imgurl,
"img_width":'640',
"img_height":'500',
"link":lineLink,
"desc":descContent,
"title":shareTitle
},function(res){
console.log(res.err_msg);
}
})
}
function wx_shareWeibo (){
WeixinJsBridge.invoke('sendWeibo',{
"appid":appid,
"img_url":imgurl,
"img_width":'640',
"img_height":'500',
"link":lineLink,
"desc":descContent,
"title":shareTitle
},function(res){
console.log(res.err_msg);
}
})
}
4.隱藏網(wǎng)頁右上角按鈕
復制代碼 代碼如下:
WeixinJsBridge.call('hideOptionMenu');
5.隱藏網(wǎng)頁底部導航欄
復制代碼 代碼如下:
WeixinJsBridge.call('hideToolbar');
6.獲取當前網(wǎng)絡連接類型:
復制代碼 代碼如下:
WeixinJsBridge.invoke('getNetworkType',{},function(e){
console.log(e.err_msg);
})
7.禁止用戶分享
復制代碼 代碼如下:
WeixinJsBridge.invoke('disabledShare',{},function(e){
})
8.判斷是否在微信內(nèi)置瀏覽器中打開
復制代碼 代碼如下:
// true or false
var flag = WeixinApi.openInWeixin();
以上8條就是本文給大家分享的內(nèi)容了,希望對大家的微信開發(fā)能有所幫助。
相關(guān)文章
JavaScript使用canvas實現(xiàn)flappy bird全流程詳解
這篇文章主要介紹了JavaScript使用canvas實現(xiàn)flappy bird流程,canvas是HTML5提供的一種新標簽,它可以支持JavaScript在上面繪畫,控制每一個像素,它經(jīng)常被用來制作小游戲,接下來我將用它來模仿制作一款叫flappy bird的小游戲2023-03-03
JavaScript實現(xiàn)正則去除a標簽并保留內(nèi)容的方法【測試可用】
這篇文章主要介紹了JavaScript實現(xiàn)正則去除a標簽并保留內(nèi)容的方法,結(jié)合實例形式詳細分析了javascript針對a標簽及span標簽的正則匹配相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
JavaScript處理中文字符串的Base64編碼與解碼的兩種方法
這篇文章主要介紹了在 JavaScript 中處理中文字符串的 Base64 編碼與解碼,解釋了 Base64 編碼與中文字符沖突的原因,分別闡述了手動實現(xiàn)和使用TextEncoder和TextDecoder API 兩種方法,包括編碼和解碼的具體實現(xiàn)及示例,最后總結(jié)了兩種方法的適用場景2025-01-01

