Guide to moving an image to the center of the screen one second after the page has finished loading

I have a query! There is an image at the bottom of the screen when my website is accessed on a mobile phone. I would like the duck in the picture to be automatically scrolled up slightly after 1 second of page load, so it appears right in the middle of the screen.

However, I am facing two issues as I am relatively new to web programming: 1. How can I ensure the duck image appears exactly in the middle? 2. How do I make the duck scroll to the center after one second of the page loading?

Thank you for taking the time to read my question and any help you provide regarding the above questions would be greatly appreciated.

window.onload = function(){
   $('html,body').animate({scrollTop:600},1000);
}
body{
  width: 100%;
  height: 1200px;
  display: flex;
  justify-content:center;
  align-items:center;
}
#js-duck{
  display: inline-block;
  width:200px;
  height: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img id="js-duck" src="https://upload.cc/i1/2022/02/15/UB1kXd.png" alt="duck">

Answer №1

If you need to execute code with a delay, the setTimeout() function is your friend;

...
setTimeout(function() {
   // Your delayed code goes here
}, 
1000); // Time in seconds before execution (1000 = 1s)
...

When it comes to centering an element, set its display property to inline (or whatever it already is) and add text-align: center; to the parent element. As for vertical alignment, I rely on an older method, so I'll defer to someone else for that.

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

When there is no content in the responseText, AJAX will display an error

I've encountered an issue while trying to fetch data using AJAX. The problem lies in receiving an empty responseText. Here's the code I'm working with: JavaScript: function getFounder(id) { var founder = ""; $.ajax({ ...

Is there a way to extract an object id from JSON data in Javascript?

I'm currently utilizing the Alpha Vantage API for retrieving stock market information. All data is provided in a specific format: To visualize this data effectively, I need to compile an array or object containing all the dates. Unfortunately, these ...

Creating Visuals from a Sophisticated DOM Structure (With Embedded iframes)

I am developing a unique application (as an owner) that has similarities to Canva but is not a direct competitor. In my application, the "canvas" is created using standard DOM elements instead of the HTML canvas element. Currently, I am exploring ways to e ...

Footer panel in Bootstrap not aligning to the bottom of the page

I'm experiencing an issue with my footer getting stuck in the middle of the page. Strangely, this problem only occurs on one specific page, and not on any other pages. I've tried various methods to fix it, but none of them have been successful. ...

Unable to get compatibility mode IE to work as intended

I am facing an issue where I need to showcase a webpage in compatibility mode. Despite using the code below: <meta http-equiv="X-UA-Compatible" content="IE=11"> It seems like this code only functions if compatibility mode is already enabled. In tha ...

Is it possible to search for a date within a string using jQuery?

Here's a unique challenge I'm facing with my to-do list app - users can quickly add tasks without filling out a detailed form. The catch? I need to find dates in their text entries, which are typically in YYYY-MM-DD format. For instance, if they ...

Tips for handling a CURL POST request in Express.JS without specifying the Content-Type

I'm encountering an issue with the data extraction process after sending a CURL request to my node.js server. The output format appears strange and I'm unsure how to parse it. CURL Request: curl -X POST -d '{ "url": "http:/ ...

Unable to save JSON file

I am encountering an issue with a JSON file I have. The JSON file contains weather data for Munich, including temperature and wind speed information. weather = [ { "city": "Munich", "temp": { "temp_val": "30 deg", ...

I am in search of assistance to update this Array State in order to display it as table data

For what seems like an eternity, I've been grappling with this issue that's definitely testing my patience! The goal is simple - create a dropdown list populated with data from a JSON file, allow users to select an item which will then be added t ...

Executing an external Python script within a Vue application's terminal locally

Hello, I am new to using Vue.js and Firebase. Currently, I am working on creating a user interface for a network intrusion detection system with Vue.js. I have developed a Python script that allows me to send the terminal output to Firebase. Right now, I a ...

Connect your Flask/Dash application to a customized Bootstrap stylesheet stored locally

I have created a Flask/Dash application and I am currently linking it to an external bootstrap stylesheet as shown below: dash_app = dash.Dash(server=server, routes_pathname_prefix='/dashapp/', ...

What sets apart the URLs recognized by Express.js from the regular ones we typically encounter?

What sets a URL recognizable to Express.js apart from the typical URLs we encounter? For instance: If I send a GET request like this, Express.js will identify and parse this URL (http://localhost:3000/things/0) where id=0: app.get('/things/:id', ...

Execute functions in a random order

My array contains a series of functions and is structured as shown below: var all_questions = [ show_question(1, 1), show_question(2, 1), show_question(3, 1), ]; I'm interested in executing those functions within the array in a random or ...

An easy guide to automatically refreshing a div with a seamless animation

I am working on an HTML page where I want to smoothly refresh a specific div every 60 seconds. What I have in mind is a simple process: fade out the div / refresh the div / fade in the div However, despite reading extensively about jQuery and AJAX for r ...

Can a div be relocated within another div based on a random roll of the dice?

Hey there, I'm currently working on creating a simple Monopoly-style game. As someone who is pretty new to JavaScript, I'm looking to figure out how to move the game piece around the board based on dice rolls. Any guidance or assistance would be ...

Is there a way to use HTML and JS to draw custom lines connecting elements?

Sorry if this question has been asked before. Is there a way to create straight lines connecting HTML elements with just HTML and JavaScript, without relying on SVG or Canvas? If so, what would be the most effective approach? ...

What method can I use in AngularJS to have a factory function reference another factory function?

As an illustration, consider the code snippet below where I want func2 to invoke func1. App.factory("MyLib", function() { return { func1: function() { console.log("func1"); }, func2: function() { func1(); } } }); Is t ...

``If you're looking to retrieve, modify, and display information in your Vue application with the help of

I'm facing an issue where I am trying to retrieve data using the axios get request and then updating it based on the response of another axios get request. However, I am unable to display the data from the second request. The following is a snippet o ...

What method can be used to incorporate Google Places API into a .js file without using a <script> element?

As I delve into the Google Places API documentation, I noticed that they require the API be loaded in this format: <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script> Is ...

Utilizing the js-yaml library to parse a YAML document

Currently, I'm utilizing js-yaml to analyze and extract the data from a yaml file in node js. The yaml file consists of key-value pairs, with some keys having values formatted like this: key : {{ val1 }} {{ val2 }} However, the parsing process enco ...