angular.module('app').directive('ScrollTrigger', () => { return { restrict: "A", link:function ($scope,$element,$attrs,$controller) { function debounce(fn, delay) { let timer = null; return function () { let context = this; let args = arguments; clearTimeout(timer); timer = setTimeout(function () { fn.apply(context, args); }, delay); } } let trigger=debounce(() => { if ($element.is(":hidden")) { return; } if (($(window).scrollTop() + $(window).height()) > ($element.offset().top + 5)) { if (!$scope.$eval($attrs.zScrollTrigger)) { $element.trigger("click"); } } }, 100); $(window).scroll(trigger); $scope.$on("destroy",()=>{ $(window).unbind("scroll",trigger); }) } } })