Jump to content

[Javascript]How can make my function rotate 360 and move up and down?

Go to solution Solved by cynexit,

JS is overkill for such a simple task, use CSS:


@-webkit-keyframes rotating {
  from {
    -ms-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -webkit-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  to {
    -ms-transform: rotate(360deg);
    -moz-transform: rotate(360deg);
    -webkit-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
@keyframes rotating {
  from {
    -ms-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -webkit-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  to {
    -ms-transform: rotate(360deg);
    -moz-transform: rotate(360deg);
    -webkit-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
#dank {
  -webkit-animation: rotating 2s linear infinite;
  -moz-animation: rotating 2s linear infinite;
  -ms-animation: rotating 2s linear infinite;
  -o-animation: rotating 2s linear infinite;
  animation: rotating 2s linear infinite;
}

Source

Hey guys so I'm working on animating my java script for a project, and being a newbie at java-script I have only figured out how to make my image move up and down in a set area. How can make my object "dank" also spin 360 infinitly whiles using the following code to move it up and down? Any help is appreciated!

window.addEventListener("load", function() {         
function myMove() {
   var elem = document.getElementById("dank");
   var pos = 100;
   var id = setInterval(frame, 10);
   function frame() {
        pos += 0.3;
        elem.style.top = pos + "px";
             if (Math.abs(pos) >= 250){
                   pos = 100;
                }
                frame();
            }
            } 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[DESKTOP] CPU: Intel Core i7 8700k GPU: ASUS ROG 2080TI RAM: 64GB Corsair Fury DDR4 Mobo: Asus z390-A Prime

Storage:1x 1TB Samsung 990 Pro SSD 2x 1TB WD Black SSD, 2x 3Tb Seagate  HDD PSU: Seasonic 950W

Monitors: 2x Asus TUF Gaming 24" 1440p  144hz, 2x LG  75Hz 1080p LED

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[SERVER/NAS] CPU: Intel Core i7 7700K GPU: Nvidia 1060 6GB RAM: 32 GB Corsair Vengeance Mobo: Asus z-170P D3

Storage:1x 1TB Western Digital SSD, 4x Ironwolf Pro 12Tb HDD, 2x 2TB WD Blue  PSU: Thermaltake 650W

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[LAPTOP (MacBook Pro 16")] CPU: 2.4 GHz 8-Core Intel Core i9, RAM: 32 GB 2667 MHz DDR4, SSD: 2 TB

GPU: AMD Radeon Pro 5500M 8GB Display: 16-inch (3072 x 1920)

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Link to comment
Share on other sites

Link to post
Share on other sites

JS is overkill for such a simple task, use CSS:


@-webkit-keyframes rotating {
  from {
    -ms-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -webkit-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  to {
    -ms-transform: rotate(360deg);
    -moz-transform: rotate(360deg);
    -webkit-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
@keyframes rotating {
  from {
    -ms-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -webkit-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  to {
    -ms-transform: rotate(360deg);
    -moz-transform: rotate(360deg);
    -webkit-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
#dank {
  -webkit-animation: rotating 2s linear infinite;
  -moz-animation: rotating 2s linear infinite;
  -ms-animation: rotating 2s linear infinite;
  -o-animation: rotating 2s linear infinite;
  animation: rotating 2s linear infinite;
}

Source

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×