Skip to content

Instantly share code, notes, and snippets.

@wplit
Last active February 4, 2022 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wplit/f52ca9849976adbae1bad3a55749d3cb to your computer and use it in GitHub Desktop.
Save wplit/f52ca9849976adbae1bad3a55749d3cb to your computer and use it in GitHub Desktop.
close lightbox when element clicked, open lightbox on page load if hash matches
jQuery(document).ready(function($) {
let openSelector = '.open-lightbox';
let closeSelector = '.close-lightbox';
let hash = 'form' /* yourwebsite.com/#form */
/*
* close open lightbox when element clicked
*/
$(closeSelector).on('click', function(e) {
e.preventDefault();
$.fancybox.close();
});
/*
* open lightbox on page load by triggering a click on the click trigger if hash matches
*/
if(window.location.hash) {
let hashinURL = window.location.hash.substring(1);
if (hashinURL === hash) {
$(openSelector).trigger('click');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment