Skip to content

Instantly share code, notes, and snippets.

@banjeremy
Last active August 29, 2015 14:13
Show Gist options
  • Save banjeremy/e49628f92a203026d99c to your computer and use it in GitHub Desktop.
Save banjeremy/e49628f92a203026d99c to your computer and use it in GitHub Desktop.
Angular directive for thumbnail hover previews.
'use strict';
app.directive('thumbnail', function($interval){
return {
restrict: 'E',
scope: {
thumbs: '&thumbs',
speed: '&speed'
},
template: '<div class="thumbnail"><img ng-src="assets/images/thumbnails/{{thumb}}"/></div>',
link: function(scope, element, attrs){
scope.thumb = scope.thumbs[0];
var timerId = 0,
thumbIndex = 0;
var updateImage = function(){
if (thumbIndex === scope.thumbs.length -1) {
thumbIndex = 0;
}
scope.thumb = scope.thumbs[++thumbIndex];
};
var startTimer = function(){
return $interval(updateImage, scope.speed);
};
var stopTimer = function(id){
$interval.cancel(id);
};
element.on('mouseover', function(){
timerId = startTimer();
});
element.on('mouseout', function(){
stopTimer(timerId);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment