Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable CSS transitions on window resize?

Tags:

jquery

css

I'm surprised I can't find any solutions to this. I've got four square divs which are in a line at full-screen, in a 2x2 grid at mobile size. They change colour with a CSS transition on click.

But when I resize my window, they float about the page into their new positions - an unwanted side effect of the CSS transition. I've tried using JQuery to toggle the transitions while resizing, which keeps them from floating, but there's a 50% chance that the transition class will be inactive when you stop resizing!

I've not put any code in here as it seems like such a simple problem. Does anybody have any simple solutions?

like image 260
JohnG Avatar asked Jul 22 '16 12:07

JohnG


People also ask

How do you stop a transition in CSS?

To pause an element's transition, use getComputedStyle and getPropertyValue at the point in the transition you want to pause it. Then set those CSS properties of that element equal to those values you just got.

What triggers CSS transition?

Triggering transitions You can trigger CSS transitions directly with pseudo classes like :hover (activates when the mouse goes over an element), :focus (activates when a user tabs onto an element, or when a user clicks into an input element), or :active (activates when user clicks on the element).

How do you control transition speed in CSS?

Specify the Speed Curve of the Transition ease - specifies a transition effect with a slow start, then fast, then end slowly (this is default) linear - specifies a transition effect with the same speed from start to end. ease-in - specifies a transition effect with a slow start.

What is CSS transition property?

The transition-property property specifies the name of the CSS property the transition effect is for (the transition effect will start when the specified CSS property changes). Tip: A transition effect could typically occur when a user hover over an element.


1 Answers

When the window resizing, we can add to < body > a special CSS-class "stop-transitions". After resizing and short timeout we removed this class. Put this before closing tag < body >:

<script>
    (function() { 
        const classes = document.body.classList;
        let timer = 0;
        window.addEventListener('resize', function () {
            if (timer) {
                clearTimeout(timer);
                timer = null;
            }
            else
                classes.add('stop-transitions');

            timer = setTimeout(() => {
                classes.remove('stop-transitions');
                timer = null;
            }, 100);
        });
    })();
</script>

And add next to your CSS. It resets the transition property for all during the resize. After resize and short timeout this class will removed and the transition works again.

body.stop-transitions * {
  transition: none !important;
}
like image 147
Dmitry Shashurov Avatar answered Sep 25 '22 09:09

Dmitry Shashurov