What is the best way to adjust CSS vw and vh values after resizing your website?

Utilizing vw and vh values in my CSS transform has resulted in the cloud image moving smoothly across the screen, from left to right, based on the actual window width.

While this works well for various window sizes initially, I have encountered an issue when resizing the window after the CSS has been loaded. Is there a way to dynamically recalculate the vw value post-resize?

Unfortunately, percentages are not a viable solution as they do not interact effectively with transform:translate.

cloud {
    animation: cloudanimation 2s linear infinite;
}

@keyframes cloudanimation {
  0% {
    transform: translateX(10vw);
  }

  100% {
    transform: translateX(90vw);
    /* percentages don't work correctly with translate */
    /* transform : translateX(90%);*/
  }
}

Answer №1

When passing relative values to animations, they are converted to absolute values. However, these values do not update when resizing;

An effective solution is to restart the animation when a resize event occurs;

Using jQuery:

$(window).resize(function() {
  //remove animation
  $('div').removeClass('cloud')

  //reset width 
  let w = $('div').outerWidth();
  $('div').outerWidth(w);

  //add animation class back
  $('div').addClass('cloud')
});

View demo here

To properly restart the animation, simply adding and removing the CSS class may not be sufficient. You also need to reset the width as explained here.


If your element can be displayed as inline-block, it appears to behave consistently without the need to add or remove animations. See the example here.

Answer №2

When the window is resized, VW and VH values are recalculated automatically.

transform: translateX(20vw);
transform: translateX(80vw);

If the issue persists, it might be originating from a different source. Visit this Fiddle link for further investigation.

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

Arranging divs in a row using jQuery sortable technique

Greetings! I have been attempting to achieve horizontal sorting for two divs but have encountered a couple of issues. Firstly, the divs do not align correctly upon initial page load and secondly, while they can be dragged around, the sortable functionality ...

How can I change the direction of the NgbModal so that it slides in from the right instead of the default top to bottom?

I'm currently using NgbModal and the Modal is opening from Top to Bottom by default. Is there a way to make it appear from right to left instead? I have already set up the positioning so that it appears in the right corner of the screen, but I can&apo ...

A guide on customizing Check Box borders using Dart

Is there a way to dynamically change the border color of a custom checkbox when an event occurs? Here is the HTML code snippet: <input id='B12' type='checkbox' class='checkbox' checked='checked'> ...

Is there a way to create animated image pixelation without relying on canvas, simply using CSS?

Pixelation can be achieved in CSS by following these steps: Set background-image to a very small image (50px or 100px). Apply image-rendering: pixelated on the element. This will give you the desired pixelated effect. Now, I am interested in animating ...

Effective HTML element placement with CSS styling

My HTML code includes a form containing a table. Take a look: This is the Code I'm Using: <html> <head></head> <body> <form onsubmit="" id="form1" action="" method="post" name="form1" style="position: rela ...

Is there a method available to transform HTML information into Json format and extract the text data from the HTML?

I have some data that looks like this. { "key" : "key1", "items" : [ { "date":"2022-04-26" , "html":"<tr>\n<th scope=\"row\">\n<span class=\"ico_age age_12\">AAAAA<\/span><a ...

Tips for aligning spin.js spinner in the center of a bootstrap 3 modal?

I'm attempting to showcase and center the spin.js spinner within a Bootstrap 3 modal. Despite successful implementation in IE and FF using the code below, I am encountering issues in Chrome, Safari Desktop, Chrome IOS, Safari IOS. The problem appears ...

Smooth scrolling in JavaScript can lead to unexpected jumps when using scroll-margin-top

I am currently working on implementing smooth scrolling into my website using JavaScript. However, I have run into a problem with CSS property scroll-margin-top causing a sudden jump at the end. The CSS I am using for scroll margins looks like this: class ...

Is there a way for me to have a table automatically scrolled to a specific column upon loading the HTML page?

My table contains a dynamic number of columns based on the months inputted from the database starting from a start_date and ending at an end_date. I have the current_date stored as a variable and want the table to load with the x-scrollbar positioned right ...

Determine if offcanvas element is visible upon initialization in Bootstrap 5 Offcanvas

I have a search-filter offcanvas div for location and property type on this webpage: On larger screens (desktop), the offcanvas is always visible, but on smaller screens (mobile), it is hidden and can be toggled with a button. This functionality works per ...

Is there a way to use JavaScript or jQuery to identify if Firefox is blocking mixed content

Is there a way to detect when Firefox (or any browser) is blocking mixed content using JavaScript or jQuery? To learn more about Firefox's mixed content blocking feature, visit: The backstory: My company has an external vendor that handles the onli ...

The text area is not increasing in size when using the " " for a new line

I have a code that enables Autocomplete input to add values chosen from the list to a textarea, with each value separated by a new line. While manually adding rows to the textarea by pressing Enter allows it to expand and fill in the new row and value, us ...

What is the best method for retrieving text that does not contain tags (such as text

My challenge involves storing headings and paragraphs into separate arrays, but I am struggling with handling text between <br>. Below is my HTML code and Python snippet: <p><strong>W Ognisku</strong><br>W londyńskim Ognisku ...

Redirecting files from the file system to the emulator is not functioning properly

I am new to Phonegap and need help resolving this issue. In my application, I need to redirect to the List.html file when a button is clicked. Here is the code I have written: button1 function test() { window.location="/home/swift-03/phonegapexa ...

When comparing the Raspberry command line editor 'Nano' to LeafPad for editing a text file string, it leads to issues where PHP cannot interpret the string accurately

Working on a Raspberry Pi, I noticed that when using Nano as the text editor to edit a text file, PHP files struggle with querying variables correctly in SQL. However, if I use the LeafPad editor that comes built-in with the Pi, the PHP file is able to out ...

The addition of a cancel swipe feature to an HTML5 eBook is causing the text input field for note-taking to malfunction

I am currently working on the development of a process to create HTML5 based eBooks for both desktop and mobile use using Adobe InDesign and exporting them with a plugin called In5. This plugin allows for the incorporation of html, css, and javascript duri ...

What is the best way to show only one instance of an item stored in a MySQL database?

I have been developing a search engine for a library that allows users to search for books. The search functionality is working well, but I have encountered an issue with books that have duplicated copies, which are available to multiple users. I want to ...

The group icon will dynamically change based on the selection made in the dropdown menu

I am currently working on a Bootstrap dropdown select for weather in my Razor Pages project. My goal is to have the input group icon change dynamically based on the selected weather option. For example, I want to display a cloudy icon when the user selects ...

Is it possible to change the color of specific days of the week (like Mondays, Tuesdays, etc.) in JQueryUI's datepicker?

Is it possible to customize the appearance of specific weekdays (such as all Mondays, all Tuesdays for the entire year) in the JQueryUI datepicker by changing their color? While there are existing methods to select certain dates and disable holidays, I h ...

What is the best way to showcase Json API content on an HTML page?

I possess a strong proficiency in html and css, however I lack experience in utilizing javascript. My aim is to showcase the date received from an API onto an html page Despite several attempts, I have not been able to achieve success yet. var getJSON ...