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:
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