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

效果

实现代码

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}` });
    }
  }, 
})
相关推荐
树下水月1 天前
微信小程序接口,必须使用https的443端口吗?
微信小程序·小程序·https
毕设源码-邱学长1 天前
【开题答辩全过程】以 灵山水牛奶配送小程序的设计与实现为例,包含答辩的问题和答案
小程序
2501_915918411 天前
基于Mach-O文件的动态库与静态库归属方案及API扫描实践
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
iOS 证书无法跨电脑使用?签名迁移方法一文讲透
android·ios·小程序·https·uni-app·iphone·webview
毕设源码-赖学姐1 天前
【开题答辩全过程】以 基于springboot的酒店预订小程序自动化订制系统为例,包含答辩的问题和答案
运维·小程序·自动化
土土哥V_araolin2 天前
双迹美业模式系统开发(现成源码)
小程序·个人开发·零售
2501_933907212 天前
南京本凡科技,怎么选择专业小程序开发才能提升品牌竞争力?
科技·微信小程序·小程序
心无旁骛~2 天前
【Claude Code开发】AI记账助手(miaozhang)微信小程序开发与部署完整指南
人工智能·微信小程序·notepad++
2501_933907212 天前
本凡科技提供的宁波小程序开发服务全面解决方案
科技·微信小程序·小程序
阿珊和她的猫2 天前
微信小程序静默授权异步问题的处理方案
微信小程序·状态模式·notepad++