阅读 45

项目实战-点餐小程序-12 首页-轮播图

一、首页轮播图的主要功能点

1.使用swiper和swiper-item组件

2.动态获取后台的轮播图照片

3.改变小程序默认的swiper显示高度

 

二、首页轮播图的代码

1.home.wxml

1 
2 <swiper indicator-dots="true" indicator-color="rgba(0, 0, 0, .3)" autoplay="true" circular="true" style=‘height:{{bannerHeight}}‘>
3 <swiper-item wx:for="{{bannerList}}" >
4 <image src="{{item.picUrl}}"  mode="widthFix" bindload="imageLoad" >image>
5 swiper-item>
6 swiper>

2.home.wxss

1 swiper image{
2   width: 100%;
3 }

3.home.js

Page({

  //页面的初始数据

  data: {
    //自定义数组,存放banner显示在页面上
    bannerList:[],
    //所有banner图片的高度
    bannerHeight: ‘‘,
  },

  onLoad: function (options) {
    //获取轮播图
    wx.cloud.database().collection("lunbotu")
    .get()
    .then(res=>{
      console.log("获取轮播图成功",res);
      this.setData({
        bannerList:res.data
      })
    })
    .catch(err=>{
      console.log("获取轮播图失败",err);
    })
  },

  //设置swiper的高度
  imageLoad(e){
    //获取当前屏幕的宽度
    let screenWidth = wx.getSystemInfoSync().windowWidth;
    console.log("获取图片的真实宽度",e.detail.width);
    console.log("获取图片的真实高度",e.detail.height);
    let imgWidth = e.detail.width //图片的真实宽度
    let imgHeight = e.detail.height //图片的真实高度
    //等比设置swiper的高度
    let swiperHeight = (screenWidth/imgWidth)*imgHeight+ "px"
    this.setData({
      bannerHeight:swiperHeight
    })
  },

})

三、首页轮播图的效果

 

 

原文:https://www.cnblogs.com/AnnLing/p/15093184.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐