angular浏览器滚动条滚动到指定element 触发事件

时间:2023-03-09 08:22:53
angular浏览器滚动条滚动到指定element 触发事件
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);
})
}
}
})