달성하려는 목표는 전환 애니메이션을 사용하여 불투명도를 변경하여 가시적 인 요소를 변경하는 부드러운 효과를 얻는 것입니다.전환으로 구성 요소 소품 변경
구성 요소는 슬라이더입니다. 다음 슬라이드의 슬라이드 불투명도 변경은 현재 슬라이드의 1로 변경되고 0으로 변경됩니다. 슬라이드 구성 요소의
내 첫 번째 버전
export class Slide extends React.PureComponent {
render() {
let { img, backgroundColor, active, ...props } = this.props;
const Wrapper = styled.div`
background-image: url(${img});
background-color: ${backgroundColor};
background-size: cover;
background-position: 50% 50%;
opacity: ${active ? 1 : 0};
transition: opacity 1s;
`;
return (
<Wrapper/>
)
}
}
내 첫번째 생각은 상태에서 현재 슬라이드 인덱스를 저장하는 것이 었습니다. 그래서 내 Slides 컨테이너는 현재 슬라이드가 무엇인지 알며 활성 속성을 true로 변경합니다. 그러나이 슬라이드를 다시 렌더링하면 효과가 나타나며 전환은 표시되지 않습니다. 첫 번째 질문 : 왜?
나는 반응하는 dom으로 노는 것을 끝내었고 나는 현재 요소의 불투명도를 수동으로 바꾼다.
export default class Slider extends Component {
__slidesCount;
__current;
__refs = [];
constructor(props) {
super(props);
this.__current = 0;
this.__slidesCount = this.props.children.length
}
componentWillMount() {
// Save number of slides
}
nextSlide =() => {
let next = this.__current < this.__slidesCount - 1 ? this.__current + 1 : 0,
nextSlide = this.__refs[next],
currentSlide = this.__refs[this.__current];
ReactDOM.findDOMNode(nextSlide).style['opacity'] = 1;
ReactDOM.findDOMNode(currentSlide).style['opacity'] = 0;
this.__current = next;
setTimeout(this.nextSlide, 3000)
};
componentDidMount() {
// set timeout to run it after component mounting is finished
setTimeout(() => {
ReactDOM.findDOMNode(this.__refs[0]).style['opacity'] = 1;
}, 0);
// call function changing slide to next
setTimeout(this.nextSlide, 3000)
}
render() {
let { children, ...props } = this.props;
let slides = children.map((slide, index) => {
return React.cloneElement(slide, { ref: ref => this.__refs.push(ref) })
});
return (
<SlidesWrapper {...props}>
{slides}
</SlidesWrapper>
)
}
}
이 솔루션에 대해 어떻게 생각하십니까? 더 쉽게 할 수있는 방법이 있습니까? 물론 jQuery를 사용하지 않고 외부 CSS 사용을 최소화하고 싶습니다.
ReactCSSTransitionGroup을 사용해 보셨나요? – Vlado
'ReactCSSTransitionGroup'에 대해 읽은 것은 새로운 요소 나 마운트 해제 된 요소에 사용된다는 것입니다. 'ReactCSSTransitionGroup은 ReactTransitionGroup을 기반으로하는 고급 API이며 React 구성 요소가 DOM에 들어가거나 떠날 때 CSS 전환 및 애니메이션을 수행하는 쉬운 방법입니다. https://facebook.github.io/react/docs/ animation.html – Darkowic