微信小程序 setData动态设置数组中的数据

现在有一组死数据

但是想在使用时动态修改其中的值

在setData中不能直接设置,真想。。。。。。,算了是这个和谐的社会救了那个工程师。


接下来,我们去征服她!

死数据:

  //地图上方控件
    controls: [
      {
        id: 1,
        iconPath: '../image/jiuyuan.png',
        position: {
          left: 85,
          top: 450-50,
          width: 70,
          height: 50
        },
        clickable: true
      },
      {
        id: 2,
        iconPath: '../image/jiuzhu.png',
        position: {
          left:165,
          top: 450 - 50,
          width: 70,
          height: 50
        },
        clickable: true
      },
    ],
我的设计初衷是让这两个图标始终在手机屏幕距离顶部85%的位置

所以我需要在获取到手机的屏幕相关信息之后动态设置position中left和top 的值

人狠话不多,直接上代码!

//获取用户手机相关信息
  getUserSystemInfo:function(){
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        console.log("手机型号"+res.model)
        console.log(res.pixelRatio)
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        console.log(res.language)
        console.log(res.version)
        //更新数据
        console.log(that.data.controls)
        that.data.controls[0].position.top = (res.windowHeight) * 0.85
        that.data.controls[1].position.top = (res.windowHeight)*0.85
        that.data.controls[0].position.left = (res.windowWidth) * 0.27
        that.data.controls[1].position.left = (res.windowWidth) * 0.54
       
        that.setData({
          UserSystemInfo:res,
          controls: that.data.controls
        })
       
      }
    })
  }

大功告成!愿每一个工程师在开发的道路上少一些坎坷。。。愿世界和平!hiahia~~~

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值