Populating array with new items
This commit is contained in:
+43
-25
@@ -1,7 +1,13 @@
|
|||||||
function initLightGallery (params = {}) {
|
class LGallery {
|
||||||
|
items
|
||||||
|
lightGallery
|
||||||
|
element
|
||||||
|
index = 0
|
||||||
|
|
||||||
|
init (params = {}) {
|
||||||
// Create the lightGallery instance
|
// Create the lightGallery instance
|
||||||
const lgEl = document.getElementById('lightgallery')
|
this.element = document.getElementById('lightgallery')
|
||||||
const lg = lightGallery(lgEl, Object.assign({
|
this.lightGallery = lightGallery(this.element, Object.assign({
|
||||||
plugins: [lgZoom, lgThumbnail, lgVideo, lgFullscreen],
|
plugins: [lgZoom, lgThumbnail, lgVideo, lgFullscreen],
|
||||||
speed: 500,
|
speed: 500,
|
||||||
/*
|
/*
|
||||||
@@ -25,34 +31,46 @@ function initLightGallery (params = {}) {
|
|||||||
for (let i = 0; i < 50; i++) {
|
for (let i = 0; i < 50; i++) {
|
||||||
items = items.concat(params.items)
|
items = items.concat(params.items)
|
||||||
}
|
}
|
||||||
|
this.items = items
|
||||||
// Append thumbnails
|
|
||||||
items.forEach(item => {
|
|
||||||
if (item.video) {
|
|
||||||
lgEl.insertAdjacentHTML('beforeend', `<a data-video='${item.video}'
|
|
||||||
${item.downloadUrl ? 'data-download-url="' + item.downloadUrl + '"' : ''}>
|
|
||||||
<img alt="" src="${item.thumbnailUrl}"/><div class="play-icon"></div></a>`)
|
|
||||||
} else {
|
|
||||||
lgEl.insertAdjacentHTML('beforeend', `<a href="${item.previewUrl}"
|
|
||||||
${item.downloadUrl ? 'data-download-url="' + item.downloadUrl + '"' : ''}>
|
|
||||||
<img alt="" src="${item.thumbnailUrl}"/></a>`)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
lg.refresh()
|
|
||||||
|
|
||||||
let timeout
|
let timeout
|
||||||
window.addEventListener('scroll', () => {
|
window.addEventListener('scroll', () => {
|
||||||
if (timeout) clearTimeout(timeout)
|
if (timeout) clearTimeout(timeout)
|
||||||
timeout = setTimeout(handleScroll, 100)
|
timeout = setTimeout(lgallery.handleScroll, 100)
|
||||||
})
|
})
|
||||||
}
|
lgallery.handleScroll()
|
||||||
|
}
|
||||||
|
|
||||||
function handleScroll () {
|
handleScroll () {
|
||||||
const scrollPosition = window.innerHeight + window.scrollY
|
const rect = lgallery.element.getBoundingClientRect()
|
||||||
const pageHeight = document.documentElement.scrollHeight
|
const scrollPosition = Math.max(0, rect.bottom - window.innerHeight)
|
||||||
const buffer = 100 // pixels before bottom to trigger load
|
const buffer = 200 // pixels before bottom to trigger load
|
||||||
|
|
||||||
if (pageHeight - scrollPosition <= buffer) {
|
if (scrollPosition <= buffer) {
|
||||||
loadMoreItems()
|
lgallery.loadMoreItems()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadMoreItems () {
|
||||||
|
const numberOfItems = 20
|
||||||
|
if (this.index < this.items.length) {
|
||||||
|
// Append thumbnails
|
||||||
|
this.items
|
||||||
|
.slice(this.index, this.index + numberOfItems)
|
||||||
|
.forEach(item => {
|
||||||
|
if (item.video) {
|
||||||
|
this.element.insertAdjacentHTML('beforeend', `<a data-video='${item.video}'
|
||||||
|
${item.downloadUrl ? 'data-download-url="' + item.downloadUrl + '"' : ''}>
|
||||||
|
<img alt="" src="${item.thumbnailUrl}"/><div class="play-icon"></div></a>`)
|
||||||
|
} else {
|
||||||
|
this.element.insertAdjacentHTML('beforeend', `<a href="${item.previewUrl}"
|
||||||
|
${item.downloadUrl ? 'data-download-url="' + item.downloadUrl + '"' : ''}>
|
||||||
|
<img alt="" src="${item.thumbnailUrl}"/></a>`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.lightGallery.refresh()
|
||||||
|
this.index += numberOfItems
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const lgallery = new LGallery()
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
<script src="/share/static/lg/lg-video.min.js"></script>
|
<script src="/share/static/lg/lg-video.min.js"></script>
|
||||||
<script src="/share/static/lg/lg-zoom.min.js"></script>
|
<script src="/share/static/lg/lg-zoom.min.js"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
initLightGallery(<%- JSON.stringify({lgConfig, items, openItem}) %>) // initLightGallery imported from web.js
|
lgallery.init(<%- JSON.stringify({lgConfig, items, openItem}) %>) // initLightGallery imported from web.js
|
||||||
<% if (false && openItem) { %>
|
<% if (false && openItem) { %>
|
||||||
const openItem = <%- openItem %>
|
const openItem = <%- openItem %>
|
||||||
const thumbs = document.querySelectorAll('#lightgallery a')
|
const thumbs = document.querySelectorAll('#lightgallery a')
|
||||||
|
|||||||
Reference in New Issue
Block a user