【问题标题】:CSS button transition linear-gradient background into transparent backgroundCSS按钮将线性渐变背景转换为透明背景
【发布时间】:2019-02-24 19:41:57
【问题描述】:

我有一个带有线性渐变背景、橙色边框和一些文本的按钮。当我将鼠标悬停在按钮上时,我希望背景变为透明而不更改按钮的其他属性。

我尝试将不透明度转换为 0,但显然,这将隐藏边框和文本。我也尝试过转换背景,但它不起作用,因为我没有要转换的端点,因为它需要透明。

body {
    background-color: darkblue;
}

.button {
    background-image: linear-gradient(red,yellow);
    border: solid orange 2px;
    border-radius: 10px;
    color: white;
    padding: 15px 25px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 24px;
}
<button class="button">Submit</button>

【问题讨论】:

    标签: html css linear-gradients


    【解决方案1】:

    使用伪元素作为背景,您可以轻松做到这一点:

    body {
      background-color: darkblue;
    }
    
    .button {
      border: solid orange 2px;
      border-radius: 10px;
      color: white;
      padding: 15px 25px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 24px;
      position: relative;
      overflow: hidden;
      z-index:0;
      background:transparent;
    }
    
    .button::before {
      content: "";
      position: absolute;
      z-index:-1;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-image: linear-gradient(red, yellow);
      transition:1s;
    }
    .button:hover::before {
      opacity:0;
    }
    <button class="button">Submit</button>

    这是另一个不使用伪元素的想法,您可以依靠更改background-colorbackground-size。诀窍是让其中一种渐变颜色保持透明,这样我们就可以看到background-color(您可以将这种渐变颜色转换为透明)。然后你增加background-size来隐藏底部颜色,我们只看到透明的。

    body {
      background-color: darkblue;
    }
    
    .button {
      border: solid orange 2px;
      background-image: linear-gradient(transparent, yellow);
      background-size:100% 100%;
      background-color:red;
      border-radius: 10px;
      color: white;
      padding: 15px 25px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 24px;
      transition:1s;
    }
    
    .button:hover {
      background-color:transparent;
      background-size:100% 500%;
    }
    <button class="button">Submit</button>

    或考虑调整background-size 以进行另一种过渡:

    body {
      background-color: darkblue;
    }
    
    .button {
      border: solid orange 2px;
      background: 
        linear-gradient(red, yellow),
        transparent;
      background-size:100% 100%;
      background-position:left; /*change this to change the way the transtion happen*/
      background-repeat:no-repeat;
      border-radius: 10px;
      color: white;
      padding: 15px 25px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 24px;
      transition:1s;
    }
    
    .button:hover {
      background-size:0% 100%;
    }
    <button class="button">Submit</button>

    【讨论】:

    • 最后一个过渡正是我在弄清楚如何过渡到透明背景后要实现的,非常感谢!
    【解决方案2】:

    在启用了实验性网络平台功能 (see) 的 Chrome 中,我们可以使用:

    CSS.registerProperty({
      name: '--alpha', 
      syntax: '<number>', 
      initialValue: 1, 
      inherits: true,
    })
    body {
      background-color: darkblue;
    }
    
    .button {
      --alpha: 1;
      background: linear-gradient(rgba(255,0,0,var(--alpha)), rgba(255,255,0,var(--alpha))) transparent;
      border: solid orange 2px;
      border-radius: 10px;
      color: white;
      padding: 15px 25px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 24px;
      transition: --alpha 1s linear;
    }
    
    .button:hover {
      --alpha: 0;
    }
    &lt;button class="button"&gt;Submit&lt;/button&gt;

    【讨论】:

      【解决方案3】:

      现在我们可以使用新的@property。请参阅support table

      @property --alpha {
        syntax: '<number>';
        inherits: false;
        initial-value: 1;
      }
      
      body {
        background-color: darkblue;
      }
      
      .button {
        background-image: linear-gradient(rgba(255, 0, 0, var(--alpha)), rgba(255, 255, 0, var(--alpha)));
        transition: --alpha 1s;
        background-color: transparent;
        border: solid orange 2px;
        border-radius: 10px;
        color: white;
        padding: 15px 25px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 24px;
      }
      
      .button:hover {
        --alpha: 0;
      }
      &lt;button class="button"&gt;Submit&lt;/button&gt;

      【讨论】:

      • clearInterval 丢失。以及为什么要使用 2 个具有相同值的变量?
      • @Qwertiy 更新了新方法。
      【解决方案4】:

      这可能会有所帮助:

      body {
          background-color: darkblue;
      }
      
      .button {
          background-image: linear-gradient(red,yellow);
          border: solid orange 2px;
          border-radius: 10px;
          color: white;
          padding: 15px 25px;
          text-align: center;
          text-decoration: none;
          display: inline-block;
          font-size: 24px;
          transition: 2s;
          z-index: 1000;
          
      }
      button:hover{
      background:linear-gradient(transparent, transparent);
      }
      &lt;button class="button"&gt;Submit&lt;/button&gt;
      所以,我只是将悬停设置为透明的线性渐变,因为你没有固定的颜色。

      【讨论】:

      • 您的示例中没有过渡
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多