js實(shí)現(xiàn)盒子移動動畫效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)盒子移動動畫效果的具體代碼,供大家參考,具體內(nèi)容如下
<!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='前進(jìn)' 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(’到達(dá)目的地’); } 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(’已在出發(fā)位置’); } else { box.style.left = box.offsetLeft - 10 + ’px’; } }, 100); }; boxStop.onclick = function () { clearInterval(timeId_1); clearInterval(timeId_2); };</script></body></html>
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 利用CSS3新特性創(chuàng)建透明邊框三角2. html清除浮動的6種方法示例3. CSS代碼檢查工具stylelint的使用方法詳解4. Vue3使用JSX的方法實(shí)例(筆記自用)5. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程6. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. 使用css實(shí)現(xiàn)全兼容tooltip提示框9. JavaScript數(shù)據(jù)類型對函數(shù)式編程的影響示例解析10. 不要在HTML中濫用div
