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

微信小程序实现banner图轮播(动态获取数据),自动获取图片高度效果图:indicator-active-color=”#007aff”//当前选中的指示点颜色js:constapp=getApp()Page({data:{//———–模拟banner图———–imgUrls:[‘/image/1545118381903.jpg’,’/imag…

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

效果图:

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

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

js:

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:

<!-------------首页轮播图-----------------  -->
<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:

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

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

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据库ER图怎么画

    数据库ER图怎么画https://jingyan.baidu.com/article/624e745961c6a134e9ba5a40.html

    2022年6月21日
    22
  • linux ubuntu 关闭防火墙命令,LINUX关闭防火墙的方法

    linux ubuntu 关闭防火墙命令,LINUX关闭防火墙的方法LINUX关闭防火墙的方法导语:LINUX关闭防火墙的方法,你懂吗?下面的是百分网小编为大家搜集的LINUX关闭防火墙的方法,欢迎阅读,谢谢!(1)重启后永久性生效:开启:chkconfigiptableson关闭:chkconfigiptablesoff(2)即时生效,重启后失效:开启:serviceiptablesstart关闭:serviceiptablesstop需要说…

    2025年7月17日
    0
  • NOIP2014_noip比赛时间

    NOIP2014_noip比赛时间NOIp2012day1T1Vigenère密码标签:模拟主要是用了ASCII码,字母’A’的ASCII码是41H(01000001B),字母’a’的ASCII码是61H(01100001B),字母’A’与’a’的二进制后5位是相同的,所以无论是大写字母还是小写字母x,x&31(11111B)的值就是x在字母表里的顺序。简单判一下边界就行了c…

    2022年8月22日
    4
  • 微信支付java实例_java开发微信应用

    微信支付java实例_java开发微信应用JAVA开发集成微信支付(WXPay)遇到的坑!

    2022年4月20日
    39
  • python的for循环是什么循环_while循环的用法举例

    python的for循环是什么循环_while循环的用法举例在本篇博客中,我们将讨论Python中for循环的原理。我们将从一组基本例子和它的语法开始,还将讨论与for循环关联的else代码块的用处。然后我们将介绍迭代对象、迭代器和迭代器协议,还会学习如何创建自己的迭代对象和迭代器。之后,我们将讨论如何使用迭代对象和迭代器实现for循环,以及利用while循环通过迭代器协议实现for循环逻辑。最后,我们将反编译一…

    2022年8月12日
    5
  • DataGridview获取选中行数「建议收藏」

    DataGridview获取选中行数「建议收藏」DataGridvie获取选中行数代码:dataGridView1.CurrentRow.Index//获取选中行数使用Messbox.Show()弹窗:

    2022年7月15日
    14

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号