当背景动态变化时,CSS background-clip 属性未在 React 中应用

waldothedeveloper

如果你想进入实时代码,我在这里准备了这个 CodeSandBox:https ://codesandbox.io/s/0j99m3m50

这是我在做什么:

我有带有状态对象的 React 组件,它具有两个带有值的属性(用户、渐变)

this.state = {
    users: "programmer",
    gradient: "linear-gradient(120deg, #f093fb 0%, #f5576c 100%)"
};

我在 h1 JSX 标签上渲染用户,但我应该看到文本被剪切到每个随机渐变背景,但是它不起作用,为什么?

render() {
  const gradually = this.state.gradient;
  const userGradient = {
    background: `${gradually}`,
    WebkitBackgroundClip: "text !important",
    backgroundClip: "text !important",
    color: "transparent"
  };

  return (
    <div>
      <h1 style={userGradient} className="text">
        {this.state.users}
      </h1>
    </div>
  );
}
沙拉娜

你需要key在你的h1标签添加一个唯一的来强制重绘。键可能与您的graduallyvar具有相同的值您还可以删除!important剪辑上的 。

这是对您的沙箱进行工作更改的链接:https : //codesandbox.io/s/css-background-clip-not-being-applied-n34o0

const gradually = this.state.gradient;
const userGradient = {
  background: `${gradually}`,
  WebkitBackgroundClip: "text", // Removed !important
  backgroundClip: "text", // Removed !important
  color: "transparent"
};

return (
  <div>
    <h1
      style={userGradient}
      className="text"
      key={gradually} // Add key
      >
      {this.state.users}
    </h1>
  </div>
);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

background-image属性未在css中显示图像

background-clip 属性使整个 div 消失

动态图像路径未在Angular 7的[style.background-image]属性中显示

CSS background-clip:来自父元素背景的文本

如何在JQuery中编辑'clip'CSS属性?

<clip-path> 未在 VectorDrawable 中呈现

div的id属性未定义/未在react应用中的onClick上捕获

简写背景属性(CSS3)中的background-size

未在React应用中触发onDrop事件

样式未在React Native中应用

未在React应用中显示的组件

Rails应用程序中CSS“ background:url”属性的正确语法是什么?

CSS类选择器样式未在React Project中应用

当background-clip属性设置为content-box时,Chrome渲染部分像素

我可以使用 JS 动态更改 Clip 属性吗?

CSS background-clip content-box,这里的内容是什么形状的?

当我的React应用程序中的路线发生变化时,我clearInterval()和应用程序中断

值属性未在 React Typescript 的 input type="checkbox" 中显示任何内容

动画未在React Native移动应用中显示

Tailwindcss 未在 ExpressJS/React 应用程序中呈现

保证金未在CSS中应用

未在 CSS 中应用 font-weight

如何在React中动态更改CSS属性的值?

背景圖像未在 React 中顯示

css'clip-path'在Firefox中不起作用

在 React 中反映状态变化时避免无限的 useEffect 循环

React中数据变化时如何运行临时效果?

-webkit-background-clip:过渡元素在mozilla中过渡结束后无法正常工作,但在chrome中可以正常工作

当应用程序中的状态发生变化时,React Fullpage TypeError 未定义