前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序实现banner图轮播(动态获取数据),自动获取图片高度

微信小程序实现banner图轮播(动态获取数据),自动获取图片高度

作者头像
全栈程序员站长
发布2022-08-31 15:39:59
1.2K0
发布2022-08-31 15:39:59
举报

大家好,又见面了,我是你们的朋友全栈君。

效果图:

微信小程序实现banner图轮播(动态获取数据),自动获取图片高度
微信小程序实现banner图轮播(动态获取数据),自动获取图片高度

indicator-active-color=”#007aff”//当前选中的指示点颜色

js:

代码语言:javascript
复制
const app = getApp()

Page({
  data: {
     //-----------模拟banner图-----------
    imgUrls: [
      '/image/1545118381903.jpg',
      '/image/1545118566631.jpg'
    ],
    circular: true,
    //是否显示画板指示点  
    indicatorDots: true,
    //选中点的颜色  
    //是否竖直  
    vertical: false,
    //是否自动切换  
    autoplay: true,
    //自动切换的间隔
    interval: 3000,
    //滑动动画时长毫秒  
    duration: 1000,
    //所有图片的高度  
    imgheights: [],
    //图片宽度 
    imgwidth: 750,
    //默认  
    current: 0
  },
  imageLoad: function (e) {//获取图片真实宽度  
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight;
    console.log(imgwidth, imgheight)
    //计算的高度值  
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    var imgheights = this.data.imgheights;
    //把每一张图片的对应的高度记录到数组里  
    imgheights[e.target.dataset.id] = imgheight;
    this.setData({
      imgheights: imgheights
    })
  },
  bindchange: function (e) {
    // console.log(e.detail.current)
    this.setData({ current: e.detail.current })
  },
   }
})

wxml:

代码语言:javascript
复制
<!-------------首页轮播图-----------------  -->
<view class='swiper'>
  <swiper indicator-dots="{
  
  {indicatorDots}}" vertical="{
  
  {vertical}}" autoplay="{
  
  {autoplay}}" duration="{
  
  {duration}}" interval='{
  
  {interval}}'indicator-active-color="#007aff" bindchange="bindchange" circular="{
  
  {circular}}" style="height:{
  
  {imgheights[current]}}rpx;">
    <block wx:for='{
  
  {imgUrls}}' wx:key="{
  
  {index}}">
      <swiper-item>
        <image src="{
  
  {item}}" data-id='{
  
  {index}}' class="slide-image" mode="widthFix" bindload="imageLoad" />
      </swiper-item>
    </block>
  </swiper>
</view>

wxss:

代码语言:javascript
复制
.swiper image {
  width: 100%;
  height: auto;
}

.swiper-image {
  height: 100%;
  width: 100%;
}

.slide-image {
  height: 100%;
  width: 100%;
  display: block;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/143227.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档