AngularJS Material design - md-colors on button ho

2020-03-28 03:30发布

Angular Material is awesome for creating colour-themed sites. As well is the usual palettes, you can create dynamic themes without SCSS, using directives like...

md-colors="{background:'primary'}"

...to set the theme colour of any element. With a few lines of code like this, you can easily change the colour of your whole site, or set it dynamically from data:

.config(function($mdThemingProvider) {
    var color = 'pink';
    $mdThemingProvider.theme('dark')
      .primaryPalette(color)
      .dark()
      ;

My site has a few custom elements which require hover states, and the ideal thing would be to have these light up in the accent colour or primary colour. But is there a way to apply md-colors or similar to a hover state?

My conventional hover states look like this in CSS, of course :

.post a:hover{
  background-color:#f00;
}

Here is a codepen set up for you to play with : https://codepen.io/anon/pen/PJRzOQ

1条回答
Animai°情兽
2楼-- · 2020-03-28 04:11

In case anyone is desperate to use md-colors as a hover state, its not possible according to the docs. But I created a work-around by setting the background color with md-colors, and hiding it behind another element. The hover state then makes the top layer transparent, so it appears to be a dynamically set hover state!

You can see an example here:

https://codepen.io/anon/pen/oGqPeE

the html layout looks like this :

<ul ng-if="link" md-theme="myTheme">
<li ng-repeat="linkObj in link" md-colors="{background:'primary'}">
  <a ng-href="{{linkObj.link_url}}" target="_blank" md-colors="{background:'accent'}">          <span ng-bind="linkObj.link_title" md-colors="{color:'accent'}"></span>
  </a>
</li>
</ul>
查看更多
登录 后发表回答