Skip to content

Instantly share code, notes, and snippets.

@TylerFisher
Created May 21, 2014 17:39
Show Gist options
  • Save TylerFisher/1550c351b7ebc5ca98b8 to your computer and use it in GitHub Desktop.
Save TylerFisher/1550c351b7ebc5ca98b8 to your computer and use it in GitHub Desktop.
An example of good code structure for Unclass
// all variables I will use
var photosArray;
var photosArrayCounter;
var $visiblePhoto;
// all functions
var changePhoto = function() {
if ($(this).hasClass('next') && photosArrayCounter < photosArray.length) {
photosArrayCounter++;
}
else if ($(this).hasClass('prev') && photosArrayCounter > 0) {
photosArrayCounter--;
}
$visiblePhoto.attr('src', photosArray[photosArrayCounter]);
}
// what should happen when the page loads
$(document).ready(function() {
// declare variables
photosArray = ['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg'];
photosArrayCounter = 0;
$visiblePhoto = $('.photo');
// attach event handlers
$('.next').click(changePhoto);
$('.prev').click(changePhoto);
// call any functions that need to run on page load
// we don't have any
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment