微信小程序scroll-view和swiper结合使用(附带图片,超详细)

微信小程序scroll-view和swiper结合使用

大家好,今天我们来学习一下scroll-view和swiper结合使用,好好看,好好学,超详细的。(这里我没有用动态的数据,我怕有些小伙伴看不懂我就用静态的数据)

第一步

首先,我们来了解一下微信小程序scroll-view和swiper的组件,进入小程序官方文档

在这里插入图片描述
在这里插入图片描述

第二步

直接上代码

<!-- 导航 -->
<scroll-view scroll-x enable-flex scroll-with-animation class="navScroll">
  <view id="{{'scroll'+ item.id}}" class="navItem">
    <view class="navContent {{navId == 0?'active':'' }}" bindtap="changNav" data-id="0">
      品牌介绍
    </view>
  </view>
  <view id="{{'scroll'+ item.id}}" class="navItem">
    <view class="navContent {{navId == 1?'active':'' }}" bindtap="changNav" data-id="1">
      专利认证
    </view>
  </view>
  <view id="{{'scroll'+ item.id}}" class="navItem">
    <view class="navContent {{navId == 2?'active':'' }}" bindtap="changNav" data-id="2">
      售后保险
    </view>
  </view>
  <view id="{{'scroll'+ item.id}}" class="navItem">
    <view class="navContent {{navId == 3?'active':'' }}" bindtap="changNav" data-id="3">
      加入我们
    </view>
  </view>
</scroll-view>

<swiper class="top_swiper" circular bindchange="topSwiper" current="{{currentId}}" style="height: {{windowHeight}}rpx;line-height: {{windowHeight}}rpx;">
  <swiper-item duration="{{duration}}">
    <view class="swiperItem" style="background-color: red;">A</view>
  </swiper-item>
  <swiper-item duration="{{duration}}">
    <view class="swiperItem" style="background-color: black;">B</view>
  </swiper-item>
  <swiper-item duration="{{duration}}">
    <view class="swiperItem" style="background-color: blue;">C</view>
  </swiper-item>
  <swiper-item duration="{{duration}}">
    <view class="swiperItem" style="background-color: orange;">D</view>
  </swiper-item>
</swiper>
.navScroll {
  display: flex;
  white-space: nowrap;
  height: 80rpx;
  background-color: rgba(83, 83, 83, 100);
  position: fixed;
  top: 0;
  z-index: 100;
}

.navScroll .navItem {
  width: calc(100%/4);
  font-size: 20rpx;
  border: 0rpx solid red;
  display: flex;
  justify-content: center;
  align-items: center;
}

.navScroll .navItem .active {
  background-color: rgba(238, 205, 141, 100);
  color: rgba(255, 255, 255, 100);
}

.navScroll .navContent {
  width: 60%;
  height: 48rpx;
  line-height: 48rpx;
  border-radius: 24rpx;
  border: 0rpx solid red;
  text-align: center;
  color: rgba(238, 205, 141, 100);
}

.top_swiper{
  width: 100%;
  text-align: center;
  color:#FFF;
  
}

.swiperItem{
  width: 100%;
  height: 100%;
}
// pages/test/test.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navId: 0, //导航栏id
    windowHeight: '', //屏幕的高度
    currentId: 0 //swiper下标
  },
  topSwiper(event) {
    let navId = event.detail.current; //获取swiper下标
    console.log(event.detail.current)
    this.setData({
      navId
    })
  },
  //导航栏
  changNav(event) {
    let navId = event.currentTarget.dataset.id; //获取导航栏下标
    let windowHeight = this.data.windowHeight; //
    console.log(windowHeight)
    if (this.data.currentId == navId) {
      return false;
    } else {
      this.setData({
        currentId: navId
      })
    }
    this.setData({
      navId,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取屏幕高度
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        let clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        let calc = clientHeight * rpxR;
        console.log(calc);
        console.log(rpxR);
        that.setData({
          windowHeight: calc,
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

如图所示:

在这里插入图片描述

结语

关于微信小程序scroll-view和swiper结合使用就介绍到这里 ,欢迎大家多多指教,互相交流,一起学习

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
是的,可以使用swiperscroll-view组件来实现微信小程序的左右分页功能。 首先,在wxml文件中添加swiperscroll-view组件,并设置相应的属性。 ```html <swiper class="swiper-container" current="{{current}}" duration="{{500}}" style="height:{{pageHeight}}px"> <swiper-item> <scroll-view class="scroll-view" scroll-x="{{true}}" scroll-y="{{false}}" style="width:{{windowWidth}}px;height:{{pageHeight}}px;"> <!--第一页内容--> </scroll-view> </swiper-item> <swiper-item> <scroll-view class="scroll-view" scroll-x="{{true}}" scroll-y="{{false}}" style="width:{{windowWidth}}px;height:{{pageHeight}}px;"> <!--第二页内容--> </scroll-view> </swiper-item> <!--更多swiper-item--> </swiper> ``` 其中,swiper组件设置了current属性,用于控制当前显示的页码;scroll-view组件设置了scroll-x属性,用于开启水平方向的滚动,并设置了样式宽度为屏幕宽度,高度为页面高度。 接着,在js文件中,监听swiper组件的change事件,用于动态更新当前页码。 ```javascript Page({ data: { current: 0, windowWidth: wx.getSystemInfoSync().windowWidth, pageHeight: wx.getSystemInfoSync().windowHeight }, onLoad: function () { //... }, onPageChange: function (e) { this.setData({ current: e.detail.current }) } }) ``` 最后,在样式文件中,设置swiper-container和scroll-view的样式,用于实现左右分页效果。 ```css .swiper-container { overflow: hidden; position: relative; } .scroll-view { white-space: nowrap; } .swiper-item { display: inline-block; vertical-align: top; width: 100%; } ``` 以上就是使用swiperscroll-view组件实现微信小程序左右分页功能的方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值