react實現(xiàn)無限循環(huán)滾動信息
本文實例為大家分享了react實現(xiàn)無限循環(huán)滾動信息的具體代碼,供大家參考,具體內(nèi)容如下
需求
后端傳遞過來的數(shù)據(jù)滾動顯示,鼠標(biāo)移入后停止?jié)L動,鼠標(biāo)移出后繼續(xù)滾動,參考公司門戶的公告信息欄

實現(xiàn)思路
思路一
在componentDidMount中定義一個定時器,每過1000ms觸發(fā)一次事件,將數(shù)組中第一條數(shù)據(jù)push到數(shù)組中,再刪除掉第一條數(shù)據(jù),最后給div添加onMouEnter和onMouseLeave事件,讓鼠標(biāo)移入時清除定時器,鼠標(biāo)移出時重新開啟定時器。
代碼:
class Roll extends React.Component{
state = {
list: [
{ title: '靜夜思' },
{ title: '唐-李白' },
{ title: '窗前明月光' },
{ title: '疑是地上霜' },
{ title: '舉頭望明月' },
{ title: '低頭思故鄉(xiāng)' },
]
}
componentWillMount = () => {
this.begin()
}
roll = () => {
let arr = this.state.list;
arr.push(this.state.list[0])
arr.splice(0,1)
this.setState({
list: arr,
})
console.log(this.state.list);
}
begin = () => {
this.timer = setInterval(() => {
this.roll()
}, 1000);
}
stop = () => {
clearInterval(this.timer)
}
render () {
return (
<div onMouseEnter={this.stop} onMouseLeave={this.begin} className='box'>
{this.state.list.map(item => {
return (
<p>
{item.title}
</p>
)
})}
</div>
)
}
}
效果圖:

可以看到實現(xiàn)出來的效果并不好,沒有往上偏移的效果,所以有了思路二。
思路二
在思路一的基礎(chǔ)上進行修改,在componentDidMount中定義定時器,每次向上偏移幾個px,當(dāng)偏移到一定距離后,將數(shù)組中第一條數(shù)據(jù)push到數(shù)組中,再刪除掉第一條數(shù)據(jù),最后給div添加onMouEnter和onMouseLeave事件。
js文件
class Roll extends React.Component{
state = {
list: [
{ title: '這是消息1' },
{ title: '這是消息2' },
{ title: '這是消息3' },
{ title: '這是消息4' },
{ title: '這是消息5' },
{ title: '這是消息6' },
{ title: '這是消息7' },
{ title: '這是消息8' },
{ title: '這是消息9' },
],
count: 0,
}
// 頁面掛載時開啟定時器
componentDidMount = () => {
this.begin()
}
// 定時器
begin = () => {
this.timer = setInterval(() => {
this.Roll()
}, 10);
}
// 關(guān)閉定時器
stop = () => {
clearInterval(this.timer)
}
// 每次向上偏移0.5px,使用state儲存偏移的次數(shù)
Roll = () => {
this.setState({
count: this.state.count+1
})
this.refs.roll.style.top = -0.5*this.state.count+'px';
// 當(dāng)偏移量達到40px時,將數(shù)組中第一個數(shù)據(jù)剪切到數(shù)組的最后,再減去一行高度對應(yīng)的偏移次數(shù)
if(-0.5*this.state.count <= -40){
let arr = this.state.list;
arr.push(this.state.list[0])
arr.splice(0,1);
this.setState({
list: arr,
count: this.state.count - 50,
})
this.refs.roll.style.top = (this.state.count*(-0.5)) + 'px'
}
}
render(){
return (
<div className="box" onMouseEnter={this.stop} onMouseLeave={this.begin}>
<div className="content" ref='roll'>
{this.state.list.map((item)=>{
return (
<p className='row'>
<a href="#" rel="external nofollow" >
{item.title}
</a>
</p>
)
})}
</div>
</div>
)
}
}
css文件
.box{
width: 300px;
height: 160px;
border: 1px solid black;
margin: 200px 300px;
position: relative;
overflow: hidden;
}
.content{
position: absolute;
top: 0px;
}
.row{
height: 20px;
margin: 5px auto;
}
效果圖:

獲取節(jié)點
1.document獲取節(jié)點
之前是真的沒想到react里也能使用document獲取元素節(jié)點,和js里一樣的用法
2.refs獲取
通過this.refs.xxx獲取
componentDidMount = () => {
console.log(this.refs.test);
}
render () {
return (
<div ref='test'>
123
</div>
)
}
3.findDOMNode獲取
通過ReactDom.findDOMNode(this)來獲取
this為當(dāng)前組件的實例
componentDidMount = () => {
console.log(ReactDom.findDOMNode(this));
}
render () {
return (
<div className='test'>
123
</div>
)
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React競態(tài)條件Race Condition實例詳解
這篇文章主要為大家介紹了React競態(tài)條件Race Condition實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
解決antd的Table組件使用rowSelection屬性實現(xiàn)多選時遇到的bug
這篇文章主要介紹了解決antd的Table組件使用rowSelection屬性實現(xiàn)多選時遇到的bug問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
react-router-dom v6版本實現(xiàn)Tabs路由緩存切換功能
今天有人問我怎么實現(xiàn)React-Router-dom類似標(biāo)簽頁緩存,很久以前用的是react-router v5那個比較容易實現(xiàn),v6變化挺大,但了解react的機制和react-router的機制就容易了,本文介紹react-router-dom v6版本實現(xiàn)Tabs路由緩存切換,感興趣的朋友一起看看吧2023-10-10
React styled-components設(shè)置組件屬性的方法
這篇文章主要介紹了styled-components設(shè)置組件屬性的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
淺談React Native Flexbox布局(小結(jié))
這篇文章主要介紹了淺談React Native Flexbox布局(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01

