angularjs - Angular JS conditional logic to check "class name" exists -


below angular js code works fine on mouseover & mouseout. need regard adding conditional logic on js code.

if class name "active" exists, img src path have in "overimg" if user mouseover & mouseout. but, present behaviour removes overimg once user mouseout element. active state have different rest of navigation element.

angularjs:

.directive('elehoveraction', function() { return {     link: function (scope, elem, attrs) {         var imgobj = $(elem).find('img');         var upimg = attrs.eleupimgsrc;         var overimg = attrs.eleoverimgsrc;          elem.bind('mouseover', function () {             $(imgobj).attr("src", overimg);             scope.$apply();         });         elem.bind('mouseout', function() {             $(imgobj).attr("src", upimg);             scope.$apply();         });     }   };                 }); 

html:

 <li class="menu-item menu-item--category active" ele-hover-action ele-up-img-src="images/test1.png" ele-over-img-src="images/test1-over.png">    <a href="#"><img src="images/test1.png" oversrc="images/test1-over.png"  alt=""/><span>test1</span></a> </li> <li class="menu-item menu-item--category" ele-hover-action ele-up-img-src="images/test2.png" ele-over-img-src="images/test2-over.png">   <a href="#"><img src="images/test2.png" oversrc="images/test2-over.png"  alt=""/><span>test2</span></a> </li> 

the obvious method add if statement "mouseout" handler checks if element hasclass active:

.directive('elehoveraction', function() { return {     link: function (scope, elem, attrs) {         var imgobj = elem.find('img');         var upimg = attrs.eleupimgsrc;         var overimg = attrs.eleoverimgsrc;          elem.bind('mouseover', function () {             imgobj.attr("src", overimg);             scope.$apply();         });         elem.bind('mouseout', function() {             if (!elem.hasclass("active")) {                 imgobj.attr("src", upimg);             }             scope.$apply();         });          if (elem.hasclass("active")) {             imgobj.attr("src", overimg);         } else {             imgobj.attr("src", upimg);         }     }   };                 }); 

i went ahead , set src attribute of image based on directive attributes. take part out if don't want it. also, wrapping elem in jquery call redundant because angular elements wrapped in either jquery (if available when angular loads) or own jqlite. otherwise, wouldn't able call elem.bind.

try in fiddle.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -