Animate the jQuery: Move the image up and down inside a smaller height container with hidden overflow

Check out the fiddle to see the animation in action!

The image is programmed to ascend until its bottom aligns with the bottom of the div, and then descend until its top aligns with the top edge of its parent div, revealing the image in the process.

This functionality is designed to accommodate images of various sizes.

$(document).ready(function() {
  move();
});

function move() {
  $(".image").animate({
    bottom: "-=50%"
  }, 10000, 'linear', function() {
    $(".image").animate({
      bottom: "+=50%"
    }, 10000, 'linear', move);
  });
}
.container {
  position: relative;
  width: 1100px;
  height: 480px;
  overflow: hidden;
  background-color: #000;
}

.image {
  position: absolute;
  max-width: 100%;
  min-width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div class="container">
  <img class="image" src="http://www.hotel-aramis.com/slider/home/notre-dame-de-paris.jpg />
    </div>

Answer №1

Here is a code snippet that appears to be effective:

$(document).ready(function () {
  startMove();
});

function startMove() {
  $img = $(".image");
  var image = new Image();
  image.src = $img.attr('src');

  var diff = image.height - $('.container').height();

  $img.animate({
    bottom: "+="+diff+'px'
  }, 10000, 'linear', function () {
    $img.animate({
        bottom: "-="+diff+'px'
    }, 10000, 'linear', startMove);
  });
}

This technique involves creating a new JavaScript image Object (not jQuery) with the same src as the selected image, using its natural height for the animation. It appears that using a jQuery object for this purpose may cause discrepancies in height, possibly due to needing to be attached to the DOM or a similar reason.

Jsfiddle link: http://jsfiddle.net/VqzvR/8/

By moving the setup code outside of the startMove() function, the HTTP GET request for the image source will only be executed once:

$(document).ready(function () {
  setupAnimation();
});

function setupAnimation() {
  $img = $(".image");
  var image = new Image();
  image.src = $img.attr('src');

  var diff = image.height - $('.container').height();

  function startMove() {
    $img.animate({
        bottom: "+="+diff+'px'
    }, 10000, 'linear', function () {
        $img.animate({
            bottom: "-="+diff+'px'
        }, 10000, 'linear', startMove);
    });
  }
  startMove();
}

Does this achieve the desired effect you were aiming for?

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

Using database queries to populate a series of interconnected drop-down menus

Currently, I am facing an issue with my 2 drop-down menus. The first one is populated with possible continents, and the second should display countries based on the continent selected in the first menu. However, all countries are showing up in the dropdown ...

The date error from day.js in Firefox is not valid

My date is formatted as 2022-01-27 09:23:48 UTC and I am trying to parse it into MMMM-DD-YYYY format (Jan-27-2022) using day.js. The parsing works well in Chrome, but Firefox returns an 'Invalid' result. import dayjs from "dayjs" const ...

Failed to fetch user id from server using Ajax request

I am facing an issue with my form that includes a text input field and a button. After submitting the form, I extract the value from the user field and send an ajax request to ajax.php to retrieve the userID from the server. The server does return a value, ...

Display a list next to a block of text and image using HTML and CSS styling

I am having trouble with keeping a navigation list aligned inline with a block of text that includes an image. My issue arises when I expand the browser to full screen size, causing the list to overlap the text block. Here is the HTML code snippet: <bo ...

What is the process of developing a straightforward Node.js script to encapsulate a binary or executable file?

I am looking to develop a Node.js file that can seamlessly execute an executable binary file and handle all inputs and outputs, specifically targeting Windows operating systems. Reason for this: I aim to install an executable tool via npm install -g in or ...

ajax and codeigniter combination for selecting options

Is it possible to implement a set_select option within an AJAX component for a dynamic dependent dropdown list that is being cleared after validation errors? I am looking to incorporate the set_select functionality in the code snippet below: <script ty ...

Angular and Bootstrap project with an advanced dropdown menu featuring multiple levels

Looking to create a multi-level drop-down menu using TypeScript without relying on jQuery? Bootstrap CSS framework may not have exactly what you need. https://i.sstatic.net/iruev.png Wondering how to implement a multi-level dropdown in your Angular proje ...

Unraveling nested JSON structures with varying formats in Javascript or Jquery: Step-by-step guide

var cart = [ { "Items": "", "category": "", "contents": [ { "Apple iPhone": "222", "French": "Bounjour", "id": 1234, "icon": "/images/bg.jpg", "callback": "use()", "pricetag":"false" } ] }, { "Items": "No 2" ...

Displaying the Yii form directly on the page upon loading, rather than enclosed within a jQuery dialog box

After studying this yii wiki page, I encountered an issue where the form is supposed to appear within a jQuery dialog box, but it opens immediately when the page loads instead. Upon further investigation, I discovered that removing the success callback fr ...

"Implementing a full page refresh when interacting with a map using

Here is an example of how I display a map on the entire page: <div id="map_canvas"> </div> UPDATE 2: I have successfully displayed a menu on the map, but there is a problem. When I click on the button, the menu appears briefly and then disapp ...

Is the CSS3 bar chart flipped?

I'm currently developing a responsive bar chart, and everything seems to be going smoothly except for one issue - when viewing it in full screen, the bars appear to be flipped upside down. It's quite puzzling as all other elements such as text an ...

Issues with exporting data to Excel in WEB API are causing problems and need to

I am having some issues with exporting JQGrid data to Excel from my WEB API controller. I'm trying to handle it through a controller method and everything seems to be working fine, but the Excel file is not showing up properly. <input type="image" ...

What is the method for retrieving a specific value following the submission of an AngularJS form?

Here is how my form begins: <!-- form --> <form ng-submit="form.submit()" class="form-horizontal" role="form" style="margin-top: 15px;"> <div class="form-group"> <label for="inputUrl" class="col-sm- ...

Angular is throwing an error because it cannot find the definition for

Embarking on a tutorial journey to dive into Angular setup. Facing a persistent error in my code... Error: [$injector:undef] http://errors.angularjs.org/1.6.0/$injector/undef?p0=Bear Listing the order of files within the <head> tag in the html ...

iOS does not support Css3 Transition

My Transition Code works on PC and android devices, but it does not work on iOS devices. I am only using HTML and CSS. /***** BOX 3 *****/ #box3 { height:240px; width:198px; border:1px solid #dedede; background-color:#fcfcfc; position:relative; overflow:h ...

style for a bootstrap form input

Can the form-control from Bootstrap be made inline for input without relying on the grid system like col-md-2? This is the one aspect of Bootstrap that I find particularly frustrating. ...

Is it possible to trigger the alert message by utilizing this code snippet?

Is it possible to display a message using this function? var start_database = function() { alert('hello'); }; window.setTimeout(start_database, 1000); ...

Issue with jQuery getScript() not resolving

My current approach involves using jQuery's getScript() method to load individual javascript files. The loading process appears to be successful, as I receive an error when using an incorrect URL and no errors with the correct one. However, I am enco ...

Having trouble with the scrollTop function in your Rails application?

I recently delved into using jQuery and have implemented a two-column layout with Twitter Bootstrap. The CSS in my file looks like this: html, body { overflow: hidden; height: 100%; } .span8, .span4 { overflow: auto; } This setup results ...

When using Node.js, Express.js, and MongoDB, ensure that the route.post() function is provided with proper callback functions instead of

I've been working on setting up a MEAN (MongoDB, Express, Node.js, Angular 6) application. I'm trying to post user signup form data to a MongoDB database, but I keep getting an error message. This is my first time working with the MEAN stack and ...