微信小程序 设置宽度是100%,然后图片能成为正方形的方法。小程序按屏幕比例的正方形

1.在全局app.js中获取设备的宽度

  globalData: {
    userInfo: null,
    sysWidth:wx.getSystemInfoSync().windowWidth, //图片宽度
  }

2.在其他js中加载时设置高度变量 如a.js

    var appInstance = getApp();
    console.log(appInstance.globalData.sysWidth); 
    this.setData({
      sysWidth: appInstance.globalData.sysWidth
    });

3.在引用的wxml中设置 style样式

<swiper autoplay="auto" interval="5000" duration="500" current="{{swiperCurrent}}" bindchange="swiperChange" style='height:{{sysWidth}}px'>  

这里实现的是swiper的宽度是100%,的正方形轮播,效果如图

当然你也可以设置成80%等等,原理相同

解决问题了请点赞哈哈

原文地址:https://www.cnblogs.com/hiveme/p/8194819.html