微信小程序获取屏幕高度宽度

let that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          widheight: res.screenHeight,
          widwidth: res.screenWidth
        });
      }
    });

根据屏幕宽度高度将某个元素固定在屏幕中间
先获取屏幕宽高,再获取当前元素的宽高,剩下的距离再减去一半就是当前元素的left值

var that = this;
    var query = wx.createSelectorQuery();
    query.select('.create_news').boundingClientRect(function (rect) {
      // console.log(rect.width)
      var createnewmargin = that.data.widwidth - rect.width
      that.setData({
        currleft: createnewmargin * 1 / 2
      })
    }).exec();

你可能感兴趣的:(微信小程序获取屏幕高度宽度)