Use CSS3 transitions with gradient backgrounds

后端 未结 16 1581
慢半拍i
慢半拍i 2020-11-21 22:18

I\'m trying to transition on hover with css over a thumbnail so that on hover, the background gradient fades in. The transition isn\'t working, but if I simply change it to

16条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 23:20

    Found a nice hack on codepen that modifies the opacity property but achieves that fade from one gradient to another by leveraging pseudo-elements. What he does is he sets an :after so that when you change the opacity of the actual element, the :after element shows up so it looks as if it were a fade. Thought it'd be useful to share.

    Original codepen: http://codepen.io/sashtown/pen/DfdHh

    .button {
      display: inline-block;
      margin-top: 10%;
      padding: 1em 2em;
      font-size: 2em;
      color: #fff;
      font-family: arial, sans-serif;
      text-decoration: none;
      border-radius: 0.3em;
      position: relative;
      background-color: #ccc;
      background-image: linear-gradient(to top, #6d8aa0, #8ba2b4);
      -webkit-backface-visibility: hidden;
      z-index: 1;
    }
    .button:after {
      position: absolute;
      content: '';
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 0.3em;
      background-image: linear-gradient(to top, #ca5f5e, #d68584);
      transition: opacity 0.5s ease-out;
      z-index: 2;
      opacity: 0;
    }
    .button:hover:after {
      opacity: 1;
    }
    .button span {
      position: relative;
      z-index: 3;
    }
    body {
      text-align: center;
      background: #ffffd;
    }
    BUTTON

提交回复
热议问题