I was wondering what is the best way to prevent variables from being global when they need to be used across multiple functions within the same module.
As you can see I need those variables to be available throughout - but I don't necessarily want then to be defined in the global as that would muck things up in the end. Do I wrap them in a function?
Also if anybody has any tips on ways to improve this code I'd love to hear it.
Here's my code example:
// Info Bullet Slide Out
var slideOut,
clickedButton,
clickedParent,
activeClass = 'is-active',
openClass = 'is-open';
function closeSlideOut(){
$('.tlx-img-point').removeClass(activeClass);
slideOut.removeClass(openClass);
clickedParent.removeClass(activeClass);
}
function openSlideOut(){
slideOut = $('.' + clickedButton.attr('id'));
slideOut.addClass(openClass);
clickedParent.addClass(activeClass);
clickedButton.addClass(activeClass);
}
$('.tlx-img-point').on('click', function(){
clickedButton = $(this);
clickedParent = clickedButton.parent();
// If you clicked on the same button twice just close the slideout
if($(this).hasClass('is-active')){
closeSlideOut();
// If you clicked on another info button close this one and open the new one
}else if(clickedParent.hasClass(activeClass)){
closeSlideOut();
// Delay 5ms to allow css animation to complete
setTimeout(function(){
openSlideOut();
}, 650);
// Open the info slide out
}else{
openSlideOut();
}
});