|
| 1 | +<!DOCTYPE html> |
| 2 | +<html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="pageFocusApp"> |
| 3 | +<head> |
| 4 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 5 | + <title>angular-inview basic example</title> |
| 6 | + <style type="text/css"> |
| 7 | + #hud { |
| 8 | + background: white; |
| 9 | + border: 1px solid gray; |
| 10 | + bottom: 0; |
| 11 | + margin: 20px; |
| 12 | + min-width: 220px; |
| 13 | + position: fixed; |
| 14 | + right: 0; |
| 15 | + top: 0; |
| 16 | + width: 25%; |
| 17 | + overflow: auto; |
| 18 | + } |
| 19 | + |
| 20 | + #lines { |
| 21 | + list-style: none; |
| 22 | + margin: 0; |
| 23 | + padding: 0; |
| 24 | + } |
| 25 | + |
| 26 | + #lines li { |
| 27 | + height: 100px; |
| 28 | + } |
| 29 | + |
| 30 | + #lines li:nth-child(odd) { |
| 31 | + background-color: lightgray; |
| 32 | + } |
| 33 | + </style> |
| 34 | +</head> |
| 35 | +<body ng-controller="basicCtrl"> |
| 36 | + |
| 37 | + <div id="hud"> |
| 38 | + <ul> |
| 39 | + <li ng-repeat="l in inviewLogs" ng-bind-html="l.message"></li> |
| 40 | + </ul> |
| 41 | + </div> |
| 42 | + |
| 43 | + <ul id="lines"> |
| 44 | + <li ng-repeat="t in testLines" |
| 45 | + in-view="lineInView($index, $inview, $inviewInfo)" |
| 46 | + in-view-options="{ considerPageFocus: true }">This is test line #{{$index}}</li> |
| 47 | + </ul> |
| 48 | + |
| 49 | + <script src="../node_modules/angular/angular.js"></script> |
| 50 | + <script src="../angular-inview.js"></script> |
| 51 | + <script type="text/javascript"> |
| 52 | + angular.module('pageFocusApp', ['angular-inview']).controller('basicCtrl', function basicCtrl ($scope, $sce) { |
| 53 | + var logId = 0; |
| 54 | + $scope.testLines = []; |
| 55 | + for (var i = 20; i >= 0; i--) { |
| 56 | + $scope.testLines.push(i); |
| 57 | + }; |
| 58 | + $scope.inviewLogs = []; |
| 59 | + $scope.lineInView = function(index, inview, inviewInfo) { |
| 60 | + console.log('inview', index); |
| 61 | + var inViewReport = inview ? '<strong>enters</strong>' : '<strong>exit</strong>'; |
| 62 | + var inviewpart = []; |
| 63 | + for (var p in inviewInfo.parts) { |
| 64 | + if (inviewInfo.parts[p]) { |
| 65 | + inviewpart.push(p); |
| 66 | + } |
| 67 | + } |
| 68 | + inviewpart = inviewpart.length % 4 === 0 ? 'all' : inviewpart.join(', '); |
| 69 | + if (typeof(inviewpart) != 'undefined') { |
| 70 | + inViewReport = '<strong>' + inviewpart + '</strong> parts ' + inViewReport; |
| 71 | + } |
| 72 | + $scope.inviewLogs.unshift({ id: logId++, message: $sce.trustAsHtml('Line <em>#' + index + '</em>: ' + inViewReport) }); |
| 73 | + return false; |
| 74 | + } |
| 75 | + document.addEventListener('focus', () => { |
| 76 | + $scope.$apply(() => { |
| 77 | + $scope.inviewLogs.unshift({ id: logId++, message: $sce.trustAsHtml('Document: focussed') }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + document.addEventListener('blur', () => { |
| 81 | + $scope.$apply(() => { |
| 82 | + $scope.inviewLogs.unshift({ id: logId++, message: $sce.trustAsHtml('Document: lost focus') }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + </script> |
| 87 | +</body> |
| 88 | +</html> |
0 commit comments