微信中一些經常使用的js事件積累

1.網頁圖片集左右滑動查看圖片,以下樣例:瀏覽器

jjs效果微信

var pictures = [];網絡

 angular.forEach(pitctures,function(k,i){
         pictures[i] = k.imgPath;
 });app

 $scope.previewPics = function(currentUrl){
        if (typeof window.WeixinJSBridge != 'undefined') {
            //微信圖片集查看
            WeixinJSBridge.invoke('imagePreview', {
                'current':currentUrl, //當前地址
                'urls':pictures //組
            });
        } else {
            alert( "請在微信中查看", null, function () {});
        }
    }ide

頁面元素:url

  <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>spa

 

2.微信窗口關閉事件,實例以下:事件

 WeixinJSBridge.invoke('closeWindow',{},function(res){
       //alert(res.err_msg);
 });圖片

 

3.分享網頁連接至朋友、朋友圈、微博get

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

相關文章
相關標籤/搜索