<div scroll-event>
...
</div>
//这个div要设置高度和可以滚动
app= angular.module("app", ["aaa", "bbb", "ccc"]);
app.controller('aController', ['$scope', '$http']);
app.directive('scrollEvent',function(){
var obj = {
restrict: 'A',//A匹配属性 E匹配标签 C匹配class M匹配注释
link: function($scope, element, attr){
$scope.$watch('data1',function(newVal,o,s){
if(newVal){
}
})
//获取当前滚动到的section的index
function getSectionIndex(scrollTop){
var sections = $("section[id^='menu']");
var sectionIndex = -1;
if(scrollTop<=headerHeight){
return sectionIndex;
}
for(var i = sections.length;i>=0;i--){
if(headerHeight + getHeight(i) < scrollTop +10){
sectionIndex = i;
break;
}
}
return sectionIndex;
}
//菜单点击事件---先解绑scroll事件,再绑定scroll事件
$scope.openClick = function(index,menus,autoScroll){
element.unbind('scroll');
$scope.selectMenu(index);
if(!autoScroll){
$('#tabbar2').animate({
'scrollTop': headerHeight + getHeight(index) + "px"
}, 50);
}
setTimeout(function(){
element.bind('scroll',scrollEvent);
},300);
}
//给元素绑定滚动事件
element.bind('scroll',scrollEvent);
function scrollEvent(e){
if($scope.showTabbar2){
var scrollTop = e.currentTarget.scrollTop;
var index = getSectionIndex(scrollTop);
if(index >= 0){
$scope.openClick(index,$scope.menus,true);
$scope.$apply();
menuPosition(index);
}else{
$("#menu").removeClass("pox");
}
}
}
}
};
return obj;
})