Struggling with creating animated squares using jQuery

I am attempting to create two animated boxes using jQuery, CSS, and HTML. However, the code is not functioning as expected. The code works fine in a jQuery tutorial app, but when I try it on my own PC, it does not work. Can you help me identify what is wrong with my code?

Here is the code snippet:

<!doctype html>
<html>
<head>
<title>Testing Animation</title>
</head>
<body>
<div class="blueBox"></div>
<div class="redBox"></div>
<style>
.blueBox, .redBox {
    width: 100px;
    height: 100px;
    top: 20px;
    position: absolute;
}
.redBox {
    left: 20px;
    background-color: red;
}
.blueBox {
    left: 140px;
    background-color: blue; 
}
</style>
<script type="text/javascript">
$(document).ready(function(){
    var $blueBox = $('div.blueBox');
    var $redBox = $('div.redBox');
    $blueBox.animate({left: "500px"}, 2000, function(){alert("Animation Completed!");
    });
    $redBox.animate({width: "0px", height: "0px", opacity: "0"}, 3000);

});

</script>
</body>
</html>

Any guidance or suggestions would be greatly appreciated!

Answer №1

Ensure to include the jQuery code in your HTML document:

<script src="http://code.jquery.com/jquery-latest.min.js"
    type="text/javascript"></script>

It is recommended to place it within the <head> tag.

If you prefer a version above v1.11.1, obtain the code link directly from the specific version on the jQuery download page. For example:

<script src="http://code.jquery.com/jquery-2.1.3.min.js"
    type="text/javascript"></script>

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

Disable css on an element when scrolling upwards

I am in the process of creating a framework that requires displaying navigation (previous/next) when the second slide becomes active. This works correctly while scrolling down, but now I need to address the case where, while scrolling up from the bottom, t ...

Revealing elements with AngularJS ng-show directive prior to concealing them

Currently, I am in the process of implementing a slide effect for bootstrap badges to showcase hierarchical data relationships using AngularJS. My goal is to have a slider-effect that smoothly reveals new sub-categories while concealing previously open su ...

Modifying the color of the accordion icon in Bootstrap 5.3 by utilizing a root variable

Is it possible to change the icon color of Bootstrap 5.3's accordion component to match the color of the heading text? I have successfully set the heading text color using a CSS root variable var(--body-text). However, when trying to override the SVG ...

Accessing Firebug in Selenium webdriver is a straightforward process that involves downloading and

Seeking guidance on how to approach a requirement using Selenium webdriver as I am new to it. The requirement entails: Select the note icon to open a 'note' pop up. Then, open Firebug within the pop-up. Access the script option in Firebug, se ...

Design a responsive layout featuring a div containing four buttons that incorporate text and logos using Bootstrap 5

Looking for help with a web application built using HTML/CSS/Bootstrap 5/JS and Rust/Actix-web? Check out the code here. The index page of the app features a navbar and a div containing four buttons in the center (vertically and horizontally aligned). The ...

Is there a way to allow scrolling in only one direction using CSS?

I am facing an issue with resizing elements within a list. The requirement is that when the elements are resized, they should overflow the container only in the x direction. There must not be any scrolling in the x direction, but scrolling should be allowe ...

Showcasing the ajax response in a textarea immediately upon the page loading

I am facing an issue with a continuous ajax request that fetches data to be displayed in a textarea. The problem is how to display the data immediately after the page loads. Snippet of my template code: <html> <head> <script type="t ...

What are the steps to update your profile picture using Angular?

In my Angular 6 application, I am implementing an image upload feature with the following code: Html: <img [src]="url ? url : 'https://www.w3schools.com/howto/img_avatar.png'"> <br/> <input type='file' (change)="onSelec ...

If the option is not chosen, remove the requirement for it

I am in the process of creating a form that offers 3 different payment options: 1 - Direct Deposit 2 - Credit Card 3 - Cash at Office The issue I am facing is with the validation of input fields for Credit Cards. I want to make it so that these field ...

Angular: Dividing a web page with its individual controller

Exploring Angular Composition An individual new to Angular is eager to implement the concept of "composition," where the main page contains various smaller web page components. Starting off with just a Header section that binds perfectly. <html> &l ...

Issue with .get() method in jQuery affecting IE7, IE8, and IE9

While testing the sending and receiving of data to a PHP file using jQuery, I encountered an issue with Internet Explorer (I am using IE9, but I also checked on IE8 and IE7). When I click on one of my divs, I send an "ID" to the server and the PHP file re ...

When it comes to designing responsive websites, the use of `@

Being new to CSS, I am open to criticism and feedback. @media (max-width: 735px) {... } @media (min-width: 735px) {... } @media (width: 320px) {... } @media (width: 360px) {... } @media (width: 375px) {... } @media (width: 414px) {... } I have tried us ...

The YouTube search function successfully sorts videos by view count, but it encounters issues when trying to sort by

My script is having issues when using orderby=published, but works fine with orderby=viewcount or orderby=rating. Could the problem be related to the query itself, or does the json format returned by 'published' not work with this script? The scr ...

Using Django to submit a newsletter form using AJAX

I am working on developing a newsletter system using Django and Ajax. So far, I have set up the necessary URLs and views as shown below: #newsletter/urls.py from django.conf.urls import patterns, include, url urlpatterns = patterns('newsletter.views ...

Obtaining a variable from within two nested functions

I'm looking for a solution where I can retrieve a variable from inside a function using an ajax call. For example: function returnStuff(){ $.ajax({ //do something }).done(function(response){ return response; }) return response; } The ...

The CSS transformation is being overridden

Having an issue where I have two functions that both apply a CSS transform to an element using jQuery, but they end up overwriting each other. These functions are called at different times and have no knowledge of each other. An example showcasing the pro ...

grab the destination URL from the embedded frame

Thank you for your attention. I am working with three iframes spread across different HTML documents. Here is how they are organized: In the file iframemain.html, there is: <iframe src="iframeparent.html" width="100%" height="600"> </iframe> ...

Mastering card sliding and spacing in React NativeLearn how to effortlessly slide cards horizontally in your React

My aim with the following code snippet is to achieve two objectives: Apply a margin of 20 units to each card Display all four cards in a single row, allowing users to swipe horizontally Unfortunately, I have not been successful in achieving either of th ...

Execute the script only if the designated object is present on the page

A few years ago, I wrote some JavaScript code that is now causing errors to pop up in the console on pages where the object it relies on is not present. The error messages look like this... Cannot read property 'top' of undefined TypeError: Ca ...

The configuration of the Jquery datepicker and how it interacts with the database's

I recently encountered an issue when trying to change the Jquery UI datepicker format to euro time. Following the instructions, I added the following lines of code: $( "#datepicker,#vreme2" ).datepicker(); $( "#datepicker,#vreme2" ).datepicker("opti ...