Issue with prolonged CSS transition duration affecting width and position changes

Suppose we need a transition for width and position.

However, using a long duration can result in unexpected behavior. The timing function will reset from zero for the remainder of the operation.

    #test {
      border: solid 1px;
      width: 100px;
      height: 50px;
      transition: width 10s linear;
    }
    #test:hover {
      width: 1000px;
      transition: width 10s linear;
    }
<div id="test"></div>

If you rehover the div at any point during the transition, it will take 10s to transition over 500px, not the original settings. Additionally, it will take 10s for the last 10px.

Is there any workaround for this issue or should I consider using the jQuery animate function?

Answer №1

Here's a workaround that involves writing a script for the animation process. In essence, you need to calculate the time needed to complete the remaining width.

This function can be applied to any element and you can also specify the desired width as an input parameter.

$("#demo").on("mouseover", function(){
  var $elem = $(this);  
  var remainingTime = 10000 - $elem.width()*10; // Calculating remaining time for animation

  $elem.stop().animate({"width": "1000px"}, remainingTime, "linear");
});

$("#demo").on("mouseout", function(){
  var $elem = $(this);
  var remainingTime = $elem.width()*10; // Calculating remaining time for animation
  
  $elem.stop().animate({"width": "100px"}, remainingTime, "linear");
});
#demo {
  border: solid 1px;
  width: 100px;
  height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div id="demo"></div>

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

What is the best way to zip two arrays of different lengths so that they can be looped through in a Django

Two arrays are at my disposal: First array: ['Tan','Goh','Tio'] Second array: [['Honda','Toyota','Proton'],['Toyota'],['Proton','Lambo']] Is there a way to merge ...

difficulty with displaying the following image using jquery

I have referenced this site http://jsfiddle.net/8FMsH/1/ //html $(".rightArrow").on('click',function(){ imageClicked.closest('.images .os').next().find('img').trigger('click'); }); However, the code is not working ...

Javascript event fails to trigger following completion of Ajax request

I'm encountering an issue with my form (input, textarea, and submit button) and a list of items. I've written a script that should: Display the text content of a list item in the textarea when clicked Submit the form via ajax without refreshing ...

Transforming the jQuery mobile application navbar into an Android apk menu

Recently, I transformed a jQuery mobile site into an Android app using PhoneGap. However, I've encountered an issue where the navigation bar/menu on Android is truncating the menu words. For example, "What we do" turns into "What we...". Do you have ...

Prevent selecting the same option in Laravel after it has been chosen once

I am working on a project that involves a form for users to fill out an application. Within this form, there is a table that displays the user's language skills. Additionally, I have a seeder in the project that provides all the available languages in ...

Extract each line of text from the ul li tags at every level of the hierarchy with the help of

I am looking to extract a list of strings from the ul and li elements with slash (/) separating each level up to the nth level using JavaScript. I attempted to use both the Jquery.map and each functions, but unfortunately, I was unsuccessful. ...

Parsing a Table in Python with HTMLParser

I am trying to convert an HTML table into a 2D array (rows and columns) using only the HTMLParser library in Python, without relying on BeautifulSoup or other non-standard libraries. This task is part of a personal project that I am working on for fun :) ...

Reorganize items that extend beyond the list into the next column using Bootstrap

I have a row with two columns, where Column 1 currently contains 7 list items under the ul tag. However, I want to display only 5 list items in Column 1 and automatically move the remaining items to the next column (i.e., Column 2). Is there a way to ach ...

Jquery JavaScript functions are compatible with Chrome and Firefox browsers but do not function properly in Internet Explorer

I am encountering an issue with my HTML file that includes CSS and Jquery. It works perfectly on Chrome and Firefox, but when tested on IE, only the CSS part functions while the JavaScript click function does not work at all. $(document).ready(function(){ ...

Creating iPhone style form elements using HTML

Thinking of replicating the form elements from iPhone using HTML/CSS but wondering if it already exists out there. I searched online but couldn't find anything. Has anyone come across something similar before? ...

Error encountered in ASP.NET Core MVC due to dependency inversion with CRUD operations

Today I delved into dependency inversion. Prior to implementing this concept, my CRUD system was functioning well, but now I am encountering some errors. The error message says "Cannot implicitly convert type int to ContractLayer.UsersDto." In the image pr ...

What is the best way to align the navigation list to the right in Bootstrap v4alpha6?

Recently, I discovered that with the alpha 6 version of bootstrap 4, they have made some changes to utilize flexbox which has caused some issues in my existing code from alpha 5. After checking out this fiddle, I attempted to use the flex justify content c ...

A Guide to Retrieving the First Child Element's Text with Jquery and a Class Selector

I want to extract the text from the first child small element inside a dynamically generated div. The structure looks like this: <div class="NDateCount" style="display:inline-block"> <!--Issue to solve--> <small ...

Retrieve custom attributes of child elements using jQuery selectors

I am looking to modify the data-answersetid attribute of all child controls under the .cloneable class in a table. Each row in the table contains a single control - either a textarea, checkbox, radio button, or input[type=number]. Each control has a custom ...

Retrieve information from ngResource and then connect the output to the display

I am currently working on a project using AngularJS. At the moment, I am using ngResource to retrieve JSON data from a static file, with plans to potentially switch to a database in the future. In my approach, I have set up a controller to call the servic ...

What is the best way to locate the text within a <div><span>text</span></div> tag using Beautiful Soup?

This block of code contains HTML elements: <div><div id="NhsjLK"> <li class="EditableListItem NavListItem FollowersNavItem NavItem not_removable"> <a href="/profile/Dileep-Sankhla/followers">Followers <span class="list_count"> ...

What's the best way to align a bottom navigation bar in the center?

Hey there! I recently added a bottom navigation bar to my website using HTML and CSS code. However, I'm having trouble centering it at the bottom of the page. Any suggestions or solutions would be greatly appreciated. Attached are screenshots of the H ...

Updating the color of text when clicking on a link using javascript

I am facing an issue where I cannot seem to change the text color using JavaScript. The functionality to show and hide div tags is working perfectly, but changing the color seems to be a challenge. It's worth noting that there are no styles defined fo ...

Fluid layout causing elements in body to be hidden by fixed header

I'm currently tackling a fluid layout challenge, utilizing percentages instead of pixels to achieve it. I've managed to create a fixed heading (#heading) at the top of the page, but now I'm struggling with ensuring that all other elements on ...

Discover the best way to enable lint support for MUI `sx` prop values

Despite attempting the method below, I am still unable to receive lint support to identify incorrect style properties and values. import { SxProps, Theme } from "@mui/material"; export const navBarStyles: Record<string, SxProps<Theme> | ...