微信小程序轮播图渲染(示例)

第一步:utlis下创建的api.js写上封装


const GET = 'GET';
const POST = 'POST';
 
const baseURL = 'https://www.zzgoodqc.cn';//配置的域名

function request(method, url, data) {
    return new Promise(function(resolve, reject) {
        let header = {
            'content-type': 'application/json',
        };
        wx.request({
            url: baseURL + url,
            method: method,
            data: method === POST ? JSON.stringify(data) : data,
            header: header,
            success(res) {
                console.log(res);
                //请求成功
                //判断code是否为0表示成功
                if (res.data.code == 0) {
                    resolve(res);
                } else {
                    //其他错误
                    //reject('运行出错,请稍后再试');
                    console.log(res.data.msg);
                    wx.showToast({   //弹出框
                        title: res.data.msg,
                        icon: 'error',
                        duration: 2000
                      });
                }
            },
            fail(err) {
                //请求失败
                reject(err)
            }
        })
    })
}


const API = {
    newsList: () => request(GET, `/index.php/index/index/getImgList`),
  };

//接口可以集合到一块,集中管理

module.exports = {
  API: API
}

第二步:mydiv.wxml(写你自己创建的wxml里):

<view class="parent">
  <!-- 轮播图 -->
  <swiper bindchange="monitorCurrent" indicator-dots="{{false}}" circular="true" indicator-color="white"
    indicator-active-color="orange" current="{{current}}" autoplay="{{autoplay}}">
    <block wx:for="{{obj}}" wx:key="*this">
      <swiper-item>
      <image class="imgss" src="{{item}}"></image>
        <!-- <view class=""></view> -->
      </swiper-item>
    </block>
  </swiper>
  <!-- 自定义轮播图进度点 -->
  <view class="dots">
    <block wx:for="{{backgroundArr}}" wx:for-index="index" wx:key="*this">
      <view class="{{current==index?'active':''}}"></view>
    </block>
  </view>
</view>


第三步:mydiv.wxss:

.parent {
  position: relative;
}

swiper {
  height: 400rpx;
}

swiper-item view {
  height: 100%;
}

.red {
  background-color: Pink;
}

.green {
  background-color: PaleGreen;
}

.blue {
  background-color: SkyBlue;
}

.dots {
  position: absolute;
  bottom: 30rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

.dots view {
  width: 10rpx;
  height: 10rpx;
  margin: 0 6rpx;
  border-radius: 10rpx;
  background-color: #fff;
}

.dots .active {
  width: 30rpx;
  background-color: orange;
}
.imgss{
  width: 750rpx;
}

第四步:mydiv.js

const app = getApp()
const $api = require('../../utils/api').API

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //轮播图的数组
    backgroundArr: ['red', 'green', 'blue','orange','pink','qqq','wwww'],
    //轮播图当前的下标
    current: 0,
    //是否自动播放轮播图
    autoplay: false,
    obj:[]
  },

  //监听轮播图的下标
  monitorCurrent: function (e) {
    // console.log(e.detail.current)
    let current = e.detail.current;
    this.setData({
      current: current
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    $api.newsList().then(res=>{
      
      console.log(res.data.data);
      this.setData({
        obj:res.data.data
    })
        })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //开启轮播图
    this.setData({
      autoplay: true
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //关闭轮播图
    this.setData({
      autoplay: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //关闭轮播图
    this.setData({
      autoplay: false
    })
  },

})

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值