forked from lim-james/Geekshacking
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SEO & GA. Fixed Lazyload issue + minor edits
- Loading branch information
1 parent
c312549
commit 9747d9f
Showing
4 changed files
with
427 additions
and
409 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
registerListener('load', setLazy); | ||
registerListener('load', lazyLoad); | ||
registerListener('scroll', lazyLoad); | ||
|
||
const observer = new MutationObserver(lazyLoad); | ||
var elementToObserve = document.querySelector(".mu-sponsor-logos-area"); | ||
observer.observe(elementToObserve, {attributes: true, subtree: true, childList: true}); | ||
|
||
var lazy = []; | ||
|
||
function setLazy() { | ||
lazy = document.getElementsByClassName('lazy'); | ||
console.log('Found ' + lazy.length + ' lazy images'); | ||
} | ||
|
||
function lazyLoad() { | ||
for (var i = 0; i < lazy.length; i++) { | ||
if (isInViewport(lazy[i])) { | ||
if (lazy[i].getAttribute('data-src')) { | ||
lazy[i].src = lazy[i].getAttribute('data-src'); | ||
lazy[i].removeAttribute('data-src'); | ||
} | ||
} | ||
} | ||
|
||
cleanLazy(); | ||
} | ||
|
||
function cleanLazy() { | ||
lazy = Array.prototype.filter.call(lazy, function (l) { return l.getAttribute('data-src'); }); | ||
} | ||
|
||
function isInViewport(el) { | ||
var rect = el.getBoundingClientRect(); | ||
|
||
return ( | ||
rect.bottom >= 0 && | ||
rect.right >= 0 && | ||
rect.top <= (window.innerHeight || document.documentElement.clientHeight) && | ||
rect.left <= (window.innerWidth || document.documentElement.clientWidth) | ||
); | ||
} | ||
|
||
function registerListener(event, func) { | ||
if (window.addEventListener) { | ||
window.addEventListener(event, func) | ||
} else { | ||
window.attachEvent('on' + event, func) | ||
} | ||
} |
Oops, something went wrong.