步驟分解:
第一步:給要獲取的元素一個ng-model變量,並且綁定事件啦!
1 <div class="home" ng-model="dirName" ng-mouseenter="switchImage($event,dirName)"></div> //給要獲取的元素一個ng-model變量
第二步:在controller中利用$event.target獲取dom元素即可!
1 $scope.switchImage = function($event, value) { 2 3 $($event.target).on("mouseenter mouseleave",function(e) { 4 var w = $(this).width(); // 得到盒子寬度 5 var h = $(this).height();// 得到盒子高度 6 var x = (e.pageX - this.offsetLeft - (w / 2)) * (w > h ? (h / w) : 1); 7 // 獲取x值 8 var y = (e.pageY - this.offsetTop - (h / 2)) * (h > w ? (w / h) : 1); 9 // 獲取y值 10 var direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4;
//direction的值為“0,1,2,3”分別對應着“上,右,下,左” 11 // 將點的坐標對應的弧度值換算成角度度數值 12 var dirName = new Array('上方','右側','下方','左側'); 13 if(e.type == 'mouseenter' && direction == 1){ 14 $(this).find('.profil-photo').html(dirName[direction]+'離開');
}else{
$(this).find('.profil-photo').html(dirName[direction]+'離開');
}
});
}