zoukankan      html  css  js  c++  java
  • 微信小程序之回到顶部的两种方式

    第一种:使用view标签形式回到顶部

    WXML:

    <image src='../../img/button-top.png' class='goTop' hidden='{{!floorstatus}}' bindtap="goTop"></image>

    WXSS:

    /* 返回顶部 */
    .goTop{
      height: 80rpx;
      width: 80rpx;
      position: fixed;
      bottom: 50rpx;
      background: rgba(0,0,0,.3);
      right: 30rpx;
      border-radius: 50%;
    }

    JS:

      // 获取滚动条当前位置
      onPageScroll: function (e) {
        console.log(e)
        if (e.scrollTop > 100) {
          this.setData({
            floorstatus: true
          });
        } else {
          this.setData({
            floorstatus: false
          });
        }
      },
    
      //回到顶部
      goTop: function (e) {  // 一键回到顶部
        if (wx.pageScrollTo) {
          wx.pageScrollTo({
            scrollTop: 0
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
          })
        }
      },

    第二种 :使用scroll-view形式的回到顶部

    WXML:

    <scroll-view scroll-y scroll-top='{{topNum}}' bindscroll="scrolltoupper">
    <image src='../../img/button-top.png' class='goTop' hidden='{{!floorstatus}}' bindtap="goTop"></image>

    WXSS:

    /* 返回顶部 */
    .goTop{
      height: 80rpx;
      width: 80rpx;
      position: fixed;
      bottom: 50rpx;
      background: rgba(0,0,0,.3);
      right: 30rpx;
      border-radius: 50%;
    }

    JS:

      data:{
        topNum: 0
      }
    
      // 获取滚动条当前位置
      scrolltoupper:function(e){
        console.log(e)
        if (e.detail.scrollTop > 100) {
          this.setData({
            floorstatus: true
          });
        } else {
          this.setData({
            floorstatus: false
          });
        }
      },
    
      //回到顶部
      goTop: function (e) {  // 一键回到顶部
        this.setData({
          topNum: this.data.topNum = 0
        });
      },
  • 相关阅读:
    [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
    [转]最长回文子串——4种解法
    [转]通过金矿模型介绍动态规划
    一句话说清楚什么是闭包函数
    [转]as3事件流机制彻底理解
    Eclipse 快捷键
    文件打包与解压缩
    第5节 环境变量与文件查找
    vim的多标签
    java思维导图
  • 原文地址:https://www.cnblogs.com/rakich/p/10724720.html
Copyright © 2011-2022 走看看