微信小程序:实现单页面内的翻页功能

效果

实现代码

wxml

通过if去判断是哪个页面

html 复制代码
<view wx:if="{{ currentPage == 'page1' }}">
  页面1
  <!-- 按钮 -->
  <view class="Btn_layout">
    <view class="Btn1">
      <view bindtap="submitForm" class="Btn_op" bindtap="nextPage">下一页</view>
    </view>
  </view>
</view>
<view wx:if="{{ currentPage == 'page2' }}">
  页面2
  <!-- 按钮 -->
  <view class="Btn_layout">
    <view class="Btn">
      <view bindtap="submitForm" class="Btn_op" bindtap="previousPage">上一页</view>
      <view bindtap="submitForm" class="Btn_op" bindtap="nextPage">下一页</view>
    </view>
  </view>
</view>
<view wx:if="{{ currentPage == 'page3' }}">
  页面3
  <!-- 按钮 -->
  <view class="Btn_layout">
    <view class="Btn1">
      <view bindtap="submitForm" class="Btn_op" bindtap="previousPage">上一页</view>
    </view>
  </view>
</view>

wxss

只写了按钮的样式

css 复制代码
/* 提交按钮 */
.Btn_layout {
  padding: 8% 0;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 按钮 */
.Btn {
  width: 95%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1000;
}

.Btn1 {
  width: 95%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  z-index: 1000;
}

.Btn_op {
  width: 48%;
  height: 32px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 2px solid #4472c4;
  background-color: #4472c4;
  color: #fff;
  letter-spacing: 2px;
  border-radius: 5px;
}

js

javascript 复制代码
Page({
  data: {
    currentPage: 'page1', //默认第一页
  },
  // 上一页的事件处理函数
  previousPage: function() {
    let currentPage = this.data.currentPage;
    let pageIndex = parseInt(currentPage.replace('page', ''));
    if (pageIndex > 1) {
      pageIndex--;
      this.setData({ currentPage: `page${pageIndex}` });
    }
  },
  // 下一页的事件处理函数
  nextPage: function() {
    let currentPage = this.data.currentPage;
    let pageIndex = parseInt(currentPage.replace('page', ''));
    // 假设最大页面数为3
    if (pageIndex < 3) {
      pageIndex++;
      this.setData({ currentPage: `page${pageIndex}` });
    }
  }, 
})
相关推荐
HERR_QQ18 小时前
【unify】unify的微信小程序开发学习 (to be continued)
学习·微信小程序·小程序
racerun1 天前
小程序导航设置更多内容的实现方法
小程序
说私域1 天前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的超级文化符号构建路径研究
人工智能·小程序·开源
mg6681 天前
微信小程序入门实例_____快速搭建一个快递查询小程序
微信小程序·小程序
程序员柳1 天前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
Jyywww1211 天前
微信小程序学习笔记
笔记·学习·微信小程序
The_era_achievs_hero1 天前
微信小程序41~50
微信小程序·小程序
走,带你去玩2 天前
uniapp 微信小程序水印
微信小程序·小程序·uni-app
是一碗螺丝粉2 天前
🔥 微信H5视频自动播放终极秘籍:WeixinJSBridge竟是官方“通行证”?
微信小程序