react 小例子--轮播

<!--页面容器-->
<div class="index-content" id="my"></div>
<script type="text/babel">
    window.onload = function(){
        // 轮播
        class Lunbo extends React.Component{
			constructor(){
				super();
				this.state={
					num:0,
					imgArray: ['img/banner1.jpg',  
							'img/banner2.jpg',  
							'img/banner3.jpg',  
							'img/banner4.jpg',
							'img/banner5.jpg']
				}
			}
			autoplay(){
				this.state.num++;
				if(this.state.num == this.state.imgArray.length){
					this.state.num = 0;
				}
				this.setState({
					num:this.state.num
				})
			};
			play(){  
			setInterval(()=>{this.autoplay()}, 2000)  
			};
			componentDidMount(){ 
				this.play();
			};
			render(){
				let {num,imgArray} = this.state ;
				var imgList = imgArray.map(function(item,index){
					return <img src={item} key={index} style={{display:(index==num) ?'block':'none'}} />
				})
				var liList = imgArray.map(function(item,index){
					return <li key={index} className={ index==num ? 'selected':''}></li>
				})
		
				return (
					<div className="banner">
						{imgList}
						<div className="banner-circle">
							<ul>
								{liList}
							</ul> 
						</div>
						
					</div>
				)
				
			}
			
		}
        ReactDOM.render(
          <Lunbo />,
          document.getElementById("my")
        );
    }复制代码

转载于:https://juejin.im/post/5c1cf032e51d451be35e30ed

`ResizeObserver loop limit exceeded` 错误是由于 ResizeObserver API 导致的。这个错误的原因是 ResizeObserver API 在连续触发时会出现循环调用,从而导致了这个错误。 解决这个问题的方法是使用 `react-responsive-carousel` 的 `EmblaCarousel` 组件,它不依赖于 ResizeObserver API。可以使用 `react-player` 的 `onPlay` 和 `onPause` 事件来控制播放和暂停视频。 下面的代码演示了如何在 `EmblaCarousel` 中使用 `react-player` 播放视频: ```jsx import React, { useState, useRef } from 'react'; import { EmblaCarousel, EmblaCarouselReact } from 'embla-carousel-react'; import ReactPlayer from 'react-player'; const App = () => { const [embla, setEmbla] = useState(null); const playerRef = useRef(null); const onPlayerPlay = () => { embla && embla.dangerouslyGetEngine().pause(); }; const onPlayerPause = () => { embla && embla.dangerouslyGetEngine().resume(); }; const onSelect = () => { playerRef.current && playerRef.current.seekTo(0); }; return ( <EmblaCarouselReact onInit={setEmbla}> <EmblaCarousel> <div> <ReactPlayer url="https://www.youtube.com/watch?v=dQw4w9WgXcQ" width="100%" height="100%" playing={false} ref={playerRef} onPlay={onPlayerPlay} onPause={onPlayerPause} /> </div> <div> <img src="https://picsum.photos/800/400" alt="Image 2" /> </div> <div> <img src="https://picsum.photos/800/401" alt="Image 3" /> </div> </EmblaCarousel> </EmblaCarouselReact> ); }; export default App; ``` 在这个例子中,我们使用了 `EmblaCarousel` 和 `EmblaCarouselReact` 组件来创建轮播图。我们还使用了 `react-player` 来播放视频,并使用了 `onPlay` 和 `onPause` 事件来控制轮播图的播放和暂停。在 `onSelect` 事件中,我们将视频的播放时间重置为 0,以便在下一次播放它时可以从开头开始。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值