vendredi 30 janvier 2015

How to add and remove class of one element while hovering over another element via Angular?


I'd like to add a class to an <li> when it's child <a> gets hovered over but I can't seem to get it to work. What is happening is that no matter what <a> element is hovered over, only the first <li> gets affected so I need to be able to target only the parent <li> of the <a> that is currently being hovered over.


Here is what I have so far:


http://ift.tt/1Ht1BXh


and here is the code:


Controller (Javascript)



var myApp = angular.module('myApp',[]);
myApp.controller("Ctrl", function($scope){
$scope.addClass = function(){
angular.element(document.querySelector('.li-hover')).addClass('hovering');
}
$scope.removeClass = function(){
angular.element(document.querySelector('.li-hover')).removeClass('hovering');
}
});


HTML



<div ng-app='myApp'>
<div ng-controller="Ctrl">
<ul>
<li class="li-hover" >
<a href="" ng-mouseover="addClass()" ng-mouseleave="removeClass()">
<span>Home</span>
</a>
</li>
<li class="li-hover" >
<a href="" ng-mouseover="addClass()" ng-mouseleave="removeClass()">
<span>About</span>
</a>
</li>
<li class="li-hover" >
<a href="" ng-mouseover="addClass()" ng-mouseleave="removeClass()">
<span>Contact</span>
</a>
</li>
</ul>
</div>
</div>


CSS



.hovering {
background-color: pink;
border-bottom: 5px solid red;
}
li {
margin: 1em;
padding: 2em;
border-bottom: 5px solid #aaa;
background: #ccc;
}
a {
background: white;
padding: 0.5em;
}




Aucun commentaire:

Enregistrer un commentaire