js實(shí)現(xiàn)盒子滾動(dòng)動(dòng)畫效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)盒子滾動(dòng)動(dòng)畫效果的具體代碼,供大家參考,具體內(nèi)容如下
1、效果圖1:
2、效果圖2:
3、源代碼如下:
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title> <style> *{ margin: 0; padding: 0; } .box1{ /* 必須加定位才可以動(dòng) */ position: absolute; left: 0; width: 50px; height: 50px; background-color: blueviolet; } </style></head><body> <div class='box1'></div> <script> // 動(dòng)畫原理: // 1.獲得盒子當(dāng)前位置 // 2.讓盒子在當(dāng)前位置加上2px 的移動(dòng)距離 // 3.利用定時(shí)器不斷重復(fù)中國(guó)操作 // 4.接觸定時(shí)器 // 5.注意添加定位,才可以使用element.style.left var box1 =document.querySelector(’.box1’) // 獲取事件源 var timer = setInterval(function(){ if( box1.offsetLeft >= 500){clearInterval(timer); // 清除定時(shí)器 }else{// 每50毫秒就將新的值給box1.leftbox1.style.left = box1.offsetLeft +2 +’px’; } },50) </script></body></html>
4、喜歡的朋友記得 點(diǎn)贊 轉(zhuǎn)發(fā) 關(guān)注噢
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Vue實(shí)現(xiàn)div滾輪放大縮小2. Android中的緩存3. Android實(shí)現(xiàn)動(dòng)態(tài)改變shape.xml中圖形的顏色4. PHP擴(kuò)展之APC——Alternative PHP Cache(可選PHP緩存)5. APP啟動(dòng)慢怎么辦,Android官方這樣說6. vue實(shí)現(xiàn)自定義多選按鈕7. android RecycleView實(shí)現(xiàn)多級(jí)樹形列表8. vue項(xiàng)目中自定義video視頻控制條的實(shí)現(xiàn)代碼9. 在Vue中創(chuàng)建可重用的 Transition的方法10. Vue 3.0 全家桶搶先體驗(yàn)
