Angular:
http://angularjs.org/
HTML
<script src="http://airve.github.io/js/verge/verge.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<div ng-controller="MyCtrl">
<div ng-repeat="article in articles" class="block" scroll="atNewArticle(item)" scroll-item="article">
<h1>{{article.title}}</h1>
<p>{{article.body}}</p>
</div>
</div>
CSS
.block{
height:300px;
}
JavaScript
var app = angular.module('myApp', []);
app.directive('scroll', function ($parse, $document, $window) {
var _ = $window._;
var verge = $window.verge;
var visibleElements = [];
return {
restrict: 'A',
scope: {
scroll: '&',
scrollItem: '='
},
link: function (scope, element, attrs) {
var debounced = _.debounce(function() {
// You might need a different test,
// perhaps including the height of the element,
// or using verge "rectangle" function
var visible = verge.inViewport(element);
var index = visibleElements.indexOf(scope.scrollItem);
var previouslyVisible = (index != -1);
if (visible && !previouslyVisible) {
visibleElements.push(scope.scrollItem);
scope.$apply(function() {
scope.scroll({item:scope.scrollItem});
});
}
if (!visible && previouslyVisible) {
visibleElements.splice(index, 1);
}
}, 500);
angular.element($document).on('scroll', debounced);
if (verge.inViewport(element)) {
visibleElements.push(element);
}
}
};
});
function MyCtrl($scope) {
$scope.articles = [
{title:'article1',body:'body of article1'},
{title:'article2',body:'body of article2'},
{title:'article3',body:'body of article3'}
];
$scope.atNewArticle = function (article) {
//i only execute when transitioning back and forth between articles
console.log(article);
};
}