追蹤游標的貓

In this example below you will see how to do a 追蹤游標的貓 with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by mollymoumou, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright mollymoumou ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>追蹤游標的貓</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <img id="cat" src="https://scontent-tpe1-1.xx.fbcdn.net/v/t1.0-9/16830767_1589004787781108_7381362622397495716_n.jpg?oh=63fb73590b8bb27170b2c9dd4fd40785&amp;oe=59443DC8" alt=""/>
<div id="cross"></div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/mollymoumou/andx8ffdandx8e64andx6e38andx6a19andx7684andx8c93-evOzyr */
html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#cat {
  width: 200px;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0px;
}

#cross {
  position: absolute;
}
#cross:before, #cross:after {
  content: "";
  display: block;
  width: 60px;
  height: 15px;
  background-color: #e2ac16;
  border-radius: 50%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) rotate(45deg);
}
#cross:after {
  transform: translate(-50%, -50%) rotate(-45deg);
}


/*Downloaded from https://www.codeseek.co/mollymoumou/andx8ffdandx8e64andx6e38andx6a19andx7684andx8c93-evOzyr */
$(window).mousemove(function(evt){
  var x = evt.pageX;
  var y = evt.pageY;
  
  console.log(x + "," + y);
  
  $("#cross").css("left",x+"px");
  $("#cross").css("top",y+"px");
  
  if (x < $(window).width()/2 - 50){
 $("#cat").attr("src","https://scontent-tpe1-1.xx.fbcdn.net/v/t1.0-9/16681929_1589004784447775_3705366261044071880_n.jpg?oh=4189c74a90263c1f43c31d0def804d5d&oe=592A3C27");
  }
  if (x > $(window).width()/2 + 50){
 $("#cat").attr("src","https://scontent-tpe1-1.xx.fbcdn.net/v/t1.0-9/16830948_1589004791114441_3807370272443576278_n.jpg?oh=665f04617f5081d29359fa5e42d8ae2d&oe=59414F99");
  }
  if ( Math.abs(x - $(window).width()/2) < 50){
 $("#cat").attr("src","https://scontent-tpe1-1.xx.fbcdn.net/v/t1.0-9/16649455_1589004814447772_7578691161876935751_n.jpg?oh=18c0812ea444d81362ee151dfd21be37&oe=593943E5");
  }
  if (x < $(window).width()/2 - 50 && y < $(window).height()/2){
  $("#cat").attr("src","https://scontent-tpe1-1.xx.fbcdn.net/v/t1.0-9/16830767_1589004787781108_7381362622397495716_n.jpg?oh=63fb73590b8bb27170b2c9dd4fd40785&oe=59443DC8")
  }
  if (x > $(window).width()/2 + 50 && y < $(window).height()/2){
  $("#cat").attr("src","https://scontent-tpe1-1.xx.fbcdn.net/v/t1.0-9/16649234_1589004807781106_1118924346477253991_n.jpg?oh=84c817dece92efc1aa5a144bee4ef687&oe=59460081")
  }
  
});

Comments