微信小程序3D炫彩輪播圖

先看下一下效果圖:


不多BB,直接上代碼:


js部分:

function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

Page({

  data: {
    currentIndex: 0
  },

  onLoad: function (options) {

  },
  /* 這裏實現控制中間凸顯圖片的樣式 */
  handleChange: function (e) {
    this.setData({
      currentIndex: e.detail.current,
      bgColor: getRandomColor()
    })
  },
})

 wxml部分:

<swiper class="imageContainer" bindchange="handleChange" previous-margin="50rpx" next-margin="50rpx" circular autoplay>
  <block wx:for="{{3}}" wx:key="{{index}}">
    <swiper-item class="item">
      <view class="itemImg {{currentIndex == index ? ' active ': ' '}}" style='background: {{bgColor}};'></view>
    </swiper-item>
  </block>
</swiper>

<view class='layout'><text style='color:{{bgColor}}'>這是一個寂寞的天,下着有些傷心的雨</text></view>

wxss部分:

page {
  background: #f7f7f7f7;
}

.imageContainer {
  width: 100%;
  height: 500rpx;
  background: #000;
  counter-reset: wangxiaoer;
}

.item {
  height: 500rpx;
}

.itemImg:before {
  content: counters(wangxiaoer, '') '. ';
  counter-increment: wangxiaoer;
  position: relative;
  display: block;
  top: 50%;
  transform: translateY(-50%);
  background-color: #fff;
  width: 60rpx;
  height: 60rpx;
  border-radius: 30rpx;
  margin: 0 auto;
  text-align: center;
}

.itemImg {
  position: absolute;
  width: 100%;
  height: 380rpx;
  border-radius: 15rpx;
  z-index: 5;
  opacity: 0.7;
  top: 13%;
  background-color: #f00;
}

.active {
  opacity: 1;
  z-index: 10;
  height: 430rpx;
  top: 7%;
  transition: all 0.2s ease-in 0s;
  background-color: #ccc;
}

.layout {
  width: 750rpx;
  height: 200rpx;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

text {
  font-weight: 700;
}

 本文適合對小程序開發有一定的基礎的人,小白可先看一下小程序文檔中的swiper組件(https://developers.weixin.qq.com/miniprogram/dev/component/swiper.html)。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章