Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React-如何通过ref应用样式_Javascript_Css_Reactjs_Jsx_Ref - Fatal编程技术网

Javascript React-如何通过ref应用样式

Javascript React-如何通过ref应用样式,javascript,css,reactjs,jsx,ref,Javascript,Css,Reactjs,Jsx,Ref,我有一个容器的参考,我想改变他的背景位置。 这需要发生在函数内部,并且值是动态的,所以我不能为此创建类 slideRef.current.style["background-position-x"] = "-262.5px" setTimeout(function(){ slideRef.current.classList.add("spin_animation"); slideRef.current.style = {backgroundPositionX: "-" + scrollA

我有一个容器的参考,我想改变他的背景位置。 这需要发生在函数内部,并且值是动态的,所以我不能为此创建类

slideRef.current.style["background-position-x"] = "-262.5px"
setTimeout(function(){
  slideRef.current.classList.add("spin_animation");
  slideRef.current.style = {backgroundPositionX: "-" + scrollAmount + "px"}
 10);
我尝试了两种方法,一种是访问属性,而不是使用驼峰大小写,另一种是将样式作为对象传递,就像内联样式一样

如何应用背景位置,直接访问ref

elementRef.current.style.backgroundPositionX = "-262.5px";
const-App=()=>{
const elementRef=React.useRef(null);
常量处理程序=()=>{
if(元素参考电流){
elementRef.current.style.color=“红色”;
elementRef.current.style.backgroundPositionX=“-262.5px”;
log(elementRef.current.style);
}
};
返回(
示例文本
单击“我”更改文本的样式
);
}
render(,document.getElementById(“根”))