Responsive Image Gallery Lightbox with jQuery, Fancybox 3, and Slick carousel
Creating a responsive image gallery can be a headache for a front-end developer. Fancybox 3 and Slick carousel provide solutions to this in a very simple way. The only prerequisite required is knowledge of Javascript and its jQuery library.
What is Fancybox 3 ?
- This is a tool built on top of jQuery library.
- It offers an elegant way to add zooming functionalities for images, HTML content, and multimedia on your web pages.
- It can display images, Html elements, SWF movies, iframes, and ajax requests.
- It is also customizable through
CSS
. - Here is a link to the Fancybox documentation
What is a Slick carousel ?
- This is a fully responsive and flexible jQuery plugin.
- Used for creating a fully responsive, customizable, and mobile-friendly carousel that works with any HTML element.
- Here is a link to the Slick carousel documentation
In the index.html,
- Link the style.css and the script.js files to the index.html file.
Add
Font-awesome
,jQuery
,Bootstrap
,Fancybox
, andSlick
CDN links above the initial links.Caution: make sure you have the latest jQuery link pasted above
Fancybox
andSlick
CDN links. Check the index file screenshots for clarification It should come first.CSS links added in the
head
element
JS script links added above body
closing tag
- Add a
section
with twodiv
elements.You can name the classes as you like. e.gclass="slider"
andclass="slider-nav"
to wrap the two image slides we want to create. - Provide the images within the first
div
i.eclass="slider"
withdata-fancybox
value for eacha
tag. This automatically binds click events that will start Fancybox.js. Use thedata-src
attribute to specify the source of the image. You can also use thehref
attribute instead. - Within the second
div
i.eclass="slider-nav"
, add a list with the images from theclass="slider"
in the same order. - Add the custom styles for
class="slider"
andclass="slider-nav"
. - Take note of the dynamic classes created dynamically in the script.js and added to style.css.
Codepen code for refence.
In the script.js file,
- Call the slick plugin on whole
section, id="slider-section"
anddiv, class="slider"
and then declare the properties you want for the slide. - Do the same, call slick plugin on
class="slider-nav"
and declare the properties you want. Add a Fancybox function to keep the thumbnails active when changing the main image, via mouse/touch drag/swipe while removing all active classes and setting active class for the current slide
Github repository link: github.com/Michael-Otieno/carousel_gallery
- Deployed link : lighthearted-sunburst-59664a.netlify.app
Final screenshots.