Summary of js methods commonly used in WeChat development

高洛峰
Release: 2017-03-15 17:26:10
Original
1712 people have browsed it

This article summarizes for you some js methods that are often used in our daily development of WeChat projects. They are all used very frequently and are very Simple method, recommended to everyone here.

1. WebpagePicturesSwipe left and right to view the pictures, as shown below:

js effect

The code is as follows:

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 () {});
        }
    }
Copy after login

Page elements:

The code is as follows:

  <p class="infoPics">
           <p class="picImg" ng-repeat="picture in info.infoContent.pitctures">
                <img ng-src="{{picture.imgPath}}" ng-click="previewPics(picture.imgPath)">
           </p>
     </p>
Copy after login

2. WeChat window closing event , The example is as follows:

The code is as follows:

 WeixinJSBridge.invoke(&#39;closeWindow&#39;,{},function(res){
       //alert(res.err_msg);
 });
Copy after login

3. Share the web link to friends, Moments, and Weibo

The code is as follows:

var lineLink = &#39;http://../..&#39;,
  imgUrl = &#39;http://../..&#39;,
  shareTitle = &#39;页面标题&#39;,
  descContent=&#39;内容简介&#39;,
  appid = &#39;&#39;;
//判断是否支持微信js
 if(typeof WeixinJsBridge == &#39;undefined&#39;){
  if(document.addEventListener){
    document.addEventListener(&#39;WeixinJsBridgeReady&#39;,onBridgeReady,false);
  }else if(document.attachEvent){
    document.attachEvent(&#39;WeixinJsBridgeReady&#39;,onBridgeReady);
    document.attachEvent(&#39;onWeixinJsBridgeReady&#39;,onBridgeReady);
  }
 }else{
  onBridgeReady();
 }
function onBridgeReady (){
  WeixinJsBridgeReady.on(&#39;menu:share:appmessage&#39;,wx_shareFriend);//分享朋友
  WeixinJsBridgeReady.on(&#39;menu:share:timeline&#39;,wx_shareTimeline);//分享到朋友圈
  WeixinJsBridgeReady.on(&#39;menu:share:weibo&#39;,wx_shareWeibo);//分享朋友
}
function wx_shareFriend (){
  WeixinJsBridge.invoke(&#39;sendAppMessage&#39;,{
    "appid":appid,
    "img_url":imgurl,
    "img_width":&#39;640&#39;,
    "img_height":&#39;500&#39;,
    "link":lineLink,
    "desc":descContent,
    "title":shareTitle
    },function(res){
      console.log(res.err_msg);
    }
  });
}
function wx_shareTimeline (){
  WeixinJsBridge.invoke(&#39;sendTimeline&#39;,{
    "appid":appid,
    "img_url":imgurl,
    "img_width":&#39;640&#39;,
    "img_height":&#39;500&#39;,
    "link":lineLink,
    "desc":descContent,
    "title":shareTitle
    },function(res){
      console.log(res.err_msg);
    }
  })
}  
function wx_shareWeibo (){
  WeixinJsBridge.invoke(&#39;sendWeibo&#39;,{
    "appid":appid,
    "img_url":imgurl,
    "img_width":&#39;640&#39;,
    "img_height":&#39;500&#39;,
    "link":lineLink,
    "desc":descContent,
    "title":shareTitle
    },function(res){
      console.log(res.err_msg);
    }
  })
}
Copy after login

4. Hide the button in the upper right corner of the web page

The code is as follows:

 WeixinJsBridge.call(&#39;hideOptionMenu&#39;); 
Copy after login

5. Hide the bottom of the web page NavigationBar

The code is as follows:

 WeixinJsBridge.call(&#39;hideToolbar&#39;);
Copy after login

6. Get the current network connection type:

The code is as follows:

 WeixinJsBridge.invoke(&#39;getNetworkType&#39;,{},function(e){
    console.log(e.err_msg);
 })
Copy after login

7. Prohibit users from sharing

The code is as follows:

  WeixinJsBridge.invoke(&#39;disabledShare&#39;,{},function(e){
  })
Copy after login

8. Determine whether it is built-in in WeChat Open

in the browser and the code is as follows:

  // true or false
  var flag = WeixinApi.openInWeixin();
Copy after login

The above 8 items are the content shared with you in this article. I hope it can be helpful to everyone’s WeChat development.

The above is the detailed content of Summary of js methods commonly used in WeChat development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!