温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序中怎么实现轮播图

发布时间:2022-04-13 14:48:02 阅读:322 作者:iii 栏目:编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍“微信小程序中怎么实现轮播图”,在日常操作中,相信很多人在微信小程序中怎么实现轮播图问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”微信小程序中怎么实现轮播图”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

业务需求:

5个图片轮番播放,可以左右滑动,点击指示点可以切换图片 

重点说明:

由于微信小程序,整个项目编译后的大小不能超过1M

查看做轮播图功能的一张图片大小都已经有100+k了

那么我们可以把图片放在服务器上,发送请求来获取。

index.wxml:

这里使用小程序提供的<swiper>组件
autoplay:自动播放
interval:自动切换时间
duration:滑动动画的时长
current:当前所在的页面
bindchange:current 改变时会触发 change 事件
由于<swiper>组件提供的指示点样式比较单一,另外再自定义指示点的样式

<view class="recommend" > 
 <view class="swiper-container"> 
  <swiper autoplay="auto" interval="5000" duration="500" current="{{swiperCurrent}}" bindchange="swiperChange" class="swiper"> 
   <block wx:for="{{slider}}" wx:key="unique"> 
    <swiper-item data-id="{{item.id}}" data-url="{{item.linkUrl}}"> 
     <image src="{{item.picUrl}}" class="img"></image> 
    </swiper-item> 
   </block> 
  </swiper> 
  <view class="dots"> 
   <block wx:for="{{slider}}" wx:key="unique"> 
    <view class="dot{{index == swiperCurrent ? ' active' : ''}}" bindtap="chuangEvent" id="{{index}}">{{index+1}}</view> 
   </block> 
  </view> 
 </view> 
</view>

index.wxss:

.swiper-container{ 
 position: relative; 
} 
.swiper-container .swiper{ 
 height300rpx; 
} 
.swiper-container .swiper .img{ 
 width100%; 
 height100%; 
} 
.swiper-container .dots{ 
 position: absolute; 
 right40rpx; 
 bottom20rpx; 
 display: flex; 
 justify-content: center; 
} 
.swiper-container .dots .dot{ 
 margin0 10rpx; 
 width28rpx; 
 height28rpx; 
 background#fff; 
 border-radius50%; 
 transition: all .6s; 
 font300 18rpx/28rpx "microsoft yahei"; 
 text-align: center; 
} 
.swiper-container .dots .dot.active{ 
 background#f80; 
 color:#fff; 
}

 index.js:

//导入js 
var util = require('../../utils/util.js'Page({ 
 data: { 
  slider: [], 
  swiperCurrent0 
 }, 
 onLoadfunction () { 
  var that = this//网络访问,获取轮播图的图片 
  util.getRecommend(function(data){ 
   that.setData({ 
    slider: data.data.slider 
   }) 
  });  
 }, 
 //轮播图的切换事件 
 swiperChangefunction(e){ 
//只要把切换后当前的index传给<swiper>组件的current属性即可 
  this.setData({ 
   swiperCurrent: e.detail.current 
  }) 
 }, 
 //点击指示点切换 
 chuangEventfunction(e){ 
  this.setData({ 
   swiperCurrent: e.currentTarget.id 
  }) 
 } 
})

utils.js:

//网络访问 
function getRecommend(callback{ 
 wx.request({ 
  url'https://c.y.qq.com/musichall/fcgi-bin/fcg_yqqhomepagerecommend.fcg'  data: { 
   g_tk5381   uin0   format'json'   inCharset'utf-8'   outCharset'utf-8'   notice0   platform'h6'   needNewCode1   _: Date.now() 
  }, 
  method'GET'  header: {'content-Type''application/json'}, 
  success: function(res){ 
   if(res.statusCode == 200){ 
    callback(res.data); 
   } 
  } 
 }) 
} 
 
module.exports = { 
 getRecommend: getRecommend 
}

运行:

微信小程序中怎么实现轮播图

到此,关于“微信小程序中怎么实现轮播图”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://www.xuebuyuan.com/3289032.html

AI

开发者交流群×