Last active
August 4, 2020 16:22
-
-
Save kreativan/40365d69ac74cb7512447ff9ea11d61b to your computer and use it in GitHub Desktop.
Crreate gallery using uikit lightbox.
This file contains hidden or 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
<div> | |
<a href="img_1.jpg" data-uk-gallery></a> | |
<a href="img_2.jpg" data-uk-gallery></a> | |
<a href="img_3.jpg" data-uk-gallery></a> | |
</div> | |
<div> | |
<a href="img_1.jpg" data-uk-gallery="cars"></a> | |
<a href="img_2.jpg" data-uk-gallery="cars"></a> | |
<a href="img_3.jpg" data-uk-gallery="cars"></a> | |
</div> | |
<style> | |
// Dynamic lightbox mobile fix | |
@media(max-width: 480px) { | |
.uk-lightbox-items img { | |
width: 90%; | |
height: auto; | |
} | |
} | |
</style> | |
<script> | |
window.addEventListener("DOMContentLoaded", function() { | |
uikitGallery(); | |
uikitGallery("cars"); | |
}); | |
</script> |
This file contains hidden or 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
/** | |
* Uikit Gallery | |
* @param {string} group | |
* @example <a href="image.jpg" data-uk-gallery=""></a> | |
* | |
*/ | |
function uikitGallery(group = "") { | |
let selector = (group == '') ? "a[data-uk-gallery]" : `a[data-uk-gallery="${group}"]`; | |
var elements = document.querySelectorAll(selector); | |
var i = 0; | |
elements.forEach(el => { | |
var index = i++; | |
el.addEventListener("click", e => { | |
e.preventDefault(); | |
let items = []; | |
for (var i = 0; i < elements.length; i++) { | |
let img = elements[i].getAttribute("href"); | |
let imgObj = {source: img}; | |
items.push(imgObj); | |
} | |
// console.log(items); | |
UIkit.lightboxPanel({ | |
items: items, | |
animation: 'scale', | |
}).show(index); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment