Changing Element Colour on Hover AngularJS

后端 未结 6 730
失恋的感觉
失恋的感觉 2021-02-07 12:50

So, I\'m just getting started with angularjs and I\'m already confused. I want to change the colour of a list element that corresponds to a hex code colour that is in an array.

相关标签:
6条回答
  • 2021-02-07 13:18

    If you want to hack stay in the view:

    <div ng-repeat="n in [1, 2, 3]" ng-style="{ 'background': (isHover ? '#ccc' : 'transparent') }" ng-mouseenter="isHover = true;" ng-mouseleave="isHover = false;">
     <span>{{ n }}</span>
    </div>
    
    0 讨论(0)
  • 2021-02-07 13:28

    In Angular, there is not ng-hover directive, so you should use ng-mouseenter & ng-mouseleave to simulate it.

    <ul id="personList">
        <li class="bigBox no_s" ng-style="personColour" 
            ng-repeat="i in persons" ng-mouseenter="changeColor($index)" 
            ng-mouseleave="recoverColor($index)">
                <a href="#/{{i.person_id}}">{{i.person_name}}</a>
        </li>
    </ul>
    

    And you should use $index to get your element in persons Array

    $scope.changeColor = function() {
        $scope.personColour = { 'color': '#' + $scope.persons[$index].color };
                               // or 'background-color' whatever you what
    }
    
    $scope.recoverColor = function() {
        $scope.personColour = {};
    }
    
    0 讨论(0)
  • 2021-02-07 13:30

    There is no ng-hover directive. You'll want to use ng-mouseenter and ng-mouseleave.

    Also, keep in mind that the syntax for ng-style is an object corresponding the CSS key-value pairs.

    <li ng-repeat="i in persons" ng-style="personColour" ng-mouseenter="changeColor(i)"></li>
    

    $scope.changeColor = function(person) {
        $scope.personColour = {color: '#'+person.colour};
    };
    

    If you'd like for the color to change back to what it was before you hovered, you can create two functions, or pass a parameter to $scope.changeColour:

    <li ng-repeat="i in persons" ng-style="personColour" ng-mouseenter="changeColor(i,true)" ng-mouseleave="changeColor(i,false)"></li>
    

    $scope.changeColor = function(person, bool) {
        if(bool === true) {
            $scope.personColour = {color: '#'+person.colour};
        } else if (bool === false) {
            $scope.personColour = {color: 'white'}; //or, whatever the original color is
        }
    };
    

    To take it a step further

    You could create a directive for each person.

    <person ng-repeat="i in persons"></person>
    

    // your module, then...
    .directive('person', [function() {
        return {
            restrict: 'E',
            replace: true,
            template: '<li class="bigBox no_s"><a href="#/{{i.person_id}}">{{i.person_name}}</a></li>',
            link: function(scope, elm, attrs) {
                elm
                    .on('mouseenter',function() {
                        elm.css('color','#'+i.colour);
                    })
                    .on('mouseleave',function() {
                        elm.css('color','white');
                    });
            }
        };
    }]);
    
    0 讨论(0)
  • 2021-02-07 13:30

    See Plunker Demo Here

    Use ng-style to conditionally apply CSS styles - I've chosen to name this style 'personStyle'. Next, bind the ng-mouseover event to set the personStyle background-color to the person's colour attribute. Finally, bind the ng-mouseleave event to reset the personStyle when the mouse leaves the element. The changeColor() function is not needed for this solution to work.

    <div id="personContainer" ng-controller="personController">
      <ul id="personList"> 
        <li class="bigBox no_s" ng-repeat="i in persons" ng-style="personStyle">
          <a href="#/{{i.person_id}}" ng-mouseleave="personStyle={}" 
                 ng-mouseover="personStyle={ 'background-color':'#' + i.colour}">
                 {{i.person_name}}</a>
        </li>
       </ul>
    </div>
    
    0 讨论(0)
  • 2021-02-07 13:33

    in the code bellow i add easy code to understand how to active style with condition. I hope that help you

    <li ng-style="( (isOver == 'true') && (linkToActive == 'm1')  ) ? { 'background-color': '#00bdcb' } : {'background-color': '#ededed'}"
    ng-mouseenter="vm.changeColorMenu('m1','true')" ng-mouseleave="vm.changeColorMenu('m1','false')">
    </li>
    
    <li ng-style="( (isOver == 'true') && (linkToActive == 'm2')  ) ? { 'background-color': '#00bdcb' } : {'background-color': '#ededed'}"
    ng-mouseenter="vm.changeColorMenu('m2','true')" ng-mouseleave="vm.changeColorMenu('m2','false')">
    </li>
    
    </ul>
    

    Javascript Code

    function changeColorMenu(indexMenu,bool)
        {
            $scope.isOver = bool;
            $scope.linkToActive = indexMenu;
        }
    
    0 讨论(0)
  • 2021-02-07 13:41

    If you check an example here you will see that ng-style directive waits for css style, not just value, so in your case it'll be:

    $scope.person.colourStyle={'background-color':$scope.persons.color}
    

    and in html it'll be:

    <li class="bigBox no_s" ng-style="i.colourStyle"  ng-repeat="i in persons" ng-hover="changeColor()">< href="#/{{i.person_id}}">{{i.person_name}}</a></li>
    

    edit:

    And You also need to set colour value to full hex for example: '#cc0000'.

    0 讨论(0)
提交回复
热议问题