微信小程序02-轮播图实现与图片点击跳转

实现效果:

前言

在小程序01中,已经学习了如何制作底部导航栏,本节让我们一起学习如何制作轮播图,以及点击轮播图完成页面跳转。

实现过程

注:(1)前期学习,我这里用的本地的图片,后期的话,会对接后端接口,动态获取数据,前期方便学习,就暂时用的本地图片。(2)关于图片跳转地址,因为前期其他页面还没写,所以我这里跳转的是切换 Tab,如果后期数据中有返回跳转链接,可以去掉 navigator  标签中的   open-type="switchTab"  属性。

1 在 index.wxml 文件中,增加以下代码:

  <!-- 头部广告图 -->
  <swiper class="top-banner" circular="{{circular}}" indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}"  interval="{{interval}}" duration="{{duration}}">
    <block wx:for="{{images_list}}" wx:key="id">
      <navigator url="{{item.link}}" open-type="switchTab">
        <swiper-item >
            <image src="{{item.image_url}}" class="slide-image"></image>
        </swiper-item>
      </navigator>
    </block>
  </swiper>

2 在index.wxss 文件中增加轮播图的样式

.top-banner {
  width: 100%;
  height: 360rpx;
}
 
.slide-image {
  width: 100%;
  height: 100%;
}

3,在index.js 文件中 增加 data 参数

Page({
  data: {
    autoplay: true,
    indicatorDots: true,
    interval: 3000,
    duration: 1200,
    circular: true, 
    images_list : [
      {
        "id": 1,
        "image_url": "../../images/banner1.jpg",
        "link": "../list/list"
      },
      {
        "id": 2,
        "image_url": "../../images/banner2.jpg",
        "link": "../mine/mine"
      },
    ],
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs',
    })
  },
  onLoad() {
    
  },
})

 踩过的坑

 按着网上的教程,给图片增加跳转链接后(如下图),图片不显示。

 

 关于此问题的文档比较少,其中一篇说,navigator   标签放在 swiper-item 标签外面,于是我就试了试,代码改成如下图:

 然后轮播图就可以正常显示了。

  • 15
    点赞
  • 109
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
1. 在wxml文件中添加轮播图组件和按钮组件,例如: ```html <swiper class="swiper-container" indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}"> <swiper-item> <image src="{{imageUrls[0]}}" /> </swiper-item> <swiper-item> <image src="{{imageUrls[1]}}" /> </swiper-item> <swiper-item> <image src="{{imageUrls[2]}}" /> </swiper-item> </swiper> <button class="btn" bindtap="handleClick">点击跳转</button> ``` 2. 在js文件中定义轮播图组件的属性和点击事件处理函数,例如: ```javascript Page({ data: { imageUrls: [ 'http://img01.tooopen.com/images/20150828/sy_140703593077.jpg', 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg', 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg' ], indicatorDots: true, autoplay: true, interval: 5000, duration: 1000 }, handleClick: function() { wx.navigateTo({ url: '/pages/detail/detail' }) } }) ``` 3. 在点击事件处理函数中使用wx.navigateTo方法跳转到目标页面,例如: ```javascript wx.navigateTo({ url: '/pages/detail/detail' }) ``` 其中,'/pages/detail/detail'是目标页面的路径。注意,目标页面必须在app.json文件中注册过。 4. 在目标页面的wxml文件中展示跳转后的内容,例如: ```html <view class="detail">这是详情页</view> ``` 5. 在app.json文件中注册目标页面,例如: ```json { "pages": [ "pages/index/index", "pages/detail/detail" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle": "black" } } ``` 其中,"pages/detail/detail"是目标页面的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值