Instructions on how to smoothly transition an element's width from 2 seconds to its height, followed by a 50% increase in height upon clicking a button

I am facing an issue with my code where I am unable to achieve a 2s height transition as expected after clicking the "changer l'animation" button. Instead, there is initially a 2s width transition, and upon clicking the button, I should see a 50% increase in the logo's height with a 2s transition effect. I would appreciate any assistance in resolving this matter. Thank you!

function createPage() {

  // Inserting image into header
  var logo = document.createElement("img");
  logo.setAttribute("src", "Images/logo_js.jpg");
  logo.setAttribute("width", "175");
  logo.setAttribute("height", "110");
  logo.setAttribute("alt", "JavaScript Logo");
  var header = document.createElement("header");
  header.appendChild(logo);
  document.body.appendChild(header);

  // Navigation bar with list and links
  // Creating different elements

}

// More JavaScript functions handling CSS styling and animation effects are included here.

function animateImage() {
  var logo = document.getElementsByTagName("img");
  logo[0].style.transition = "height 2s";
  logo[0].style.WebkitTransition = "height 2s";

}
// HTML code and media queries for responsive design and layout are also provided in the original text.

Answer №1

Although I attempted to enhance guest271314's answer, it appears that he was already on the right track. Therefore, I will provide my own solution.

It seems like you are looking to create a mouseover effect where the image stretches in width for 2 seconds and then proceeds to stretch in height for another 2 seconds. Is my understanding correct?

<img id="cats" src="http://lorempixel.com/175/110/cats" width="175" height="110" />

<script>
  function animateImage() {
    var logo = document.getElementById("cats");
    logo.style.width = (logo.width * 1.5) + "px";
    logo.style.height = (logo.height * 1.5) + "px";
  }

  document.querySelector("#cats").onmouseover = animateImage;
</script>

<style>
   #cats {
      -webkit-transition: width 2s, height 2s linear 2s;
      transition: width 2s, height 2s linear 2s;
   }
</style>

Answer №2

transition is in place, but there is no adjustment to the height property. A solution would be to update the height by adding half of the current .height value within the animerImage() function.

<button>click</button>
<img src="http://lorempixel.com/175/110/cats" width="175" height="110"/>
<script>

function animerImage() {
  var logo = document.getElementsByTagName("img");
  logo[0].style.transition = "height 2s";
  logo[0].style.WebkitTransition = "height 2s";
  var height = logo[0].height / 2;
  logo[0].style.height = logo[0].height + height + "px";
}

document.querySelector("button").onclick = animerImage;
</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

sending Ruby arrays to jQuery code in a Haml template within the Ruby on Rails framework

Implementing a Highcharts graph on a haml template using jQuery and javascript is my current task. Here's an example showcasing the standalone graph from the demo code on the HighCharts site: :javascript $(document).ready(function() { $("#tabs ...

Breaking down the initial state within a redux reducer

Can you explain the distinction between returning state in the default case of a Redux reducer using return state and return { ...state } ? ...

When users click on the next/link, it is triggering a problem within the getInitialProps function

Currently, I am attempting to include a link that will direct users to the create page. However, when I tried clicking on the link, I encountered the following error: TypeError: Cannot read properties of undefined (reading 'cookies') The code fo ...

Can an image be coded to follow the cursor once it reaches the image itself?

I'm in the process of developing a game where an image follows the cursor. I want to prevent players from cheating by moving the cursor outside the designated area and then reaching the finish line. Currently, the image moves wherever my cursor goes. ...

ObtainComputedStyle yields surprising CSS properties

Can anyone help me figure out why I'm not able to access a CSS rule's value correctly using getComputedStyle? Here's what I'm dealing with: background: linear-gradient(to right, red , yellow); However, the result I'm getting is d ...

Extracting data from a database using PEAR for Excel: Enumerating columns

One aspect of my website allows users to extract an Excel file using data from a database, with the help of the PEAR library. The Excel table that is generated currently has 32 columns. My main goal is to insert a 33rd column that assigns a number startin ...

Display various components using a dropdown selection

I am seeking guidance on how to display different components based on the selected option. I am unsure of how to write the code for displaying either component one or two. Can you provide any examples or references that may help? <template> <div ...

After rendering express, Node.js establishes a connection to a TCP socket

I am currently working on a project involving a node.js web server, a TCP client, and a Java TCP server, all for the purpose of studying socket and web programming. The idea is that a user connects to a webpage, inputs some information, Node.js sends this ...

What is the process for updating a value within a JSON file?

While utilizing axios, I am receiving a JSON file as a response. Axios automatically converts this JSON file to an object. Here is an example of how it appears: let newMessage{ channel:"WHATSAPP", messages:"[{"message_type":"text","message_con ...

New Relic identifies mysterious delays caused by MongoDB's findOne method

After setting up newrelic to pinpoint the bottlenecks in my app, I discovered a major issue that has left me stumped. The source of most delays seems to be mongoDB user.findOne, but the biggest challenge is locating where in the code this delay is occurri ...

The Material UI dialog box popped up against an unexpected gray backdrop

Currently, I am using Material UI in conjunction with React to create a dialog that appears when a button is tapped. This button is located within a table, which is displayed over a Paper component. The problem arises when I utilize the dialog with its def ...

What is the best way to set the value of the days in a month to a div

I've been experimenting with creating a calendar using javascript, HTML, and CSS on my own. I managed to achieve some functionality like obtaining the current month and year as well as the previous month and year by clicking a button in HTML. However, ...

CSS Selector for table cell containing a checked input in the cell before it

Is it possible to select a table cell based on the state of a previous cell using CSS? I have a feeling that this may not be achievable with CSS alone. Can someone confirm my suspicion? Let's consider this example: <table> <tr> ...

Variable used in jQuery/ajax click eventIncorporating a

Looking to build a basic webpage with jQuery/Ajax functionality? I have 3 buttons, each performing an ajax call to retrieve different files from the server. The buttons are differentiated by their class names and the files they request. However, all other ...

The combination of Node.js module.exports and shorthand ternary operators for conditional statements

Can you explain the purpose of this line 'undefined' != typeof User ? User : module.exports and why is everything enclosed within (function(){})? I am having trouble understanding its significance. This code snippet is extracted from a library f ...

Are there any notifications triggered when a draggable element is taken out of a droppable zone?

Within a single droppable area, there is a collection of individual Field Names that can be dragged, and a table featuring X headers, each of which can be dropped into. Initially, these headers are empty. Is there a way to detect when an item is taken out ...

The absence of a type annotation for `T` has been noted

Attempting to create code with a simple JavaScript function: filterArray(user: any, items: Array<Object>) { items = items.filter(item => {return true;}); return items; } Encountering the following error: Error message: Missing type anno ...

JQuery submitting a partial view form displays an empty page along with a JSON response

I am working on an Edit function in MVC4 public ActionResult Edit(UserGroup usergroup) { usergroup.created_date = DateTime.Now; usergroup.modified_date = DateTime.Now; db.Entry(usergroup).State = EntityState.Mod ...

Want to know how to center the value of a slider range input in your React project using NextJS and Tailwind

Can someone help me with the issue I'm having with the offset? Is there a way to link a value to the thumb? I've tried two methods, but one gives a significant offset and the other gives less. However, it seems more like a workaround than a solu ...

Trouble with AJAX BitMovin: unable to process GET request

I've been struggling to find a solution to my problem as I cannot pinpoint what exactly to search for. My current issue involves using AJAX for a PHP Get request, causing it to submit and navigate away from the page. The BitMovin library appears to b ...