微信小程序轮播图开发


帮企商城 2021-09-23 15:38:26

图怪兽_cd908474545cb1109e7cdaf395874a16_77595.jpg

一、js

 data: {
    host: getApp().globalData.baseUrl,
    carouselList:[],
  },
 onLoad: function (options) {
    this.requestCarouselListData();//请求轮播图
  },
  //请求轮播图
  requestCarouselListData(){
    var that = this;//注意this指向性问题
    var urlStr = that.data.host + "/xjj/chome_carousel_list.json"; //请求连接注意替换(我用本地服务器模拟)
    console.log("请求轮播图:" + urlStr);
    wx.request({
      url: urlStr,
      data: {//这里放请求参数,如果传入参数值不是String,会被转换成String 
        // x: '',
        // y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log("轮播图返回值:");
        console.log(res.data.result);
        var resultArr = res.data.result;
        that.setData({
          carouselList: resultArr
        })
      }
    })
  },

//点击了轮播图
chomeCarouselClick: function (event) {
    var urlStr = event.currentTarget.dataset.url;
    console.log("点击了轮播图:" + urlStr);
    // wx.navigateTo({
    //   url: 'test?id=1'
    // })
  },

2. wxml

<!-- 轮播图 -->
<view class='carousel'> 
  <swiper class='carousel_swiper' indicator-dots="true" indicator-color="#f4f4f4" indicator-active-color="#4eb8b8" autoplay="true" interval='2000' circular='true'>
    <block wx:for="{{carouselList}}" wx:key="key">
      <swiper-item bindtap='chomeCarouselClick' data-url='{{item.url}}'>
        <image class="carouselImg" src='{{host}}{{item.img}}' mode='aspectFill' ></image>
      </swiper-item>
    </block>
  </swiper>
 </view>
/*几个有用的说明:
  indicator-dots 是否显示指示器
  indicator-color 指示器默认颜色
  indicator-active-color   指示器选中颜色
  autoplay 是否自动播放
  interval 每一页停留的时长
  circular 播放到最后一页后是否再衔接第一页循环播放
*/

三、wxss

page { //这个是当前页整体的背景色
  background-color: #f4f4f4;
}
.carousel{
  width: 100%;
  background-color: rebeccapurple;
}
.carousel_swiper{
  width: 100%;
  height: 400rpx;
  display: block;
  position: relative;
  background: #f4f4f4;
}
.carouselImg{
  width: 100%;
  height: inherit;
}

1-210F60Z134Q3.jpg运行截图.png

附件

本地服务器 轮播图的数据 chome_carousel_list.json
{
    "result": [{
            "id": "101",
            "img": "/xjj/img/carousel_1.png",
            "title": "",
            "url": "https://www.baidu.com/"
        },
        {
            "id": "102",
            "img": "/xjj/img/carousel_2.png",
            "title": "百度翻译",
            "url": "https://fanyi.baidu.com/"
        },
        {
            "id": "103",
            "img": "/xjj/img/carousel_3.png",
            "title": "百度地图",
            "url": "https://map.baidu.com/"
        },
        {
            "id": "104",
            "img": "/xjj/img/carousel_4.png",
            "title": "简书是一个写博客的网站,挺好用的,可以试试看",
            "url": "https://www.jianshu.com/"
        }
    ]
}


相关阅读:

春哥团队获得1000万元投资将用于开发各种商业源码系统

春哥技术团队在京发布春哥数字人系统CGDigitalPersonSystem

春哥团队8周年:帮助更多的人实现在家创业赚钱

帮企商城国庆狂欢盛宴 放“价”到底!

脱单盲盒项目,日入过万不是梦!2021年最大风口项目!

如何给我们春哥团队付款?

微信小程序日常开发中常遇到的错误代码

小程序前端直传阿里云oss

微信小程序之image组件

解决微信小程序switchTab不能带参方法