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

fix: infinityscroll bugs for #1149 #1160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/mip-infinitescroll/infinitescroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

define(function (require) {
// 使用了jquery $.Deferred
var util = require('util');
var $ = require('jquery');
var viewport = require('viewport');
var InfiniteScroll = function (opt) {
Expand All @@ -16,6 +17,7 @@ define(function (require) {

opt.$result = $(opt.$result);
opt.$loading = $(opt.$loading);
opt.$ele = opt.ele;

// 设置默认值
me.options = $.extend({
Expand Down Expand Up @@ -176,15 +178,16 @@ define(function (require) {
var scrollHandler;
viewport.on('scroll', function scrollHandler () {
// 若为暂停状态,什么也不做

if (me.state === 'pause') {
if (me.state === 'pause' || !me._isElementInViewport()) {
return;
}

// 获取当前滚动条位置
me.currentScrollTop = viewport.getScrollTop();
// 某些浏览器(安卓QQ)滚动时会隐藏头部但不触发resize,需要反复获取 wtf...
me.wrapperHeight = viewport.getHeight();
// 获取容器高度
me.scrollerHeight = viewport.getScrollHeight();

// 到顶了
if (me.currentScrollTop <= 0) {
Expand Down Expand Up @@ -261,7 +264,7 @@ define(function (require) {
// trigger scroll事件,确保继续触发数据加载
viewport.trigger('scroll');
}
// 失败
// 失败
}, function () {
// 标记数据状态为请求失败
me.dataStatus = 3;
Expand Down Expand Up @@ -376,6 +379,25 @@ define(function (require) {
);
},

/**
* 判断组件是否在可视区域内
*
* @return {boolean} true 或 false
*/
_isElementInViewport: function () {
var ele = this.options.$ele;
var rect = util.rect.getElementRect(ele);

var winWidth = viewport.getWidth();
var winHeight = viewport.getHeight();
var offRect = util.rect.getElementOffset(ele);
var offLeft = offRect.left;
var offTop = offRect.top;
var offWidth = offRect.width;
var offHeight = offRect.height;

return (offLeft > -offWidth && offLeft < winWidth && offTop > -offHeight && offTop < winHeight)
},
/**
* 获取当前可视区页码的方法(从0计)
*
Expand Down
3 changes: 2 additions & 1 deletion src/mip-infinitescroll/mip-infinitescroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ define(function (require) {
limitShowPn: 0,
preLoadPn: 2,
firstResult: [],
pushResult: self.pushResult
pushResult: self.pushResult,
ele: element
});
};

Expand Down