小(xiǎo)程序 swiper 左右箭頭/前進後退/方向控制

  1. 新聞資(zī)訊
  2. 技術百科
公司新聞 案例分(fēn)享 技術百科 行業動态

小(xiǎo)程序 swiper 左右箭頭/前進後退/方向控制

來源:奇站網絡 浏覽量:10193 發布日期: 2018-03-18

圖片輪播是很經常用到的效果,小(xiǎo)程度裏默認提供了swiper組件,不過并沒有左右切換的按鈕/箭頭,這裏探讨一(yī)下(xià)如何實現該功能。

一(yī)、靜态數據

先開(kāi)發預覽版本,數據全部用的js設定好的本地數據,實現方式如下(xià):

  1. <view class=swiper-car>
  2. <swiper current={{carIndex}} indicator-dots="{{swiperCar.indicatorDots}}" autoplay="{{swiperCar.autoplay}}" interval="{{swiperCar.interval}}" duration="{{swiperCar.duration}}">
  3. <block wx:for="{{carImgs}}" wx:key="cidx">
  4. <swiper-item>
  5. <image src="{{item}}" class="slide-image" />
  6. </view>
  7. </swiper-item>
  8. </block>
  9. </swiper>
  10. <button class=swiper-left bindtap="bindPrev">
  11. <image src=../../images/swiper-prev.png mode=widthFix></image>
  12. </button>
  13. <button class=swiper-right bindtap="bindNext">
  14. <image src=../../images/swiper-next.png mode=widthFix></image>
  15. </button>
  16. </view>

wxss

  1. .swiper-car {
  2. height: 430rpx;
  3. padding: 0 82rpx;
  4. position: relative;
  5. }
  6. .swiper-car swiper {
  7. height: 450rpx;
  8. }
  9. .swiper-car swiper-item {
  10. font-size: 11pt;
  11. }
  12. .swiper-car swiper-item image {
  13. height: 300rpx;
  14. }
  15. .swiper-car button {
  16. width: 35rpx;
  17. height: 65rpx;
  18. padding: 0;
  19. position: absolute;
  20. top: 50%;
  21. margin-top: -30rpx;
  22. border: 0;
  23. background: transparent;
  24. }
  25. .swiper-car button::after {
  26. border: 0;
  27. }
  28. .swiper-car button image {
  29. width: 35rpx;
  30. }
  31. .swiper-car .swiper-left {
  32. left: 25rpx;
  33. text-align: left;
  34. }
  35. .swiper-car .swiper-right {
  36. right: 25rpx;
  37. }

實現方式跟網頁端的輪播類似。

二、動态數據

上一(yī)個例子數據是直接在js裏給定,很奇怪的是更換成遠程獲取的數據,setData後發現圖片竟然無法顯示了,後面發現是swiper不能在嵌套裏的關系(?),移出view就可以了,但是之前的樣式無論如何都不起作用了。
剛好swiper上面有個heading,就将左右箭頭放(fàng)在heading裏,再調整下(xià)位置

  1. .heading {
  2. margin-top: 40rpx;
  3. padding: 15rpx;
  4. background: #fff;
  5. font-size: 11pt;
  6. font-weight: bold;
  7. color: #000;
  8. position: relative;
  9. }
  10. .heading .arrow {
  11. width: 30rpx;
  12. height: 59rpx;
  13. z-index: 99;
  14. padding: 80px 20rpx;
  15. }
  16. .heading .swiper-left {
  17. position: absolute;
  18. left: 0;
  19. top: 290rpx;
  20. transform: translate(0, -50%);
  21. cursor: pointer;
  22. }
  23. .heading .swiper-right {
  24. position: absolute;
  25. right: 0;
  26. top: 290rpx;
  27. transform: translate(0, -50%);
  28. }
标簽:

廈門市中資源網絡服務有限公司

電(diàn)話(huà):13313868605

QQ:3413772931

地址:廈門集美區軟件園三期


                    掃一(yī)掃加我(wǒ)咨詢