当前位置:   article > 正文

uniapp页面回到顶部方法_uniapp 回到顶部

uniapp 回到顶部

本文讲的是在uniapp项目中实现页面回顶效果的方法。以下是代码(回顶可能多个页面都需要用到建议封装成一个组件)

一、方法一

  1. <template>
  2. <view class="content">
  3. <view class="" v-for="(item,index) in 100" :key="index">
  4. {{index}}
  5. </view>
  6. <view class="upward" v-if="isShow" @click="Totop()">
  7. <u-icon name="arrow-upward" color="#434343" size="28"></u-icon>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. isShow:false,
  16. }
  17. },
  18. onPageScroll(e){
  19.              // 监听页面滚动
  20. if(e.scrollTop>200){
  21. this.isShow=true;
  22. }else{
  23. this.isShow=false;
  24. }
  25. },
  26. methods: {
  27. Totop(){
  28. uni.pageScrollTo({
  29. scrollTop: 0,//滚动到页面的目标位置
  30. duration: 300
  31. });
  32. }
  33. }
  34. }
  35. </script>
  36. <style lang="less">
  37. .content{
  38. width: 100%;
  39. position: relative;
  40. .u-tabs{
  41. width: 100%;
  42. // margin: 18rpx auto;
  43. height: 80rpx;
  44. display: flex;
  45. align-items: center;
  46. background-color: #fff;
  47. }
  48. .upward{
  49. width: 70rpx;
  50. height: 70rpx;
  51. display: flex;
  52. justify-content: center;
  53. align-items: center;
  54. border-radius: 100%;
  55. border: 3rpx solid #d0d0d0;
  56. margin-bottom: 20rpx;
  57. background-color: rgba(255, 255, 255, 0.4);
  58. position: fixed;
  59. bottom: 300rpx;
  60. right: 30rpx;
  61. }
  62. }
  63. </style>

onPageScroll是页面生命周期,监听页面滚动,参数为Object

uni.pageScrollTo相关参数在官方文档可以查看

效果图(页面滚动距离大于200显示回顶按钮)

二、使用uView组件

  1. <template>
  2. <view class="wrap">
  3. <text>滑动页面,返回顶部按钮将出现在右下角</text>
  4. <u-back-top :scroll-top="scrollTop"></u-back-top>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. scrollTop: 0
  12. }
  13. },
  14. onPageScroll(e) {
  15. this.scrollTop = e.scrollTop;
  16. }
  17. };
  18. </script>
  19. <style lang="scss" scoped>
  20. .wrap {
  21. height: 200vh;
  22. }
  23. </style>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/312480
推荐阅读
相关标签
  

闽ICP备14008679号