简体   繁体   中英

Angularjs Override ng-show ng-hide on :hover

I have a series of "icons" that I show in my template.

<div ng-repeat="data in items | orderBy:'-timestamp'">
    <div class="icon">
        <i>1</i>
        <span>2</span>
    </div>
</div>

I have the following css to show span when .icon is hovered over and hide i .

.icon:hover i { display: none; }
.icon:hover span { display: block; }

However, I also want to be able to show every single instance of span when $scope.options == true . So I added the following:

<i ng-hide="options">1</i>
<span ng-show="options">2</span>

But now, my :hover is broken and doesn't end up showing the span .

Is there a way to override the ng-show so that my css will still display:block when it is hovered?

plunker

You can skip the css and let angular handle it using ng-mouseenter/ng-mouseleave. Then use an or to have it show when a second variable goes true.

HTML:

<div ng-repeat="data in items | orderBy:'-timestamp'">
    <div ng-mouseenter="options=true" ng-mouseleave="options=false" class="icon">
        <i ng-hide="options || checkbox">1</i>
        <span ng-show="options || checkbox">2</span>
    </div>

</div>
<input type='checkbox' ng-model="checkbox" ng-click="options=!options">Show

use the $scope.options value to add a class to your .icon div, then make a more specific CSS rule to overrride the :hover event.

<div class="icon" ng-class="{ override: $scope.options == true }">
  <i ng-hide="options">1</i>
  <span ng-show="options">2</span>
</div>

And in your CSS:

.icon.override:hover i { display: block; }
.icon.override:hover span { display: block; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM