Animating a CSS shape with the .animate method

I need help creating a dynamic graphic for my school project using css, jquery, and html. I want to make a rectangle that moves across the screen, but I'm having trouble getting it to work properly. Despite trying different variations of the animate function, the rectangle remains stuck on the left side of the window.

Below is the code I have tried:

var derp = 20;
function scroll() {
  var scrollLeft = Math.floor(Math.random() * 100);
  $('#rectangle').animate({
    left: scrollLeft "px",
  }, derp, function() {
    scroll();
  });
}

setInterval(100, scroll());
html,
body {
  width: 100%;
  height: 100%;
  min-height: 100%;
}

body {
  background-image: url("background.jpg");
  background-repeat: no-repeat;
  background-position: right top;
  background-size: cover;
}

.rectangle {
  width: 20px;
  height: 100%;
  background: black;
}

#rectangle {
  left: 10px;
  position: absolute;
}

img,
div,
p,
body {
  margin: 0px;
  padding 0px;
}
<div class="rectangle"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

Answer №1

You have the ability to achieve this using just CSS.

div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation: anim 3s forwards alternate;
  animation-iteration-count: 6;
}

@keyframes anim {
  from {
    left: 0px;
  }
  to {
    left: 200px
  }
}
<div></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Send the parameter to the compile method within the directive

I am currently working on creating a generic field and utilizing a directive for it. In my HTML code, I have defined the following: <div def-field="name"></div> <div def-field="surname"></div> <div def-field="children"></d ...

My method for updating form input properties involves switching the disable attribute from "false" to "true" and vice versa

I have a form that uses Ajax to submit data. Once the user submits the form, the text is updated to indicate that the data was sent successfully, and then the form is displayed with the fields filled out. I want to display the form but prevent users from r ...

How can I establish the conversion from pixels to em units?

According to a source I found, 1 em is equivalent to 16px. You can read more about it here. Despite setting the font-size to 100% in the body and using font-size: 1em, the standard font size in my Jekyll site remains 12px. Curious? Visit my Jekyll site a ...

Best Practices for Variable Initialization in Stencil.js

Having just started working with Stencil, I find myself curious about the best practice for initializing variables. In my assessment, there seem to be three potential approaches: 1) @State() private page: Boolean = true; 2) constructor() { this.p ...

The art of controlling iframe elements with jquery

I've been researching various topics related to this issue, but I'm still unable to achieve the desired outcome. Currently, I am embedding an iframe within an HTML document like so: <iframe class="full-screen-preview__frame" id="nitseditpre ...

Optimizing Style Inclusion Order with Vue SFC, vue-loader, and webpack

My Vue/SFC/webpack/vue-loader application integrates bootstrap using 'import', with component styles directly included in the SFCs. However, vue-loader always places bootstrap after the component styles, preventing proper overwrite/cascade over b ...

Can you create a stroke that is consistently the same width as the container BoxElement?

Utilizing a BoxElement provided by the blessed library, I am showcasing chat history. New sentences are inserted using pushLine. To enhance readability, days are separated by lines (which are added using pushLine). The width of each line matches that of t ...

Issues with zoom functionality not functioning properly within IE11

I am currently developing an application with Angular that is designed to be compatible with tablets and touch-enabled devices. One of the key features I want to implement is the ability for users to zoom/scale up the app, especially for those with visual ...

What is the best way to showcase the final div at the top with CSS?

I'm working with 4 separate divs, with the last one acting as a footer. Is there a way to position this last div at the top using CSS? ...

What could be the reason why I am unable to load the ejs file?

https://i.stack.imgur.com/91bPg.png Issue: Unable to find the "index.ejs" view in the views directory ...

Ways to create collapsible navigation bars in your website

As someone exploring client-side development, I may be misusing the term "collapsible" in my title. What I aim to accomplish in my web application is allowing users to collapse header bars into small chevrons and expand them back when necessary. I am on t ...

The background widths do not stretch to 100%, but rather stop right before reaching the

Experience it live: Adjust your screen width slightly to activate the mobile layout, but ensure that there is still enough room for a horizontal scroll bar. Astonishingly, all my background images stop short of this point. Innovative Fix To prevent the ...

Issue with integrating Contact Form 7 HTML into Wordpress

I've revisited this question with a new "input" HTML tag Despite changing everything, CSS still isn't being applied. HTML Code : <form action="#" class="contact-one__form row"> <div class="col-lg-6"> ...

Switch the dropdown selection depending on the checkbox status

I'm currently facing a bit of confusion with my project. I am constrained by an existing framework and need to come up with a workaround. To simplify, I am tasked with populating a dropdown list based on the selected checkboxes. I have managed to get ...

Error in jQuery and Canvas Image Crop: The index or size is invalid, exceeding the permissible limit

Recently, I downloaded and installed the Canvas Image Crop plugin from CodeCanyon but encountered a problem specifically on firefox. An error message kept popping up whenever I tried to upload certain images: "Index or size is negative or greater than the ...

Tips for embedding a file within a text box in HTML and enabling users to make direct edits

I am currently working on a feature that allows users to open a .txt or .html file from their file explorer and paste the contents into a textarea for editing and saving purposes. My question is whether it's possible to read the file content and auto ...

simple steps to retrieve elements from an xml or string

I have integrated a third-party API and it is returning the following XML data: <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> \u000a <errorNotification> \u000a\u0009 <message>invalid company code<& ...

Teaching sessions along with the implementation of functions

I've created a set of input fields with the class replaceInput. The idea is to have a simple function that clears the value when the user focuses on the field, and then returns it to 'X' if the field is empty on focus out. My query is, coul ...

Struggling with the Nivo slider not loading properly?

Check out my personal website. I'm having an issue with my Nivo slider not displaying properly - it just keeps loading. Any ideas on why this is happening and how I can fix it? Below is the CSS I am using: #slider { position:relative; width: ...

Adding an input field and icon at the center using ::after

Having trouble centering an input field with an icon after it in a div. I can center the input text easily using text-align:center, but struggling to center the icon added using ::before. The search text is centered, but not the icon How can I center bot ...