Skip to content

Commit b5452d1

Browse files
appotryzkqiang
authored andcommitted
🐛 修正懒加载的视图判断
1 parent 096ee55 commit b5452d1

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

source/js/utils.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ Fluid.utils = {
3838
elementVisible: function(element, offsetFactor) {
3939
offsetFactor = offsetFactor && offsetFactor >= 0 ? offsetFactor : 0;
4040
var rect = element.getBoundingClientRect();
41-
const viewportHeight = window.innerHeight || document.documentElement.clientHeight;
42-
return (
43-
(rect.top >= 0 && rect.top <= viewportHeight * (1 + offsetFactor) + rect.height / 2) ||
44-
(rect.bottom >= 0 && rect.bottom <= viewportHeight * (1 + offsetFactor) + rect.height / 2)
45-
);
41+
var vh = window.innerHeight || document.documentElement.clientHeight;
42+
var vw = window.innerWidth || document.documentElement.clientWidth;
43+
var expandH = vh * offsetFactor;
44+
var expandW = vw * offsetFactor;
45+
// 判断元素矩形与视口(上下左右各扩展 offsetFactor 屏)是否有重叠
46+
return rect.bottom > -expandH
47+
&& rect.top < vh + expandH
48+
&& rect.right > -expandW
49+
&& rect.left < vw + expandW;
4650
},
4751

4852
waitElementVisible: function(selectorOrElement, callback, offsetFactor) {
@@ -62,14 +66,15 @@ Fluid.utils = {
6266
return;
6367
}
6468
if ('IntersectionObserver' in window) {
69+
var margin = (window.innerHeight || document.documentElement.clientHeight) * offsetFactor + 'px';
6570
var io = new IntersectionObserver(function(entries, ob) {
6671
if (entries[0].isIntersecting) {
6772
callback();
6873
ob.disconnect();
6974
}
7075
}, {
7176
threshold : [0],
72-
rootMargin: (window.innerHeight || document.documentElement.clientHeight) * offsetFactor + 'px'
77+
rootMargin: margin + ' 0px'
7378
});
7479
io.observe(element);
7580
} else {

0 commit comments

Comments
 (0)