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(“根”))代码>