動畫原理 Document 動畫原理 Document 夏雨荷 動畫封裝 Document 點擊夏雨荷才走 夏雨荷 不同動畫緩動動畫 Document 點擊夏雨荷才走 夏雨荷 ... ...
動畫原理

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { position: absolute; left: 0; width: 100px; height: 100px; background-color: pink; } </style> </head> <body> <div></div> <script> var div = document.querySelector('div'); var tinner = setInterval(function () { if (div.offsetLeft > 400) { clearInterval(tinner); } div.style.left = div.offsetLeft + 1 + 'px' }, 30) </script> </body> </html>動畫原理

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { position: absolute; left: 0; width: 100px; height: 100px; background-color: pink; } span { position: absolute; left: 0; top: 200px; display: block; width: 150px; height: 150px; background-color: purple; } </style> </head> <body> <div></div> <span>夏雨荷</span> <script> function animate(obj, target) { var inner = setInterval(function () { if (obj.offsetLeft >= target) { clearInterval(inner) } obj.style.left = obj.offsetLeft + 1 + 'px' },30) } var div = document.querySelector('div'); var span = document.querySelector('span'); animate(div, 200); animate(span, 300); </script> </body> </html>動畫封裝

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { position: absolute; left: 0; width: 100px; height: 100px; background-color: pink; } span { position: absolute; left: 0; top: 200px; display: block; width: 150px; height: 150px; background-color: purple; } </style> </head> <body> <button>點擊夏雨荷才走</button> <div></div> <span>夏雨荷</span> <script> var btn = document.querySelector('button'); var div = document.querySelector('div'); var span = document.querySelector('span'); function move(obj, target) { clearInterval(obj.tinner); // 防止多次點擊 obj.tinner = setInterval(function () { if (obj.offsetLeft >=target) { clearInterval(tinner) } obj.style.left = obj.offsetLeft + 1 + 'px' },10) } move(div, 300); btn.addEventListener('click', function () { move(span,400) }) </script> </body> </html>不同動畫
緩動動畫

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { position: absolute; left: 0; width: 100px; height: 100px; background-color: pink; } span { position: absolute; left: 0; top: 200px; display: block; width: 150px; height: 150px; background-color: purple; } </style> </head> <body> <button>點擊夏雨荷才走</button> <span>夏雨荷</span> <script> // 緩動動畫函數封裝obj目標對象 target 目標位置 // 思路: // 1. 讓盒子每次移動的距離慢慢變小, 速度就會慢慢落下來。 // 2. 核心演算法:(目標值 - 現在的位置) / 10 做為每次移動的距離 步長 // 3. 停止的條件是: 讓當前盒子位置等於目標位置就停止定時器 function animate(obj, target) { // 先清除以前的定時器,只保留當前的一個定時器執行 clearInterval(obj.timer); obj.timer = setInterval(function() { // 步長值寫到定時器的裡面 var step = (target - obj.offsetLeft) / 30; if (obj.offsetLeft == target) { // 停止動畫 本質是停止定時器 clearInterval(obj.timer); } // 把每次加1 這個步長值改為一個慢慢變小的值 步長公式:(目標值 - 現在的位置) / 10 obj.style.left = obj.offsetLeft + step + 'px'; }, 15); } var span = document.querySelector('span'); var btn = document.querySelector('button'); btn.addEventListener('click', function() { // 調用函數 animate(span, 500); }) // 勻速動畫 就是 盒子是當前的位置 + 固定的值 10 // 緩動動畫就是 盒子當前的位置 + 變化的值(目標值 - 現在的位置) / 10) </script> </body> </html>原理

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> span { position: absolute; left: 0; top: 200px; display: block; width: 150px; height: 150px; background-color: purple; } </style> </head> <body> <button class="btn500">點擊夏雨荷到500</button> <button class="btn800">點擊夏雨荷到800</button> <span>夏雨荷</span> <script> function animate(obj, target) { // 清理定時器,防止多次點擊 clearInterval(obj.timer); obj.timer = setInterval(function () { // 把我們步長值改為整數 不要出現小數的問題 var step = (target - obj.offsetLeft) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); // 到達目標就清除 if (obj.offsetLeft == target) { clearInterval(obj.timer); } obj.style.left = obj.offsetLeft + step + 'px' }, 15); } var btn500 = document.querySelector('.btn500'); var btn800 = document.querySelector('.btn800'); var span = document.querySelector('span'); btn500.onclick = function () { animate(span, 500); }; btn800.onclick = function () { animate(span, 800); } </script> </body> </html>緩動動畫數值間移動

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> span { position: absolute; left: 0; top: 200px; display: block; width: 150px; height: 150px; background-color: purple; } </style> </head> <body> <button class="btn500">點擊夏雨荷到500</button> <button class="btn800">點擊夏雨荷到800</button> <span>夏雨荷</span> <script> function animate(obj, target, callback) { // 清理定時器,防止多次點擊 clearInterval(obj.timer); obj.timer = setInterval(function () { // 把我們步長值改為整數 不要出現小數的問題 var step = (target - obj.offsetLeft) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); // 到達目標就清除 if (obj.offsetLeft == target) { clearInterval(obj.timer); // 回調函數 if (callback){ callback() } } obj.style.left = obj.offsetLeft + step + 'px' }, 15); } var btn500 = document.querySelector('.btn500'); var btn800 = document.querySelector('.btn800'); var span = document.querySelector('span'); btn500.onclick = function () { animate(span, 500); }; btn800.onclick = function () { animate(span, 800, function () { span.style.backgroundColor = 'red'; // 回調函數 }); } </script> </body> </html>緩動動畫回調函數

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .sliderbar { position: fixed; right: 0; bottom: 100px; width: 40px; height: 40px; text-align: center; line-height: 40px; cursor: pointer; color: #fff; } .con { position: absolute; left: 0; top: 0; width: 200px; height: 40px; background-color: purple; z-index: -1; } </style> <script src="animate.js"></script> </head> <body> <div class="sliderbar"> <span>←</span> <div class="con">問題反饋</div> </div> <script> // 獲取元素 var sliderbar = document.querySelector('.sliderbar'); var con = document.querySelector('.con'); sliderbar.onmouseenter = function () { animate(con, -160, function() { // 當我們動畫執行完畢,就把 ← 改為 → sliderbar.children[0].innerHTML = '→'; }); }; sliderbar.onmouseleave = function () { animate(con, 0, function() { // 當我們動畫執行完畢,就把 → 改為 ← sliderbar.children[0].innerHTML = '←'; }); } </script> </body> </html>緩動動畫底部
相關資料: https://github.com/1515806183/html-code/tree/master/wapapi-04