简体   繁体   English

使用渐变使边框图像工作

[英]Making border-image work with gradients

I'm working on a webapp that uses react.js and sass for styles (so all my style files are .scss).我正在开发一个使用 react.js 和 sass 作为样式的 web 应用程序(所以我所有的样式文件都是 .scss)。 I have a textbox with the current style:我有一个当前样式的文本框:

input[type=text] {
  text-align: center;
  font: inherit;
  border: 6px solid #999999;
  padding: 5px 5px;
  font-size: 15px;
  box-shadow: 0 1px 1px #DDD;
  width: 223px;
  outline: none;
  display: block;
  color: #7B8585;
  margin: 0 auto 20px;
}

At some point, my app wants to change the border colour.在某些时候,我的应用程序想要更改边框颜色。 This is what I have for that:这就是我所拥有的:

var borderStyle;
if (gradient) {
  borderStyle = {
    'borderImage': '-webkit-linear-gradient(left, #0083c5 0%, #0083c5 33%, #ec4a26 66%, #ec4a26 100%)',
  };
}

Later, the input component:后来,输入组件:

<input type="text" style={borderStyle} onChange={this.handleChange} />

Currently what I see is a white border with a tiny image of the red-blue gradient in each corner of the border.目前我看到的是一个白色边框,边框的每个角都有一个红蓝渐变的小图像。 I've tried using borderColor , which doesn't work with gradients at all, apparently.我试过使用borderColor ,显然它根本不适用于渐变。 Am I missing something obvious, or is it not possible to do a simple border gradient?我是否遗漏了一些明显的东西,或者不可能做一个简单的边框渐变?

The desired result is a left-to-right gradient (so the left border is entirely blue, the right is entirely red, and the top and bottom borders feature the blue-to-red transition).所需的结果是从左到右的渐变(因此左边框完全为蓝色,右侧完全为红色,顶部和底部边框具有蓝色到红色的过渡)。


In response to Harry's answer, I changed to the following code:针对Harry的回答,我改成如下代码:

 if (gradient) { borderStyle = { borderImage: 'linear-gradient(to right, #0083c5 0%, #0083c5 33%, #ec4a26 66%, #ec4a26 100%)', borderImageSlice: 1 }; }

as specified in the react docs for inline styles .内联样式反应文档中所述。 However when I inspect the element, the borderImageSlice property I've defined is missing;但是,当我检查元素时,我定义的borderImageSlice属性丢失了; only the borderImage one is there, and I still only have tiny gradients in the corners of the border.只有borderImage一个在那里,我仍然在边框的角落里只有很小的渐变。

You need to add a border-image-slice property also while applying the border. 在应用边框时,还需要添加border-image-slice属性。 Doing this would give the exact output as you need. 这样做可以根据需要提供准确的输出。

I have added it via CSS itself in the below snippet (without the JS) but you should be able to adapt it :) 我已经通过CSS本身在下面的代码片段中添加了它(没有JS),但你应该能够适应它:)

 input[type=text] { text-align: center; font: inherit; border: 6px solid #999999; padding: 5px 5px; font-size: 15px; box-shadow: 0 1px 1px #DDD; width: 223px; outline: none; display: block; color: #7B8585; margin: 0 auto 20px; border-image: linear-gradient(to right, #0083c5 0%, #0083c5 33%, #ec4a26 66%, #ec4a26 100%); border-image-slice: 1; } 
 <input type="text" /> 

Note: I have also modified the gradient syntax to use the standard one so that it works in all browsers that support border-image property. 注意:我还修改了渐变语法以使用标准语法,以便它适用于所有支持border-image属性的浏览器。


Below is a snippet which applies the border image when the text in the input box is changed. 下面是一个片段,当输入框中的文本发生更改时,该片段应用边框图像。

 var ip = document.getElementById("inp"); ip.addEventListener("change", function() { this.style.borderImage = 'linear-gradient(to right, #0083c5 0%, #0083c5 33%, #ec4a26 66%, #ec4a26 100%)'; this.style.borderImageSlice = '1'; }); 
 input[type=text] { text-align: center; font: inherit; border: 6px solid #999999; padding: 5px 5px; font-size: 15px; box-shadow: 0 1px 1px #DDD; width: 223px; outline: none; display: block; color: #7B8585; margin: 0 auto 20px; } 
 <input type="text" id="inp" /> 


It seems like ReactJS by default adds px as units to all numbers that are passed for inline styles and because of this the border-image-slice: 1 is wrongly getting set as border-image-slice: 1px . 似乎ReactJS默认情况下将px作为单位添加到为内联样式传递的所有数字中,因此border-image-slice: 1被错误地设置为border-image-slice: 1px As this property is a unitless property in CSS, it is not getting applied properly. 由于此属性是CSS中的无单元属性,因此未正确应用。 The solution is to wrap this value within quotes and also add a semi-colon within the quotes (like in the below code sample): 解决方案是将此值包装在引号内,并在引号内添加分号(如下面的代码示例所示):

var borderStyle = {
    borderImage: 'linear-gradient(to right, #0083c5 0%, #0083c5 33%, #ec4a26 66%, #ec4a26 100%)',
      borderImageSlice: '1;' // note the quotes and the semi-colon.
  };

Big credits for finding out this problem goes to Henrik Andersson . 找出这个问题的大功劳归功于Henrik Andersson

JSBin Demo with ReactJS 使用ReactJS进行JSBin演示

I managed to fix such problem by adding 1 / 1 / 0 stretch by myself to the inline-style so it looks like this:我设法通过向内联样式添加1 / 1 / 0 stretch来解决这个问题,所以它看起来像这样:

var borderImage = `linear-gradient(to right, #1A80AC 0%, #1A80AC ${position.x / 3}%,
#8798AD ${ position.x / 3 }%, #8798AD 100%) 1 / 1 / 0 stretch`

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

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