微信小程序接收后端数据并显示轮播图

轮播图、是否自动轮播autoplay、修改轮播速度slider部分代码:
index.wxml

<view class="container" style="">
    <!--轮播图-->
  <swiper class="home-swiper" indicator-dots="true" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
    <block wx:for-items="{{lunboData}}">
      <swiper-item>
        
        <image src="{{item.imgurl}}" class="slide-image" data-tourl='{{item.link}}'bindtap="changeurl" />
      
      </swiper-item>
    </block>
  </swiper>
  <button bindtap="changeAutoplay">
  change autoplay
  </button>
</view>
<view>
  <slider bindchange="changeInterval" show-value step="100" min="600" max="1800"/>
</view>


index.js

Page({
  /**
 * 页面的初始数据
 */
  data: {
    src: '/images/girl.png',
    name: 'Hello World',
  //轮播图配置
    autoplay:true,
    interval: 3000,
    duration: 1200
  },
  /**
 * 自定义函数--获取微信用户信息
	   */
  getMyInfo: function (e) {
    let info = e.detail.userInfo;
    this.setData({
      src: info.avatarUrl, //更新图片来源
      name: info.nickName  //更新昵称
    })
    console.log(info);

  },
  /*onLoad: function () {
    var that = this;
    var data = {
      "datas": [{"id": 1,"imgurl": "http://news.jiangnan.edu.cn/__local/9/F9/7C/EFECC58D5C9BD5AAB242A846E8C_6321FDE4_44A01.jpg"
        },
        {
          "id": 2,
          "imgurl": "http://news.jiangnan.edu.cn/__local/8/A5/D5/AD4930674E67DD550218B76192D_C5B38DBA_4B294.jpg"
        },
        {
          "id": 3,
          "imgurl": "../../images/51cp.jpg"
        },
        {
          "id": 4,
          "imgurl": "../../images/91cp.jpg"
        }
      ]
    };
    that.setData({
      lunboData: data.datas
    })
  },*/
 onLoad:function(options){
    var that = this;
    wx.request({
      url: 'http://localhost:8080/weixinlunbo',
      data: {},
      method: 'GET', 
      // header: {}, // 设置请求的 header 默认是application/json
      success: function (res) {
        var data = {
          "datas": [{
            "id": 1, 
            "imgurl": "",
            "link":""
          },
          {
            "id": 2,
            "imgurl": "",
            "link": ""
          },
          {
            "id": 3,
            "imgurl": "",
            "link": ""
          },
          {
            "id": 4,
            "imgurl": "",
            "link": ""
          }
          ]
        };
        data.datas[0].imgurl = res.data[0].img;
        data.datas[1].imgurl = res.data[1].img;
        data.datas[2].imgurl = res.data[2].img;
        data.datas[3].imgurl = res.data[3].img;
        data.datas[0].link = "image1";
        data.datas[1].link = "image2";
        data.datas[2].link = "image3";
        data.datas[3].link = "image4";
        console.log("autoplay:"+res.data[4].autoplay)
        that.setData({
          lunboData: data.datas,
          autoplay:res.data[4].autoplay
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  changeAutoplay:function(){
      var that=this;
      if(that.data.autoplay==true){
        that.setData({
          autoplay: false
        })
      }
      else{
        that.setData({
          autoplay: true
        })
      }
    console.log("后"+that.data.autoplay)
  },
  changeInterval:function(e){
    var that=this;
    that.setData({
      interval: e.detail.value
    })
    console.log("轮播速度:"+e.detail.value+"ms")
  },
  changeurl:function(e){
    var url = e.currentTarget.dataset.tourl
    console.log( e.currentTarget.dataset.tourl)
    wx.navigateTo({
      url: url,
    })
  }
})

index.wxss

/* pages/index/index.wxss */
.container {
  height: 80vh; /*高100视窗,这里写100%无效*/
  display: flex; /*flex布局模式*/
  flex-direction: column; /*垂直布局*/
  align-items: center; /*水平方向居中*/
   /*justify-content: space-around;垂直方向分散布局*/
	}
/*	image {
    margin-top: 50rpx;
    margin-bottom: 50rpx;
	  width: 300rpx; 
    border-radius: 50%;
}*/
text {
  font-size: 50rpx; /*字体大小*/
  }
.home-swiper {
  width: 100%;
  height: 40%;
}

.slide-image {
  width: 100%;
  height: 100%;
}

后端部分代码:

 @RequestMapping("/weixinlunbo")
    public void weixinlunbo(HttpServletResponse response) throws Exception{
        JSONArray lunbo =new JSONArray();
        for(int i=0;i<4;i++){
            JSONObject lunbo1=new JSONObject();
            lunbo1.put("id",i);
            if(i==0){
                lunbo1.put("img","http://news.jiangnan.edu.cn/__local/9/F9/7C/EFECC58D5C9BD5AAB242A846E8C_6321FDE4_44A01.jpg");
            }
            if(i==1){
                lunbo1.put("img","http://news.jiangnan.edu.cn/__local/8/A5/D5/AD4930674E67DD550218B76192D_C5B38DBA_4B294.jpg");
            }
            if(i==2){
                lunbo1.put("img","../../images/51cp.jpg");
            }
            if(i==3){
                lunbo1.put("img","../../images/91cp.jpg");
            }
            lunbo.add(lunbo1);
        }
        JSONObject attr=new JSONObject();
        attr.put("autoplay",false);
        lunbo.add(attr);
    response.getWriter().write(lunbo.toString());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值