手把手教你用JS實(shí)現(xiàn)回車評論功能
首先我們來制作b站的回車框
HTML部分
<div class="wrapper"> <i class="avatar"></i> <textarea id="tx" placeholder="發(fā)一條友善的評論" rows="2" maxlength="200"></textarea> <button>發(fā)布</button> </div> <div class="wrapper"> <span class="total">0/200字</span> </div> <div class="list"> <div class="item" style="display: none;"> <i class="avatar"></i> <div class="info"> <p class="name">清風(fēng)徐來</p> <p class="text">大家都辛苦啦,感謝各位大大的努力,能圓滿完成真是太好了[笑哭][支持]</p> <p class="time">2022-10-10 20:29:21</p> </div> </div> </div>
然后就是我們的css部分,我們要實(shí)現(xiàn)點(diǎn)擊文本框使文本框進(jìn)行均勻下拉的操作,可以使用focus偽類選擇器,
focus是css的一個(gè)偽類選擇器,可以用來選取獲得焦點(diǎn)的元素,然后為這些獲得焦點(diǎn)的元素設(shè)置樣式。
只要是可以接收鍵盤事件或其他用戶輸入的元素都可以:focus選擇器,大多數(shù)情況下:focus選擇器都是被使用在鏈接和表單元素上的。
然后再下面添加運(yùn)動(dòng)的路徑即可
.wrapper { min-width: 400px; max-width: 800px; display: flex; justify-content: flex-end; } .avatar { width: 48px; height: 48px; border-radius: 50%; overflow: hidden; background: url(./images/avatar.jpg) no-repeat center / cover; margin-right: 20px; } .wrapper textarea { outline: none; border-color: transparent; resize: none; background: #f5f5f5; border-radius: 4px; flex: 1; padding: 10px; transition: all 0.5s; height: 30px; } .wrapper textarea:focus { border-color: #e4e4e4; background: #fff; height: 50px; } .wrapper button { background: #00aeec; color: #fff; border: none; border-radius: 4px; margin-left: 10px; width: 70px; cursor: pointer; } .wrapper .total { margin-right: 80px; color: #999; margin-top: 5px; opacity: 0; transition: all 0.5s; } .list { min-width: 400px; max-width: 800px; display: flex; } .list .item { width: 100%; display: flex; } .list .item .info { flex: 1; border-bottom: 1px dashed #e4e4e4; padding-bottom: 10px; } .list .item p { margin: 0; } .list .item .name { color: #FB7299; font-size: 14px; font-weight: bold; } .list .item .text { color: #333; padding: 10px 0; } .list .item .time { color: #999; font-size: 12px; }
接下來就是我們JS部分
我來解析一下下面的思路
還是第一步我們先得獲取里面的元素,只有這樣才能進(jìn)行接下來的操作
然后我們來寫文本焦點(diǎn):tx這個(gè)文本域制作一個(gè)事件,這個(gè)事件是獲得焦點(diǎn),讓下面的0/200字的文字顯示出來
tx.addEventListener('focus',function(){ ? ? ? total.style.opacity=1 ? ? })
當(dāng)我們點(diǎn)擊空白的時(shí)候得隱藏起來,同意復(fù)制上面的代碼只需要改成blur就行
tx.addEventListener('blur',function(){ ? ? ? total.style.opacity=0 ? ? })
3.然后我們得做一個(gè)監(jiān)聽事件,檢測用戶輸入多少字,然后讓下面的0/200行來跟著用戶變化
? ? tx.addEventListener('focus',function(){ ? ? ? total.innerHTML=`${tx.value.length}/200字` ? ? })
第四步我們開始正式操作
實(shí)現(xiàn)按下回車發(fā)布評論
這里的keyup是最新版語法已經(jīng)淘汰之前的keycoad,意思是鍵盤彈出事件,然后這里的e在keyup里面是回車的意思,所以要寫在方法里面
然后我們進(jìn)行判斷,如果用戶輸入不為空就會顯示打印,這邊的trim是消除兩邊空格的意思
item.style.display='block' ? ? ? ? ? ? text.innerHTML=tx.value
意思是顯示文本中的內(nèi)容,然后最后等我們按下回車結(jié)束后,就得清空文本域,所以我們的tx的最后得為空,然后發(fā)現(xiàn)我們下面的沒有復(fù)原,這時(shí)候我們還需要加total.innerHtml='0/200字'才能復(fù)原成之后的樣子
? ? tx.addEventListener('keyup',function(e){ ? ? ? ? //只有按下回車才會觸發(fā) ? ? ? ? if(e.key === 'Enter'){ ? ? ? ? ? ? //如果用戶輸入不為空就會顯示打印 ? ? ? ? ? ? if(tx.value.trim()!==''){ ? ? ? ? ? ? ? ? item.style.display='block' ? ? ? ? ? ? text.innerHTML=tx.value ? ? ? ? ? ? } ? ? ? ? ? ? //等我們按下回車,結(jié)束后,清空文本域 ? ? ? ? ? ? tx.value='' ? ? ? ? ? ? //按下回車就得復(fù)原 ? ? ? ? ? ? total.innerHTML='0/200字' ? ? ? ? } ? ? })
這是JS源碼
<script> const tx=document.querySelector('#tx'); const total=document.querySelector('.total') const item=document.querySelector('.item'); const text=document.querySelector('.text') //1.當(dāng)我們文本獲得焦點(diǎn),讓total顯示出來 tx.addEventListener('focus',function(){ total.style.opacity=1 }) //1.當(dāng)我們文本失去焦點(diǎn),讓total隱藏出來 tx.addEventListener('blur',function(){ total.style.opacity=0 }) // 3.監(jiān)測用戶輸入 tx.addEventListener('focus',function(){ total.innerHTML=`${tx.value.length}/200字` }) //4.按下回車發(fā)布評論 tx.addEventListener('keyup',function(e){ //只有按下回車才會觸發(fā) if(e.key === 'Enter'){ //如果用戶輸入不為空就會顯示打印 if(tx.value.trim()!==''){ item.style.display='block' text.innerHTML=tx.value } //等我們按下回車,結(jié)束后,清空文本域 tx.value='' //按下回車就得復(fù)原 total.innerHTML='0/200字' } }) </script>
還是老規(guī)矩,為了大家看的清除,我一邊進(jìn)行學(xué)習(xí),一邊寫注釋讓我更加明白進(jìn)行的操作,也是為了方便寫文章
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>評論回車發(fā)布</title> <style> .wrapper { min-width: 400px; max-width: 800px; display: flex; justify-content: flex-end; } .avatar { width: 48px; height: 48px; border-radius: 50%; overflow: hidden; background: url(./images/avatar.jpg) no-repeat center / cover; margin-right: 20px; } .wrapper textarea { outline: none; border-color: transparent; resize: none; background: #f5f5f5; border-radius: 4px; flex: 1; padding: 10px; transition: all 0.5s; height: 30px; } .wrapper textarea:focus { border-color: #e4e4e4; background: #fff; height: 50px; } .wrapper button { background: #00aeec; color: #fff; border: none; border-radius: 4px; margin-left: 10px; width: 70px; cursor: pointer; } .wrapper .total { margin-right: 80px; color: #999; margin-top: 5px; opacity: 0; transition: all 0.5s; } .list { min-width: 400px; max-width: 800px; display: flex; } .list .item { width: 100%; display: flex; } .list .item .info { flex: 1; border-bottom: 1px dashed #e4e4e4; padding-bottom: 10px; } .list .item p { margin: 0; } .list .item .name { color: #FB7299; font-size: 14px; font-weight: bold; } .list .item .text { color: #333; padding: 10px 0; } .list .item .time { color: #999; font-size: 12px; } </style> </head> <body> <div class="wrapper"> <i class="avatar"></i> <textarea id="tx" placeholder="發(fā)一條友善的評論" rows="2" maxlength="200"></textarea> <button>發(fā)布</button> </div> <div class="wrapper"> <span class="total">0/200字</span> </div> <div class="list"> <div class="item" style="display: none;"> <i class="avatar"></i> <div class="info"> <p class="name">清風(fēng)徐來</p> <p class="text">大家都辛苦啦,感謝各位大大的努力,能圓滿完成真是太好了[笑哭][支持]</p> <p class="time">2022-10-10 20:29:21</p> </div> </div> </div> <script> const tx=document.querySelector('#tx'); const total=document.querySelector('.total') const item=document.querySelector('.item'); const text=document.querySelector('.text') //1.當(dāng)我們文本獲得焦點(diǎn),讓total顯示出來 tx.addEventListener('focus',function(){ total.style.opacity=1 }) //1.當(dāng)我們文本失去焦點(diǎn),讓total隱藏出來 tx.addEventListener('blur',function(){ total.style.opacity=0 }) // 3.監(jiān)測用戶輸入 tx.addEventListener('focus',function(){ total.innerHTML=`${tx.value.length}/200字` }) //4.按下回車發(fā)布評論 tx.addEventListener('keyup',function(e){ //只有按下回車才會觸發(fā) if(e.key === 'Enter'){ //如果用戶輸入不為空就會顯示打印 if(tx.value.trim()!==''){ item.style.display='block' text.innerHTML=tx.value } //等我們按下回車,結(jié)束后,清空文本域 tx.value='' //按下回車就得復(fù)原 total.innerHTML='0/200字' } }) </script> </body> </html>
總結(jié)
到此這篇關(guān)于用JS實(shí)現(xiàn)回車評論功能的文章就介紹到這了,更多相關(guān)JS回車評論功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript使用WebSocket實(shí)現(xiàn)實(shí)時(shí)通信的技術(shù)詳解
WebSocket作為一種高效的通信協(xié)議,為開發(fā)者提供了一種在客戶端和服務(wù)器之間進(jìn)行全雙工通信的方法,本文將深入探討WebSocket技術(shù),并提供實(shí)戰(zhàn)代碼示例2024-04-04js實(shí)現(xiàn)權(quán)限樹的更新權(quán)限時(shí)的全選全消功能
上一篇發(fā)了添加權(quán)限時(shí)的權(quán)限樹JS源碼,下面把更新時(shí)的也發(fā)給大家借鑒一下,因?yàn)楦聲r(shí)候牽扯到判斷已有權(quán)限等,所以,還要麻煩一些。2009-02-02JS奇技之利用scroll來監(jiān)聽resize詳解
這篇文章主要給大家介紹了JS奇技之利用scroll來監(jiān)聽resize的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06javascript中使用正則表達(dá)式清理table樣式的代碼
本文給大家講解的是使用javascript實(shí)現(xiàn)去除多余的TABLE的樣式,主要通過結(jié)合正則表達(dá)式來實(shí)現(xiàn),非常的簡單實(shí)用,有需要的小伙伴可以參考下。2015-07-07