微信中一些常用的js方法汇总
1.网页图片集左右滑动查看图片,如下样例:
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.微信窗口关闭事件,实例如下:
WeixinJSBridge.invoke('closeWindow',{},function(res){
//alert(res.err_msg);
});
3.分享网页链接至朋友、朋友圈、微博
var lineLink = 'http://../..',
imgUrl = 'http://../..',
shareTitle = '页面标题',
descContent='内容简介',
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.隐藏网页右上角按钮
WeixinJsBridge.call('hideOptionMenu');
5.隐藏网页底部导航栏
WeixinJsBridge.call('hideToolbar');
6.获取当前网络连接类型:
WeixinJsBridge.invoke('getNetworkType',{},function(e){
console.log(e.err_msg);
})
7.禁止用户分享
WeixinJsBridge.invoke('disabledShare',{},function(e){
})
8.判断是否在微信内置浏览器中打开
// true or false
var flag = WeixinApi.openInWeixin();
以上8条就是本文给大家分享的内容了,希望对大家的微信开发能有所帮助。