Vertically animating an image using jQuery

I have been experimenting with trying to make an image appear as if it is floating by using jQuery to animate it vertically. After some research, I stumbled upon this thread: Animating a div up and down repeatedly which seems to have the solution I need. However, even after implementing the code on my website, I can't seem to get the image to behave like the example shown here. Here is the current state of the code that I am working with.

HTML

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="styles.css">
    <link href="https://fonts.googleapis.com/css?family=Noto+Sans" rel="stylesheet">
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type ="text/javascript" src="script.js"></script>  

    <!-- Checking to see if running properly
    <script>
    window.onload = function() {
        if (window.jQuery) {  
            // jQuery is loaded  
            alert("Yeah!");
        } else {
            // jQuery is not loaded
            alert("Doesn't Work");
        }
    }
    </script> -->
</head>

<body>

<table id="wrapper">
  <tr>
    <td><img src="PixelBuddah.png" class="bouncer" alt="PixelBuddah" /></td>
  </tr>
</table>

<footer>
    <p>...</p>
</footer>

</body>
</html>

CSS

html, body, #wrapper {
   height:100%;
   width: 100%;
   margin: 0;
   padding: 0;
   border: 0;
}
#wrapper td {
   vertical-align: middle;
   text-align: center;
}

body {
    background-color: #F5E9D3;
}

footer {
    display: inline-block;
    float: right;
    text-align: right;
    margin: auto;
    margin-right: 1.2em;
    font-family: 'Noto Sans', sans-serif;
    font-weight: 600;
}

Javascript

function loop() {
$('.bouncer').animate({'top': '500'}, {
    duration: 1000, 
    complete: function() {
        $('.bouncer').animate({top: 0}, {
            duration: 1000, 
            complete: loop});
    }});

$('<div/>').text('exiting loop').appendTo($('.results'));
}
loop();

I'm still relatively new to this concept, so any guidance or assistance would be greatly appreciated! Thank you!

Answer №1

To adjust the image's position, you must include the position property in the CSS styling for your image. Here is an example of CSS code for the img tag:

function bounce() {
$('.bouncer').animate({'top': '500'}, {
    duration: 1000, 
    complete: function() {
        $('.bouncer').animate({top: 0}, {
            duration: 1000, 
            complete: bounce});
    }});

$('<div/>').text('Exiting bounce effect').appendTo($('.results'));
}
bounce();
html, body, #wrapper {
   height:100%;
   width: 100%;
   margin: 0;
   padding: 0;
   border: 0;
}
#wrapper td {
   vertical-align: middle;
   text-align: center;
}

body {
    background-color: #F5E9D3;
}

footer {
    display: inline-block;
    float: right;
    text-align: right;
    margin: auto;
    margin-right: 1.2em;
    font-family: 'Noto Sans', sans-serif;
    font-weight: 600;
}

img {
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

<table id="wrapper">
  <tr>
    <td><img src="PixelBuddah.png" class="bouncer" alt="PixelBuddah" /></td>
  </tr>
</table>

<footer>
    <p>...</p>
</footer>

</body>

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

Is there a method to halt scrolling through programming?

This code snippet is turning off all scrolling capabilities. var myScroller = new IScroll('#wrapper'); myScroller.disable(); Is there a way to prevent scrolling without disabling iScroll? var myScroller = new IScroller('#wrapper'); m ...

What is the best way to include a new array at the beginning of a lexicographically sorted array?

After lexicographically sorting an array, I am trying to figure out how to add values from another array "cityOfTheMoscow" first and then maintain the lexigraphical order. However, while using the code snippet below: result.unshift(...self.cityOfTheMoscow ...

Delete the content on a webpage using an Ajax jQuery request to transfer it elsewhere

Whenever I submit a form using an ajax post request, I receive values from the controller and manipulate the page based on those values. However, my issue is that I need to erase the values from the previous request when the form is submitted again. For in ...

Exploring various websites simultaneously using window.open

As a newcomer to javascript, I have encountered an issue with the window.open() method that I would like some help with. In my code, I take a user string, modify it in different ways, and then search for these variations. The intention is to open a new wi ...

How can I add a Facebook HTML5 Like Button to AJAX Pages?

I am trying to implement the Facebook HTML5 Like Button on my Ajax views. On my main page, this is what I have: <div id="fb-root"></div> <script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById( ...

Selenium was unsuccessful in finding the text on the webpage

Trying to extract Amazon reviews for a specific product, the text for ratings cannot be located using selenium. However, it can be easily extracted using soup. Link to page: Sample code using Soup: link='same link as mentioned above' url=requ ...

Resize a circle upon hovering while keeping the same thickness of the border

I'm working on creating a circle with just a border (no background) that scales on hover. Here's the code I have so far: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link type="text/css" rel="stylesh ...

Unusual sizing of text input in jQuery Mobile

I've encountered an issue regarding the width of a text input in jQuery Mobile - it appears to be too large. <div data-role="page"> <div data-role="header"> <h1>Test</h1> </div> <div data-role="co ...

How to ensure HTML elements are styled to occupy the full width of their parent container while respecting the minimum width requirement

Is there a way to make HTML elements have a width ranging from 150px to 200px while filling up 100% of their parent's width? Check out Image 1: https://i.sstatic.net/nYNGp.jpg And here is Image 2 (after resizing the window): https://i.sstatic.net/ ...

Verify all inputs are complete in the FullScreenForm

I have been working on implementing a form from this website: http://tympanus.net/Development/FullscreenForm/ My main objective is to validate each input when the form is submitted and display any errors that may arise. To achieve this, I have already se ...

Issues arise when implementing ReactJS transitions for progress bars

As a newcomer to the world of ReactJS, I have been exploring the FB documentations and online tutorials to kickstart my test project. Within this project, I wanted to implement a progress bar that visually represents the user's progression through fi ...

Utilizing i18n's useTranslation and Redux's connect Higher Order Components to export components efficiently

My application has been utilizing Redux for quite some time, with the component exports structured like this: export default connect(mapStateToProps, mapDispatchToProps)(MyComponent); Now, I am integrating an i18n translation library and would like to use ...

Determining the distance between two points in miles using Next.js

Are you familiar with the concept of geographical coordinates? Take for example these two points: point1 = {lat: 40.6974034, lng: -74.1197636} point2 = {lat: 42.694034, lng: -75.117636} My goal is to find the distance between these two poi ...

Using a JQuery variable to display a div using the .show() method

I am attempting to use the .show() method to reveal div elements in a slideshow-like manner. My goal is to utilize the ID of a button on my navigation bar to trigger the display of a specific slide similar to how a slideshow operates. For example, if butt ...

Implementing TypeScript type definitions for decorator middleware strategies

Node middlewares across various frameworks are something I am currently pondering. These middlewares typically enhance a request or response object by adding properties that can be utilized by subsequent registered middlewares. However, a disadvantage of ...

Update the div content to completely replace it with fresh data using Ajax

I'm currently working on implementing a currency switcher feature for my website, allowing users to toggle between two different currencies. The site is a reservation platform with a booking form displayed on the left side and the booking details occu ...

Struggling with sending data to a modal in AngularJS?

I am dealing with the following code snippet $scope.currentTask = undefined; $scope.openModal = function (task, size, parentSelector) { var parentElem = parentSelector ? angular.element($document[0].querySelector('.modal-d ...

The mismatch between JSON schema validation for patternProperties and properties causes confusion

Here is the JSON schema I am working with: { "title": "JSON Schema for magazine subscription", "type": "object", "properties": { "lab": { "type": "string" } }, "patternProperties": { "[A-Za-z][A-Za-z_]*[A-Za-z]": { "type" ...

How React Utilizes Json Arrays to Add Data to a File Tree Multiple Times

For some reason, when attempting to add data to a file tree, it seems to duplicate the data sets. I'm not entirely sure why this is happening. Here's an example of the simple json array I am working with: export const treeDataFile = [{ type: & ...

Is there a way to ensure my circular image maintains its shape when viewed on mobile devices?

On my website, I have circular images implemented using the Bootstrap 4 class rounded-circle. While they appear perfectly round on desktop, they become oval-shaped on mobile devices such as the iPhone XR. You can see the issue in this Codepen link. Here i ...