简体   繁体   English

过渡不适用于边框图像和渐变

[英]Transition doesn't work with border-image and gradient

I am using border-image with gradient and it works fine, but it seems transition is not supported for it. 我使用带gradient border-image ,它工作正常,但它似乎不支持transition

Is it possible to achieve transition on hover for this example? 是否有可能在此示例中实现悬停transition

JsFiddle 的jsfiddle

 div { border:10px solid blue; height:120px; float:left; transition:1s all; border-image: linear-gradient(to bottom, white, blue) 1 100%; } div:hover { border-image: linear-gradient(to bottom, skyblue, blue) 1 100%; } 
 <div></div> 

As the others already told you, it isn't possible to transition a gradient (yet). 正如其他人已经告诉过你的那样,现在还不可能转换渐变。 The best way to fake the effect would be to work with opacity, which can be transitioned. 伪造效果的最佳方法是使用不透明度,可以转换。 You don't need to add any elements however, the :before and :after pseudo elements will do just fine. 你不需要添加任何元素, :before:after伪元素就可以了。 have a look at the following css: 看看下面的css:

div {
    height:120px;
    width:10px;
    padding: 0 10px;
    background: salmon;
    background-clip: content-box;
    position: relative;
}
div:after, div:before {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    content:'';
}
div:after {
    background: linear-gradient(to bottom, white 0%, blue 100%);
    z-index: -1;
    transition: opacity 1s;
}
div:before {
    background: linear-gradient(to bottom, skyblue 0%, blue 100%);
    z-index: -2;
}
div:hover:after {
    opacity: 0;
}

And an example: https://jsfiddle.net/et0ffrqx/2/ 例如: https//jsfiddle.net/et0ffrqx/2/

Not Possible 不可能

That isn't possible yet because linear-gradient is calculated as an image, not actually colors. 这还不可能,因为linear-gradient被计算为图像,而不是实际颜色。

Solution

Try putting the <div> within another <div> which can act as a border. 尝试将<div>放在另一个可以作为边框的<div>中。 Then the outer <div> can have an animated background 然后外部的<div>可以有一个动画背景

I've found this codepen demonstrating how this can be done with JavaScript. 我发现这个codepen演示了如何使用JavaScript完成此操作。

My best bet for you would be to have two <div> stacked on top of each other. 我最好的选择是将两个<div>堆叠在一起。 The bottom <div> would be the target gradient and the top being the start. 底部<div>将是目标渐变,顶部是开始。 Then just fade the top <div> 然后淡出顶部的<div>

 #start { position:absolute; width: 100px; height: 100px; z-index: 1; opacity: 1; background: linear-gradient(red,blue); transition: opacity 0.5s ease; } #end { position:absolute; width: 100px; height: 100px; background: linear-gradient(green,orange); z-index: -1; } #start:hover { opacity: 0; } 
 <div id="start">Start</div> <div id="end">End</div> 

The snippet demonstrates a simple way to fade between gradients. 该片段演示了一种在渐变之间淡入淡出的简单方法。 Not perfect but smoother and without JavaScript. 不完美,但更顺畅,没有JavaScript。 Put your other stuff in side the <div> and adjust the width and height to your needs. 把你的其他东西放在<div>旁边,根据你的需要调整宽度和高度。

Also try using :before and :after to avoid having duplicate div s 同时尝试使用:before:after以避免重复div

No 没有

Animations aren't supported for those properties. 这些属性不支持动画。

You can however, think of another way to accomplish this visually. 但是,您可以想出另一种方法来实现这一目标。

maybe you have 2 wrappers around something, and they are 2 different gradients, and there is padding around them to simulate the look of a border... and then the elements with the gradients have opacity that fades to and from on hover. 也许你有两个包裹物,它们是两个不同的渐变,并且它们周围有填充物以模拟边框的外观......然后具有渐变的元素具有在悬停时淡入淡出的不透明度。

https://jsfiddle.net/sheriffderek/5uoypaoo/ https://jsfiddle.net/sheriffderek/5uoypaoo/

<div class="gradient-1">
    <div class="gradient-2"></div>
    <div class="thing"></div>
</div>


.thing {
    position: relative;
    width: 200px;
    height: 200px;
    background: white;
    float: left;
}

.gradient-1 {
    position: relative;
    background: linear-gradient(45deg, pink, blue);
    opacity: 1;
    padding: 1rem;
    float: left;
}

.gradient-1:hover .gradient-2 {
    opacity: 1;
}

.gradient-2 {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: linear-gradient(45deg, lightgreen, orange);
    opacity: 0;
    transition: opacity 1s ease-in-out;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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