(JavaScript) デバイスの傾き

In this example below you will see how to do a (JavaScript) デバイスの傾き with some HTML / CSS and Javascript

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

Technologies

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

<head>
  <meta charset="UTF-8">
  <title>(JavaScript) デバイスの傾き</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js" type="text/javascript"></script>


    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div id="box"></div>

<div id="alpha"></div>
<div id="beta"></div>
<div id="gamma"></div>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

  

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




</body>

</html>

/*Downloaded from https://www.codeseek.co/moonglows76/javascript-andx30c7andx30d0andx30a4andx30b9andx306eandx50beandx304d-rYNmXe */
div { font-size: 20px; }
#box { position: absolute; top: 50%; left: 50%; width: 100px; height: 100px; margin-left: -50px; margin-top: -50px; background-color: black; }


/*Downloaded from https://www.codeseek.co/moonglows76/javascript-andx30c7andx30d0andx30a4andx30b9andx306eandx50beandx304d-rYNmXe */
$(function(){

    $box = $("#box");
    window.addEventListener("deviceorientation", function(e){

        $("#alpha").html("alpha: " + e.alpha);
        $("#beta").html("beta: " + e.beta);
        $("#gamma").html("gamma: " + e.gamma);

        $box.css({
            "margin-top": (-100 + e.beta*10) + "px",
            "margin-left": (-100 + e.gamma*10) + "px",
            "background-color": "hsl(" + e.alpha + ", 50%, 50%)"
        });
    });

});

Comments