js實現盒子移動動畫效果
本文實例為大家分享了js實現盒子移動動畫效果的具體代碼,供大家參考,具體內容如下
<!doctype html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'> <meta http-equiv='X-UA-Compatible' content='ie=edge'> <title>Document</title> <style> .box { width: 200px; height: 200px; border: 1px solid red; position: absolute; left: 0; top: 50px; } </style></head><body><input type='button' value='前進' id='box_start'><input type='button' value='停止' id='box_stop'><input type='button' value='回退' id='box_back'><br><br><div class='box'></div><script> let boxStart = document.getElementById('box_start'); let boxStop = document.getElementById('box_stop'); let boxBack = document.getElementById('box_back'); let timeId_1; let timeId_2; boxStart.onclick = function () { let box = document.getElementById('box'); clearInterval(timeId_2); timeId_1 = setInterval(function () { if (box.offsetLeft >= 600) { clearInterval(timeId_1); box.style.left = 600 + ’px’; alert(’到達目的地’); } else { box.style.left = box.offsetLeft + 10 + ’px’; } }, 100); }; boxBack.onclick = function () { let box = document.getElementById('box'); clearInterval(timeId_1); timeId_2 = setInterval(function () { if (box.offsetLeft <= 0) { clearInterval(timeId_2); box.style.left = '0'; alert(’已在出發位置’); } else { box.style.left = box.offsetLeft - 10 + ’px’; } }, 100); }; boxStop.onclick = function () { clearInterval(timeId_1); clearInterval(timeId_2); };</script></body></html>
效果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. React+umi+typeScript創建項目的過程2. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執行過程解析3. SharePoint Server 2019新特性介紹4. ASP中常用的22個FSO文件操作函數整理5. 三個不常見的 HTML5 實用新特性簡介6. ASP調用WebService轉化成JSON數據,附json.min.asp7. .Net core 的熱插拔機制的深入探索及卸載問題求救指南8. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁9. 讀大數據量的XML文件的讀取問題10. 解決ASP中http狀態跳轉返回錯誤頁的問題
