当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS写滑稽笑脸运动效果

JS写滑稽笑脸运动效果

2020年06月23日  | 移动技术网IT编程  | 我要评论

效果演示:

(就这玩意儿,差点写崩了...)

代码:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>滑稽笑脸运动</title>
  <meta name="author" content="marinerzp">
  <style>
    *{padding: 0;margin: 0;}
    html,body{
      width: 100%;
      height: 100%;
    }
    #main{
      width: 100px;
      height: 100px;
      border-radius: 50%;
      background:url(images/1.jpg) 0 0/100px 100px;
      position: absolute;
      left: 0;
      top: 0;
      z-index: 3;
    }
    .show{
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background-color: rgb(239, 187, 101);
      position: absolute;
      animation: disappear 1.2s ;
      animation-fill-mode: forwards;
      
    }
    @keyframes disappear{
      0%{
        opacity: 1;
        transform:scale(1);
      }
      100%{
        opacity: 0;
        transform:scale(0);
      }
    }
  </style>
</head>
<body>
  <div id="main">
  </div>
  
  <script>
    let omain=document.queryselector('#main');
    let maxleft=window.innerwidth-omain.offsetwidth;
    let maxtop=window.innerheight-omain.offsetheight;
 
    window.οnresize=function(){//监听窗口大小改变事件
      maxleft=window.innerwidth-omain.offsetwidth;
      maxtop=window.innerheight-omain.offsetheight;
    };
    /*
      水平方向上:以向右为正方向
      竖直方向上:以向下为正方向
    */ 
    let vx=6;//3px/s
    let vy=9;//4px/s
    let x=0,y=0;
    ~~function move(){
      x+=vx;
      y+=vy;
      if (y>=maxtop) {
        y=maxtop;
        vy=-vy;
      }
      if (y<=0) {
        y=0;
        vy=-vy;
      }
      if (x>=maxleft) {
        x=maxleft;
        vx=-vx;
      }
      if (x<=0) {
        x=0;
        vx=-vx;
      }
      omain.style.left=`${x}px`;
      omain.style.top=`${y}px`;
      
      createtail(x,y);//生成拖尾
      requestanimationframe(move);
    }();
    function createtail(x,y){
      let node=document.createelement('p');
      node.classlist.add('show');
      node.style.csstext=`left:${x+20}px;top:${y+20}px`;
      document.body.appendchild(node);
      settimeout(()=>{
        document.body.removechild(node);
        node=null;
      },1200);
    }
    
 
  </script>
</body>
</html>

总结

到此这篇关于js写滑稽笑脸运动效果的文章就介绍到这了,更多相关js 滑稽笑脸内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网