Last active
August 29, 2015 14:13
-
-
Save banjeremy/e49628f92a203026d99c to your computer and use it in GitHub Desktop.
Angular directive for thumbnail hover previews.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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