Skip to content

Instantly share code, notes, and snippets.

View bob-lee's full-sized avatar

Bob Lee bob-lee

  • MyTrucking.com
  • New Zealand
View GitHub Profile
@bob-lee
bob-lee / polyfill-ie11-nodelist-foreach.js
Created November 24, 2017 18:41
Polyfill for IE11 missing NodeList.forEach
if ('NodeList' in window && !NodeList.prototype.forEach) {
console.info('polyfill for IE11');
NodeList.prototype.forEach = function (callback, thisArg) {
thisArg = thisArg || window;
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, this[i], i, this);
}
};
}
@bob-lee
bob-lee / workpage-handlebars-template.html
Created November 24, 2017 18:47
Work page Handlebards template
<div id="images" class="images">
{{#each images}}
<div class="image" data-idx="{{@index}}">
<picture>
<img data-idx="{{@index}}" data-src="{{imageSrc this}}" class="image1" alt="{{this.fileName}}">
</picture>
</div>
{{/each}}
@bob-lee
bob-lee / intersection-observer-setup-1.js
Last active November 24, 2017 20:47
IntersectionObserver setup 1
const INTERSECT_PAGESIZE = 2;
const list = document.querySelectorAll('img.image1');
let intersectionObserver; // instance of observer
let intersectionRatio; // 1: fully shown, 0.5: half shown, ...
let elementToObserve; // img tag being observed
let _indexToObserve; // index of img tag being observed
Object.defineProperty(this, "indexToObserve", {
get: function () { return _indexToObserve; },
@bob-lee
bob-lee / intersection-observer-setup-2.js
Created November 24, 2017 18:54
IntersectionObserver setup 2
intersectionObserver = new IntersectionObserver(function(entries) {
const entry = entries[0]; // observe one element
const currentRatio = intersectionRatio;
const newRatio = entry.intersectionRatio;
const boundingClientRect = entry.boundingClientRect;
const scrollingDown = currentRatio !== undefined && newRatio < currentRatio &&
boundingClientRect.bottom < boundingClientRect.height;
intersectionRatio = newRatio;
@bob-lee
bob-lee / intersection-observer-setup-3.js
Last active November 24, 2017 19:05
IntersectionObserver setup 3
// register 'load' event handlers
list.forEach(function(img) {
img.addEventListener('load', function() {
observe(img);
});
});
// Now all set up, let's start loading first two images
indexToObserve = 0;
@bob-lee
bob-lee / intersection-observer-setup-4.js
Created November 24, 2017 18:57
IntersectionObserver setup 4
function observe(me) { // observe new element, me
if (!me) {
return;
}
const index = me.getAttribute('data-idx');
if (index == indexToObserve) {
elementToObserve = me;
intersectionObserver.observe(me);
<img [src]="(image.toLoad && image.url) || ''"
(load)="loaded(image)"
class="image1" alt="{{image.fileName}}">
@bob-lee
bob-lee / image.service.ts
Last active November 24, 2017 21:54
image.service setup for IntersectionObserver
const INTERSECT_PAGESIZE = 2;
@Injectable()
export class ImageService {
list: any[] = [];
intersectionObserver: IntersectionObserver;
intersectionRatio: number;
elementToObserve: Element;
return this.http.get(`${API}/${path}`).toPromise().then((snapshot) => {
this.list = [];
// prepare a list for intersection observer
this._indexToObserve = 0;
const items = snapshot.json();
this.list = items.reverse().map((item, index) => {
/* provide 2 additional properties for each image:
toLoad: boolean, the component to load image,
index: number, the component to call observe(item) on image loaded
import React from 'react';
import getUrls from './api'
const INTERSECT_PAGESIZE = 2;
export default class Observer extends React.Component {
state = { list: [] }
intersectionRatio = null; // 1: fully shown, 0.5: half shown, ..
elementToObserve = null; // img tag being obseved
_indexToObserve = null; // index of img tag being obseved