• JavaScript 实现简单的移动和缓动的效果


    目录

    一、HTML布局

    二、CSS布局

    三、JavaScript代码——移动

     四、JavaScript代码——缓动

    五、完整代码 


    一、HTML布局

    简单定义两个盒子,一个做移动效果、一个做缓动效果。

    1. <div class="box box1">div>
    2. <div class="box box2">div>

    二、CSS布局

    为两个盒子定义宽高和背景色,随便做个效果哦!能看就管,

    三、JavaScript代码——移动

    1.获取元素

    把刚才html定义的元素获取过来在js中运用。

    1. var box1 = document.querySelector('.box1');
    2. var box2 = document.querySelector('.box2');

    2.封装函数实现移动

    num:定义盒子每次都移动h1个像素。

    target: 每次移动的距离。

    if判断:通过if来判断,到达了设定距离,就会删除间隔函数。

    box.style.left:没有达到距离,一直赋值给‘盒子’左边距。

    myRun:给盒子设计点击事件,点击才会出现移动,this指向box1,里面是所调用的值,可以直接在里面修改,移动一次的距离,一共移动的距离。

    1. function myRun(box,h1,h2){
    2. var myInter = setInterval(function(){
    3. var offsetLeft = box.offsetLeft;
    4. var num = h1;
    5. var target = h2;
    6. if(offsetLeft==target){
    7. clearInterval(myInter);
    8. }else{
    9. box.style.left = offsetLeft+num+'px';
    10. }
    11. },1000)}
    12. box1.onclick=function(){
    13. myRun(this,50,200); }

    移动效果

     四、JavaScript代码——缓动

    与移动同理使用函数来封装,代码大致与js移动相同,中间判断与上文稍微有些不同,其中的含义是,第一次移动取移动距离的十分之一,接下来的每一次移动,都是取省下来还剩多少距离的十分之一,取整是为了,在无线接近于所设置的距离可以移动。

    1. function move(obj,sum){
    2. var liLi = setInterval(function(){
    3. var offsetLeft =obj.offsetLeft;
    4. var num = (sum - offsetLeft)/10;
    5. num > 0 ? Math.ceil(num):Math.floor(num);
    6. if(offsetLeft==sum){
    7. clearInterval(liLi);
    8. }else{
    9. obj.style.left = offsetLeft+num+'px';
    10. }
    11. },1000)
    12. } box2.onclick=function(){
    13. move(this,200);
    14. }

    缓动效果

    五、完整代码 

    1.移动代码

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>移动title>
    6. <style>
    7. * {
    8. padding: 0px;
    9. margin: 0px;
    10. }
    11. .box {
    12. width: 200px;
    13. height: 200px;
    14. background: blue;
    15. position: absolute;
    16. left: 0px
    17. }
    18. .box2 {
    19. background: red;
    20. margin-top: 210px;
    21. }
    22. style>
    23. head>
    24. <body>
    25. <div class="box box1">div>
    26. <div class="box box2">div>
    27. <script>
    28. var box1 = document.querySelector('.box1');
    29. var box2 = document.querySelector('.box2');
    30. function myRun(box, h1, h2) {
    31. var myInter = setInterval(function () {
    32. var offsetLeft = box.offsetLeft;
    33. var num = h1;
    34. var target = h2;
    35. if (offsetLeft == target) {
    36. clearInterval(myInter);
    37. } else {
    38. box.style.left = offsetLeft + num + 'px';
    39. }
    40. }, 1000)
    41. }
    42. box1.onclick = function () {
    43. myRun(this, 50, 200);
    44. }
    45. script>
    46. body>
    47. html>

    2.缓动效果

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>移动title>
    6. <style>
    7. * {
    8. padding: 0px;
    9. margin: 0px;
    10. }
    11. .box {
    12. width: 200px;
    13. height: 200px;
    14. background: blue;
    15. position: absolute;
    16. left: 0px
    17. }
    18. .box2 {
    19. background: red;
    20. margin-top: 210px;
    21. }
    22. style>
    23. head>
    24. <body>
    25. <div class="box box1">div>
    26. <div class="box box2">div>
    27. <script>
    28. var box1 = document.querySelector('.box1');
    29. var box2 = document.querySelector('.box2');
    30. function move(obj, sum) {
    31. var liLi = setInterval(function () {
    32. var offsetLeft = obj.offsetLeft;
    33. var num = (sum - offsetLeft) / 10;
    34. num > 0 ? Math.ceil(num) : Math.floor(num);
    35. if (offsetLeft == sum) {
    36. clearInterval(liLi);
    37. } else {
    38. obj.style.left = offsetLeft + num + 'px';
    39. }
    40. }, 1000)
    41. } box2.onclick = function () {
    42. move(this, 200);
    43. }
    44. script>
    45. body>
    46. html>

  • 相关阅读:
    Elasticsearch-IndexTemplate和DynamicTemplate 有什么区别
    【记事本 】dom操作的类型
    Swagger
    学习笔记13--路径-速度分解法之汽车速度规划
    机器学习与深度学习的基本概念
    BLIP-2:冻结现有视觉模型和大语言模型的预训练模型
    vue中$nextTick的使用
    思维分析逻辑 3 DAY
    JAVA编程规范之ORM 映射
    ContentProvider与ContentResolver
  • 原文地址:https://blog.csdn.net/qq_65715980/article/details/125728020