angular学习笔记-angular中怎么获取dom元素 - 前端段子手?

angular学习笔记-angular中怎么获取dom元素

步骤分解:

第一步:给要获取的元素一个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]+\'离开\');
}
});
}

posted on 2017-03-26 22:58 前端段子手 阅读(13432) 评论(0) 编辑收藏举报