小程序之輪播圖

一、js

 data: {
    host: getApp().globalData.baseUrl,
    carouselList:[],
  },
 onLoad: function (options) {
    this.requestCarouselListData();//請(qǐng)求輪播圖
  },
  //請(qǐng)求輪播圖
  requestCarouselListData(){
    var that = this;//注意this指向性問題
    var urlStr = that.data.host + "/xjj/chome_carousel_list.json"; //請(qǐng)求連接注意替換(我用本地服務(wù)器模擬)
    console.log("請(qǐng)求輪播圖:" + urlStr);
    wx.request({
      url: urlStr,
      data: {//這里放請(qǐng)求參數(shù),如果傳入?yún)?shù)值不是String,會(huì)被轉(zhuǎn)換成String 
        // x: '',
        // y: ''
      },
      header: {
        'content-type': 'application/json' // 默認(rèn)值
      },
      success(res) {
        console.log("輪播圖返回值:");
        console.log(res.data.result);
        var resultArr = res.data.result;
        that.setData({
          carouselList: resultArr
        })
      }
    })
  },

//點(diǎn)擊了輪播圖
chomeCarouselClick: function (event) {
    var urlStr = event.currentTarget.dataset.url;
    console.log("點(diǎn)擊了輪播圖:" + 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>
/*幾個(gè)有用的說明:
  indicator-dots 是否顯示指示器
  indicator-color 指示器默認(rèn)顏色
  indicator-active-color   指示器選中顏色
  autoplay 是否自動(dòng)播放
  interval 每一頁停留的時(shí)長
  circular 播放到最后一頁后是否再銜接第一頁循環(huán)播放
*/

三、wxss

page { //這個(gè)是當(dāng)前頁整體的背景色
  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;
}
運(yùn)行截圖.png

附件

本地服務(wù)器 輪播圖的數(shù)據(jù) 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": "簡書是一個(gè)寫博客的網(wǎng)站,挺好用的,可以試試看",
            "url": "http://www.itdecent.cn/"
        }
    ]
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近工作上項(xiàng)目不忙,然后自己學(xué)習(xí)了一下微信小程序開發(fā),自己做點(diǎn)記錄,有剛開始學(xué)習(xí)的同學(xué)也可以看一下,互相交流. 什...
    l寧采臣l閱讀 1,487評(píng)論 0 4
  • swiper滑塊視圖容器 swiper滑塊視圖容器用來在指定區(qū)域內(nèi)切換內(nèi)容的顯示,常用于制作海報(bào)輪播圖效果和頁簽內(nèi)...
    hi武林高手閱讀 1,721評(píng)論 0 2
  • 不管是做APP開發(fā)還是小程序,移動(dòng)端離不開輪播圖的功能,下面就寫一個(gè)小程序的輪播圖功能分享給大家 效果圖: ste...
    LemonnYan閱讀 26,413評(píng)論 1 6
  • create by jsliang on 2018-9-17 17:58:56 Recently revised...
    梁_飄閱讀 1,678評(píng)論 0 6
  • 微信小程序的特點(diǎn) 小程序是一種不需要下載、安裝即可使用的應(yīng)用,它出現(xiàn)了觸手可及的夢(mèng)想,用戶掃一掃或者搜一下即開打開...
    Simple_3f19閱讀 968評(píng)論 0 0

友情鏈接更多精彩內(nèi)容