Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solutions: PullToRefresh cannot trigger pull-up action #3521

Closed
liangzhuang327 opened this issue Jan 17, 2020 · 3 comments
Closed

Solutions: PullToRefresh cannot trigger pull-up action #3521

liangzhuang327 opened this issue Jan 17, 2020 · 3 comments

Comments

@liangzhuang327
Copy link

liangzhuang327 commented Jan 17, 2020

1、antd-mobile的PullToRefresh组建在有的手机上无法触发上拉加载

查看现象,是没有触发PullToRefresh的上拉刷新的功能,查看源码发现ele.scrollHeight-ele.scrollTop != ele.clientHeight
查看页面元素发现,ele的高度是动态算出来的,因为是移动端,是用rem动态算出来的,算法是

let remHeight = 2.48 // 元素用rem表示的高度
let __fontUnit = 43.3333 // 不同分辨率下的手机1rem代表的像素
someElementHeight = remHeight * __fontUnit

最终呈现到页面的高度因为不同手机的分辨率someElementHeight可能出现小数A;而用js代码得到的
ele.clientHeight却始终是整数B,从而真实someElementHeight的px值和通过js代码得到的真实值就可能在不
同的机型上存在误差;

// 如果A<B, 那么元素的真实设置的高度就是A(设置在style上),那么ele.scrollTop就可能比理论(元素的高度为B)值偏大,ele.scrollHeight-ele.scrollTop != ele.clientHeight,经过长时间测试发现等式左边会始终比等式右边小1!从而不会触发滑动的临界值
  • 结论:
    因为使用rem的原因,导致动态设置到元素上的高度(px)有可能小数,从而造成ele.scrollHeight-ele.scrollTop != ele.clientHeight
  • 解决方案
    • 1、在动态给元素赋值的时候取一下绝对值(不管取ceil还是floor),但不一定保证等于ele.clientHeight,故不推荐此中方案
    • 2、改源码,0<=Math.abs(ele.scrollHeight-ele.scrollTop - ele.clientHeight) <=1,至于为什么用区间,因为在部分机型上MIX2S的scrollTop会有小数情况,此处不在贴出

备注:源码位置,在PullToRefresh.js中isEdge方法中,为了避免升级版本导致不可用,可自行将此源码拷贝,建议自行封装此组建

源码改成PullToRefresh.js中的isEdge方法:

_this2.isEdge = function (ele, direction) {
            var container = _this2.props.getScrollContainer();
            if (container && container === document.body) {
                // In chrome61 `document.body.scrollTop` is invalid
                var scrollNode = document.scrollingElement ? document.scrollingElement : document.body;
                if (direction === UP) {
                    return scrollNode.scrollHeight - scrollNode.scrollTop <= window.innerHeight;
                }
                if (direction === DOWN) {
                    return scrollNode.scrollTop <= 0;
                }
            }
            if (direction === UP) {
                let __height = Math.abs(ele.scrollHeight - ele.scrollTop - ele.clientHeight)
                return __height>=0 && __height<=1 // 更改后代码
                return ele.scrollHeight - ele.scrollTop === ele.clientHeight; // 源码代码
            }
            if (direction === DOWN) {
                return ele.scrollTop <= 0;
            }
        };

@Lishiyong4217
Copy link

你好!请问怎么找这个源码,我在我项目全局搜索isEdge函数没搜到,请问预计那个版本修复这个问题?

@wumx
Copy link

wumx commented Oct 9, 2020

let __height = Math.abs(ele.scrollHeight - ele.scrollTop - ele.clientHeight)
这个建议换成
var __height = Math.abs(ele.scrollHeight - ele.scrollTop - ele.clientHeight)
否则build会出现 Failed to minify the code from this file

@awmleer
Copy link
Member

awmleer commented Sep 23, 2021

v2 已经停止维护了,建议升级到 v5 吧:
https://next.mobile.ant.design/components/pull-to-refresh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants