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

效果

实现代码

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}` });
    }
  }, 
})
相关推荐
努力学前端Hang34 分钟前
微信小程序安装tdsign组件库
微信小程序·小程序
2501_9151063238 分钟前
如何在 iOS 设备上理解和分析 CPU 使用率(windows环境)
android·ios·小程序·https·uni-app·iphone·webview
weixin_177297220691 小时前
[特殊字符]旧物重生,绿色未来——旧物回收二手交易小程序,开启环保新生活[特殊字符]
小程序·生活
说私域1 小时前
链动2+1模式S2B2C商城小程序在电商直播中的应用与影响研究
大数据·人工智能·小程序·流量运营
码农客栈1 小时前
小程序学习(十二)之命令行创建uni-app项目
学习·小程序·uni-app
莫桐2 小时前
微信小程序-横向轮播时,轮播高度随内容高度动态撑开
微信小程序·小程序
敲敲了个代码2 小时前
UniApp 的 rpx是什么,跟rem比呢?
前端·javascript·面试·职场和发展·微信小程序·uni-app
00后程序员张2 小时前
iOS 应用加固软件怎么选,从源码到IPA方案选择
android·ios·小程序·https·uni-app·iphone·webview
imHere·2 小时前
UniApp + 微信小程序 xr-frame 3D 开发实战指南
微信小程序·uni-app·xr
游戏开发爱好者82 小时前
iOS App 抓不到包时的常见成因与判断思路,结合iOS 调试经验
android·ios·小程序·https·uni-app·iphone·webview