uniapp 实现一键到顶和一键刷新功能

需求分析

页面向下滑动到一定距离时显示按钮,点击按钮可以一键到底,一键刷新,向上滑动到一定距离时按钮消失

需求实现

思路方法:封装一个组件,两个按钮:一键到顶和一键刷新按钮。在父页面引用组件,然后在父页面的onPageScroll事件中监听滚动高度,控制组件显隐。

核心功能:利用uniapp的onPageScroll 

文档介绍: uni.pageScrollTo(OBJECT) | uni-app官网 (dcloud.net.cn)

uni.pageScrollTo(OBJECT)

将页面滚动到目标位置。

平台差异说明

AppH5微信小程序支付宝小程序百度小程序字节跳动小程序、飞书小程序QQ小程序华为快应用360小程序
√(nvue不支持)

OBJECT参数说明

参数名类型必填说明
scrollTopNumber滚动到页面的目标位置(单位px)
selectorString选择器,App、H5、微信小程序2.7.3+ 、支付宝小程序1.20.0+支持
durationNumber滚动动画的时长,默认300ms,单位 ms
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

selector 语法 selector类似于 CSS 的选择器,但仅支持下列语法。

  • ID选择器:#the-id
  • class选择器(可以连续指定多个):.a-class.another-class
  • 子元素选择器:.the-parent > .the-child
  • 后代选择器:.the-ancestor .the-descendant
  • 跨自定义组件的后代选择器:.the-ancestor >>> .the-descendant
  • 多选择器的并集:#a-node, .some-other-nodes

示例:

uni.pageScrollTo({
	scrollTop: 0,
	duration: 300
});

封装组件如下:

<template>
  <view class="flex-column btns-wrap">
    <u-icon v-if="isToTop"
            name="arrow-upward"
            size="40"
            class="co-primary plr-8 ptb-8"
            @click="toTop"></u-icon>
    <view v-if="isRefresh" class="mid-line"></view>
    <u-icon v-if="isRefresh"
            name="reload"
            size="40"
            class="co-primary plr-8 ptb-8"
            @click="refresh"></u-icon>
  </view>
</template>

<script>
export default {
  props: {
    isToTop: { // 控制一键到顶按钮显隐
      type: Boolean,
      default: true
    },
    isRefresh: { // 控制一键刷新按钮显隐
      type: Boolean,
      default: true
    },
  },
  methods: {
    // 一键到顶,支持自定义duration
    toTop(duration) {
      uni.pageScrollTo({
        scrollTop: 0,
        duration: (duration === 0 || duration) ? duration : 500
      })
    },
// 一键刷新,传递事件给父组件
    refresh() {
      this.$emit('refresh');
    }
  },
}
</script>
<style lang="scss">
.btns-wrap {
  position: fixed;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 60rpx;
  bottom: 200rpx;
  right: 60rpx;
  box-shadow: 0 0 4px 1px #f0f0f0;
  z-index: 998;
}
.mid-line {
  border-bottom: 2rpx solid #f0f0f0;
}
</style>

父页面:


<template>
    <view>
        <TopAndRefresh ref="topAndRefreshRef" v-if="isTopAndRefresh" @refresh="fetchList" />
    </view>
</template>




<script>
import TopAndRefresh from '../components/topAndRefresh.vue'

export default {
    components: {
        TopAndRefresh
      },
    data() {
        return {
            isTopAndRefresh: false,
        }
    },
    onPageScroll(e) {
	    this.isTopAndRefresh = e.scrollTop > 500;
    },
    methods: {
        fetchList() {
            // 请求
        }
    }
}
</script>

效果如下:

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了将uniapp的tabBar换到部,我们需要进行以下步骤: 1.在App.vue中添加以下代码: ```vue <template> <div> <tab-bar></tab-bar> <router-view></router-view> </div> </template> <script> import tabBar from '@/components/tabBar.vue' export default { components: { tabBar } } </script> ``` 2.在tabBar.vue中添加以下代码: ```vue <template> <div class="tab-bar"> <div class="tab-bar-item" v-for="(item, index) in tabList" :key="index" @click="switchTab(index)"> <img :src="item.iconPath" :class="activeIndex === index ? 'active' : ''"> <p :class="activeIndex === index ? 'active' : ''">{{ item.text }}</p> </div> </div> </template> <script> export default { data() { return { activeIndex: 0, tabList: [ { iconPath: '/static/tabBar/home.png', selectedIconPath: '/static/tabBar/home-active.png', text: '首页', pagePath: '/pages/index/index' }, { iconPath: '/static/tabBar/mine.png', selectedIconPath: '/static/tabBar/mine-active.png', text: '我的', pagePath: '/pages/mine/mine' } ] } }, methods: { switchTab(index) { this.activeIndex = index uni.switchTab({ url: this.tabList[index].pagePath }) } } } </script> <style> .tab-bar { display: flex; justify-content: space-between; align-items: center; position: fixed; top: 0; left: 0; right: 0; height: 100px; background-color: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .tab-bar-item { display: flex; flex-direction: column; justify-content: center; align-items: center; width: 50%; height: 100%; } .tab-bar-item img { width: 40px; height: 40px;} .tab-bar-item p { font-size: 24px; margin-top: 10px; } .tab-bar-item img.active { width: 50px; height: 50px; } .tab-bar-item p.active { color: #007aff; } </style> ``` 3.在pages.json中添加以下代码: ```json { "globalStyle": { "navigationBarTextStyle": "white", "navigationBarTitleText": "核心圈", "navigationBarBackgroundColor": "#009cfd", "backgroundColor": "#ff12e9" }, "tabBar": { "position": "top", "color": "#7A7E83", "selectedColor": "#007aff", "backgroundColor": "#ffffff", "borderStyle": "black", "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "static/tabBar/home.png", "selectedIconPath": "static/tabBar/home-active.png" }, { "pagePath": "pages/mine/mine", "text": "我的", "iconPath": "static/tabBar/mine.png", "selectedIconPath": "static/tabBar/mine-active.png" } ] } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值