CSS @supports媒体的功能@property之类的功能

发布于 2025-02-13 10:04:42 字数 1150 浏览 0 评论 0原文

我正在尝试使用此按钮进行样式,并带有一个梯度边框,在悬停时充满了该梯度,但我想在过渡时做到这一点,否则看起来很糟糕。

我看到并实施了这种方法以制定按钮: (我正在使用样式的组件,所以这就是与道具的交易)

background: linear-gradient(
        to right,
        var(--color1, ${(props) => props.background || "#202020"}),
        var(--color2, ${(props) => props.background || "#202020"})
      )
      padding-box,
    linear-gradient(to right, #0000ff, #ff00ff) border-box;

问题是在我想进行过渡时出现的,因为您无法过渡该梯度。 我尝试了边框的伪元素解决方案,并用不透明的过渡进行过渡,但是背景上的元素并不总是100%重新扎屏屏幕时,看起来很糟糕。

我发现该解决方案在Chrome上非常有效:

  @property --color1 {
syntax: "<color>";
initial-value: ${(props) => props.background || "#202020"};
inherits: false;
  }

  @property --color2 {
    syntax: "<color>";
    initial-value: ${(props) => props.background || "#202020"};
    inherits: false;
  }
:hover {
      --color1: #0000ff;
      --color2: #ff00ff;
  }

在不悬停的Firefox(也许还有其他浏览器)中,它可以正常工作,因为它无法识别-Color1或2的定义,并进入后备。 但是,当我悬停并定义变量时,问题就到了,因此颜色立即弹出并且看起来不好。

我对解决方案的想法是使用@supports媒体查询的东西,但我不知道如何将其与@property这样的功能使用,我找不到任何内容。有人知道如何实施该媒体吗?还是其他解决方案?

另外:其他浏览器的解决方案不需要是梯度过渡,它可以像过滤器一样简单:亮度(x%)

I'm trying to style this button, with a gradient border that fills itself of that gradient when hovering, but I want to do that in a transition, otherwise it looks bad.

I saw and implemented this approach to make the button:
(I'm using styled components so thats the deal with props)

background: linear-gradient(
        to right,
        var(--color1, ${(props) => props.background || "#202020"}),
        var(--color2, ${(props) => props.background || "#202020"})
      )
      padding-box,
    linear-gradient(to right, #0000ff, #ff00ff) border-box;

The problem comes when I want to do the transition, as you can't transition that gradient.
I've tried the pseudo element solution for the border and making the transition with opacity but the element on the background isn't always 100% centered with the one on the front when rezising screen and it looks bad.

I found this solution that works perfectly on chrome:

  @property --color1 {
syntax: "<color>";
initial-value: ${(props) => props.background || "#202020"};
inherits: false;
  }

  @property --color2 {
    syntax: "<color>";
    initial-value: ${(props) => props.background || "#202020"};
    inherits: false;
  }
:hover {
      --color1: #0000ff;
      --color2: #ff00ff;
  }

In firefox (and maybe others browsers) works fine when not hovering, because it doesn't recognize --color1 or 2 as defined and goes to the fallback.
But the problem comes when I hover and define the variables, so the color pops instantly and looks bad.

My idea of a solution is something using the @supports media query but I dont know how to use it with a feature like @property and I could not find anything about it. Does someone know how to implement that media? or any other solution?

Also: the solution for other browsers doesn't need to be the gradient transition, it can be something as simple as a filter: brightness(x%)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

站稳脚跟 2025-02-20 10:04:42

在这种特殊情况下,可以使用混合模式解决您的问题,并且可以使用交叉浏览器来解决。

.box {
  width: 200px;
  height: 100px;
  border: 10px solid #0000;
  background: 
    linear-gradient(to right, #0000ff, #ff00ff) border-box,
    #202020 padding-box;
  background-blend-mode: darken;
  transition: .5s;
  
  color:#fff;
  text-align:center;
  line-height:100px;
  font-size:30px;
  cursor:pointer;
}

.box:hover {
  background-color:#0000;
}
<div class="box"> some text </div>

In this particular case, your issue can be solved using blend-mode and it will work cross browser.

.box {
  width: 200px;
  height: 100px;
  border: 10px solid #0000;
  background: 
    linear-gradient(to right, #0000ff, #ff00ff) border-box,
    #202020 padding-box;
  background-blend-mode: darken;
  transition: .5s;
  
  color:#fff;
  text-align:center;
  line-height:100px;
  font-size:30px;
  cursor:pointer;
}

.box:hover {
  background-color:#0000;
}
<div class="box"> some text </div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文