react 原生實(shí)現(xiàn)頭像滾動(dòng)播放的示例
本文介紹了react 原生實(shí)現(xiàn)頭像滾動(dòng)播放的示例,分享給大家,具體如下:

之前需要的是下面這個(gè)效果

現(xiàn)在。。。
憋說話,先看看效果。

感覺很簡(jiǎn)單呀,直接渲染,transform就可以了,嗯嗯,是的沒錯(cuò)。
渲染出來,獲取元素,然后添加css,美中不足銜接不是很順暢,不過還是做出來了。
<div className="avatar-transform" ref={this.tmall}>
{!!personAvatar.length && personAvatar.map((item, index) => {
return <div className="avatar-wrap" key={index}>
<div><img src={item.avatar} alt="" /></div>
<div className="avatar-name">{item.nickname.substring(0, 2)}***已參與活動(dòng)</div>
</div>
})}
</div>
changeCss(){
if(this.index === 31){
this.tmall.current.style = `transform: translateY(${this.index * 4}rem);`
this.getAvatar()//獲取數(shù)據(jù)
this.index = 0
return
}
this.tmall.current.style = `transform: translateY(-${this.index * 4}rem);transition: all 1s ease;`
++this.index
setTimeout(()=>{
this.changeCss()
},3000)
}
但是問題還是來了
一次性返回幾百條數(shù)據(jù),什么鬼,難道還直接渲染嗎,我的天,100多個(gè),渲染出來,那酸爽,得了吧,全部渲染出來是不可能的了,這輩子都不可能全部渲染出來的了,看一下有沒什么方法沒有。
網(wǎng)上搜了一下,搜出來的是什么鬼,算了算了,還是得自己寫
思路:想了一下,可不可以,在后面添加一個(gè)節(jié)點(diǎn),然后刪除最前面的節(jié)點(diǎn)
嗯?好像可以
還是原來的配方原來的味道
<div className="avatar-transform" ref={this.tmall}>
{!!personAvatar.length && personAvatar.map((item, index) => {
return <div className="avatar-wrap" key={index}>
<div><img src={item.avatar} alt="" /></div>
<div className="avatar-name">{item.nickname.substring(0, 2)}***已參與活動(dòng)</div>
</div>
})}
</div>
getAvatar(target = false) { //獲取數(shù)據(jù)
http.get('********').json(r => {
if (r.error) {
return Alert(r.msg);
}
//判斷一下,不是第一次請(qǐng)求
if (target) {
this.listData = r.users
this.changeCss()
return
}
//裝起來
this.listData = r.users
this.setState({
personAvatar: this.listData.splice(0, 5) //先來5個(gè)
})
//最開始是在下面的transform: translateY(7rem),所以得上來
this.tmall.current.style = `transform: translateY(0);transition: all 1s ease;`
setTimeout(() => {
this.changeCss()
}, 3000)
});
}
.avatar-transform {
transform: translateY(7rem);
}
changeCss() {
let data = this.listData.shift()
if (!data) {
this.getAvatar(1)
return
}
let div = document.createElement('div')
div.className = "avatar-wrap"
div.innerHTML = `<div><img src='${data.avatar}' alt="" /></div>
<div class="avatar-name">${data.nickname.substring(0, 2)}***已參與活動(dòng)</div>`
this.tmall.current.style = `transform: translateY(-${4}rem);transition: all 1s ease;`
this.tmall.current.appendChild(div) //添加節(jié)點(diǎn)
setTimeout(() => {
this.tmall.current.style = `transform: translateY(0);`
this.tmall.current.removeChild(this.tmall.current.childNodes[0]) //刪除第一個(gè)節(jié)點(diǎn)
}, 1000)
setTimeout(() => {
this.changeCss()
}, 3000)
}
到此這篇關(guān)于react 原生實(shí)現(xiàn)頭像滾動(dòng)播放的示例的文章就介紹到這了,更多相關(guān)react 頭像滾動(dòng)播放內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React?state結(jié)構(gòu)設(shè)計(jì)原則示例詳解
這篇文章主要為大家介紹了React?state結(jié)構(gòu)設(shè)計(jì)原則示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
react?事項(xiàng)懶加載的三種方法及使用場(chǎng)景
這篇文章主要介紹了react?事項(xiàng)懶加載的三種方法及使用場(chǎng)景,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
react中使用ant組件庫(kù)的modal彈窗報(bào)錯(cuò)問題及解決
這篇文章主要介紹了react中使用ant組件庫(kù)的modal彈窗報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
React應(yīng)用框架Dva數(shù)據(jù)流向原理總結(jié)分析
這篇文章主要為大家介紹了React 應(yīng)用框架Dva數(shù)據(jù)流向原理總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
React性能優(yōu)化系列之減少props改變的實(shí)現(xiàn)方法
這篇文章主要介紹了React性能優(yōu)化系列之減少props改變的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
useReducer?createContext代替Redux原理示例解析
這篇文章主要為大家介紹了useReducer?createContext代替Redux原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
React-Native之TextInput組件的設(shè)置以及如何獲取輸入框的內(nèi)容
這篇文章主要介紹了React-Native之TextInput組件的設(shè)置以及如何獲取輸入框的內(nèi)容問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

