Search
 
SCRIPT & CODE EXAMPLE
 

CSS

animation reverse

<style>
        html {
            scroll-behavior: smooth
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: content-box;
        }

        :root {
            --main-color: #000;
        }

        body {
            background: var(--main-color);
            width: 100%;
            height: 100%;
        }

        @keyframes spin {
            0% {
                transform: rotateY(0deg);
            }

            100% {
                transform: rotateY(180deg);
            }
        }

        div {
            background: black;
            padding: 1rem;
            display: inline-block;
        }

        .item {
            /* because span cant be animated */
            display: block;
            color: yellow;
            font-size: 2rem;
        }

        .item.active {
            animation: spin 1s forwards;
            animation-timing-function: ease-in-out;
        }

        .item.in-active {
            animation-direction: reverse;
        }

    </style>
</head>

<body>
    <div>
        <span class="item">ABC</span>
    </div>
    
    <script>
        let item = document.querySelector('.item')

        // play normal
        item.addEventListener('mouseover', () => {
            item.classList.add('active')
        })

        // play in reverse
        item.addEventListener('mouseout', () => {
            item.style.opacity = 0 // avoid showing the init style while switching the 'active' class

            item.classList.add('in-active')
            item.classList.remove('active')

            // force dom update
            setTimeout(() => {
                item.classList.add('active')
                item.style.opacity = ''
            }, 5)

            item.addEventListener('animationend', onanimationend)
        })

        function onanimationend() {
            item.classList.remove('active', 'in-active')
            item.removeEventListener('animationend', onanimationend)
        }
    </script>
</body>
Comment

PREVIOUS NEXT
Code Example
Css :: css super smooth shadow 
Css :: background shorthand css 
Css :: table td data in middle 
Css :: select all class in css 
Css :: ul list style type image 
Css :: onclick jquery add css 
Css :: change style of ordered list numbers css 
Css :: html list over three columns 
Css :: responsive flexbox in css 
Css :: css bg code 
Css :: css center text 
Css :: css animation infinite 
Css :: css text properties 
Css :: how to middle fixed element 
Css :: css first element 
Css :: add more than 2 css jquery 
Css :: td wrap text without space 
Css :: create notification badge in css 
Css :: change angular material icon color 
Css :: How to horizontally center an element 
Css :: css select element by role 
Css :: edit hover with sass 
Css :: install code command on mac 
Css :: css position fixed center 
Css :: css user-select 
Css :: css text align right 
Css :: jest test try catch 
Css :: transparent button css 
Css :: css flex wrap space between rows 
Css :: how to move anything left in css 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =