Adding Skeleventy-based site sources.
This commit is contained in:
parent
ac7f2e6002
commit
28a89401ff
79 changed files with 2183 additions and 0 deletions
55
sources/resources/js/modules/lazyload/index.js
Normal file
55
sources/resources/js/modules/lazyload/index.js
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import $$ from '@utilities/selectors'
|
||||
import { exists } from '@utilities/helpers'
|
||||
|
||||
const Lazyload = function Lazyload()
|
||||
{
|
||||
|
||||
// lazyload our images
|
||||
const images = $$.wrapper.querySelectorAll('[data-src]')
|
||||
|
||||
if ( exists(images) )
|
||||
{
|
||||
|
||||
// options
|
||||
const options = {
|
||||
threshold: 0.5
|
||||
}
|
||||
|
||||
const preloadImage = (img) => {
|
||||
|
||||
// find and store the image's data-load attribute
|
||||
const src = img.dataset.src
|
||||
|
||||
img.src = src
|
||||
|
||||
// add a class of loaded
|
||||
// we can then use this as a hook for fade-in animations etc
|
||||
img.classList.add('loaded')
|
||||
img.removeAttribute('data-src')
|
||||
|
||||
}
|
||||
|
||||
const lazyLoad = new IntersectionObserver((entries, lazyLoad) => {
|
||||
|
||||
entries.forEach(entry => {
|
||||
|
||||
if (entry.isIntersecting) {
|
||||
|
||||
preloadImage(entry.target)
|
||||
lazyLoad.unobserve(entry.target)
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}, options)
|
||||
|
||||
images.forEach(image => {
|
||||
lazyLoad.observe(image)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
export default Lazyload
|
||||
Loading…
Reference in a new issue