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 viewport bug #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 14 additions & 16 deletions blazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'use strict';

//private vars
var _source, _viewport, _isRetina, _supportClosest, _attrSrc = 'src', _attrSrcset = 'srcset';
var _source, _isRetina, _supportClosest, _attrSrc = 'src', _attrSrcset = 'srcset';

// constructor
return function Blazy(options) {
Expand Down Expand Up @@ -63,10 +63,9 @@
scope.options.src = _source = scope.options.src || 'data-src';
_supportClosest = Element.prototype.closest;
_isRetina = window.devicePixelRatio > 1;
_viewport = {};
_viewport.top = 0 - scope.options.offset;
_viewport.left = 0 - scope.options.offset;

scope.options._viewport = {};
scope.options._viewport.top = 0 - scope.options.offset;
scope.options._viewport.left = 0 - scope.options.offset;

/* public functions
************************************/
Expand Down Expand Up @@ -101,11 +100,11 @@
//throttle, ensures that we don't call the functions too often
util.validateT = throttle(function() {
validate(scope);
}, scope.options.validateDelay, scope);
}, scope.options._viewport.validateDelay, scope);
util.saveViewportOffsetT = throttle(function() {
saveViewportOffset(scope.options.offset);
saveViewportOffset(scope.options._viewport, scope.options.offset);
}, scope.options.saveViewportOffsetDelay, scope);
saveViewportOffset(scope.options.offset);
saveViewportOffset(scope.options._viewport, scope.options.offset);

//handle multi-served image src (obsolete)
each(scope.options.breakpoints, function(object) {
Expand Down Expand Up @@ -164,23 +163,22 @@

function elementInView(ele, options) {
var rect = ele.getBoundingClientRect();

if(options.container && _supportClosest){
// Is element inside a container?
var elementContainer = ele.closest(options.containerClass);
if(elementContainer){
var containerRect = elementContainer.getBoundingClientRect();
// Is container in view?
if(inView(containerRect, _viewport)){
if(inView(containerRect, options._viewport)){
var top = containerRect.top - options.offset;
var right = containerRect.right + options.offset;
var bottom = containerRect.bottom + options.offset;
var left = containerRect.left - options.offset;
var containerRectWithOffset = {
top: top > _viewport.top ? top : _viewport.top,
right: right < _viewport.right ? right : _viewport.right,
bottom: bottom < _viewport.bottom ? bottom : _viewport.bottom,
left: left > _viewport.left ? left : _viewport.left
top: top > options._viewport.top ? top : options._viewport.top,
right: right < options._viewport.right ? right : options._viewport.right,
bottom: bottom < options._viewport.bottom ? bottom : options._viewport.bottom,
left: left > options._viewport.left ? left : options._viewport.left
};
// Is element in view of container?
return inView(rect, containerRectWithOffset);
Expand All @@ -189,7 +187,7 @@
}
}
}
return inView(rect, _viewport);
return inView(rect, options._viewport);
}

function inView(rect, viewport){
Expand Down Expand Up @@ -328,7 +326,7 @@
return array;
}

function saveViewportOffset(offset) {
function saveViewportOffset(_viewport, offset) {
_viewport.bottom = (window.innerHeight || document.documentElement.clientHeight) + offset;
_viewport.right = (window.innerWidth || document.documentElement.clientWidth) + offset;
}
Expand Down