在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
本文实例为大家分享了javascript实现缓动动画效果的具体代码,供大家参考,具体内容如下 实现思路1、主要使用setInterval定时函数 代码示例<!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>aninamate动画</title> <style> * { margin: 0; padding: 0; } .content { width: 1000px; margin: 0 auto; } button { padding: 5px; margin: 60px 10px; border: 1px solid #666; outline-color: palevioletred; } .both { background-color: pink; color: #fff; background-color: palevioletred; } .box { position: relative; height: 210px; margin: 0px auto; background-color: #191b28; } .yutu { position: absolute; top: 0; left: 0; width: 180px; height: 210px; } .qiaojingjing { position: absolute; top: 0; left: 820px; width: 180px; height: 210px; } .word1 { display: none; position: absolute; top: -50px; left: 45%; } .word2 { display: none; position: absolute; top: -30px; left: 50%; } </style> </head> <body> <div class="content"> <button class="btn1">于途前进</button> <button class="btn2">乔晶晶前进</button> <button class="both">双向奔赴</button> <button class="btn3">于途后退</button> <button class="btn4">乔晶晶后退</button> <div class="box"> <img src="images/于途.png" alt="" class="yutu"> <img src="images/乔晶晶.png" alt="" class="qiaojingjing"> <span class="word1">余生请多指教!</span> <span class="word2">余生请多指教!</span> </div> </div> <script> var btn1 = document.querySelector('.btn1'); var btn2 = document.querySelector('.btn2'); var btn3 = document.querySelector('.btn3'); var btn4 = document.querySelector('.btn4'); var both = document.querySelector('.both'); var yutu = document.querySelector('.yutu'); var qiaojingjing = document.querySelector('.qiaojingjing'); var word1 = document.querySelector('.word1'); var word2 = document.querySelector('.word2'); btn1.addEventListener('click', function() { animate(yutu, 340, function() { word1.style.display = 'block'; }); }); btn2.addEventListener('click', function() { animate(qiaojingjing, 520, function() { word2.style.display = 'block'; }); }); btn3.addEventListener('click', function() { animate(yutu, 0, function() { word1.style.display = 'none'; }); }); btn4.addEventListener('click', function() { animate(qiaojingjing, 820, function() { word2.style.display = 'none'; }); }); both.addEventListener('click', function() { animate(yutu, 340); animate(qiaojingjing, 520); word1.style.display = 'block'; word2.style.display = 'block'; }); // 动画函数 obj动画对象, target目标左偏移量, callback回调函数 function animate(obj, target, callback) { // 清除之前的动画 clearInterval(obj.timer); obj.timer = setInterval(function() { // 计算出每次移动距离 var step = (target - obj.offsetLeft) / 20; // 步数取整 step = step > 0 ? Math.ceil(step) : Math.floor(step); obj.style.left = obj.offsetLeft + step + 'px'; if (obj.offsetLeft == target) { // 停止动画 clearInterval(obj.timer); // 如果有回调函数,执行回调函数 if (callback) { callback(); } } }, 30); } </script> </body> </html> 动画效果: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持极客世界。 |
请发表评论