如何在JSX中编写CSS的关键帧(@keyframes)。

huangapple go评论67阅读模式
英文:

How to write the key frame (@keyframes) of css in jsx

问题

My project utilizes the Vue3 framework, which is built using Vite.

When I encapsulate the loading component, I want to incorporate the template and style from a .vue file into a JSX file.

Here is the template:

const myLoad = {
    props: ['msg'],
    render(ctx) {
        const { $props } = ctx
        return (
            <div class="loading">
                <div>
                    <span></span>
                    <p>{$props.msg}</p>
                </div>
            </div>
        )
    }
}

and I use styils/vue to add CSS in JSX (You can also use another CSS-in-JS library), but I met a problem. In the original CSS, I used keyframes to write animations:

@keyframes spin {
    to {
        transform: rotateZ(360deg);
    }
}
span {
    display: block;
    width: 60px;
    height: 60px;
    border: 3px solid transparent;
    border-top-color: #fff;
    border-bottom-color: #fff;
    border-radius: 50%;
    animation: spin ease 1000ms infinite;
}

You can write the CSS in JSX like this:

import { css } from '@styils/vue';

const styles = css`
    @keyframes spin {
        to {
            transform: rotateZ(360deg);
        }
    }

    span {
        display: block;
        width: 60px;
        height: 60px;
        border: 3px solid transparent;
        border-top-color: #fff;
        border-bottom-color: #fff;
        border-radius: 50%;
        animation: spin ease 1000ms infinite;
    }
`;

// Add the 'styles' to your JSX component

Here is the loading.vue:

<template>
    <div class="loading">
        <div>
            <span></span>
            <p>{{ msg }}</p>
        </div>
    </div>
</template>

<script setup>
defineProps({
    msg: {
        type: String
    }
});
</script>

<style lang="scss">
.loading {
    top: 0;
    z-index: 101;
    height: 100%;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    position: fixed;
    display: flex;
    justify-content: center;
    align-items: center;

    /* Include the keyframes and span styles here */
}

</style>

Make sure to include the styles CSS definition in your JSX component to make the animation work.

英文:

My project utilizes the Vue3 framework, which is built using Vite.

When I encapsulate the loading component, I want to incorporate the template and style from a .vue file into a JSX file.

Here is the template:

const myLoad = {
    props: [&#39;msg&#39;],
    render(ctx) {
        const { $props } = ctx
        return (
            &lt;div class=&quot;loading&quot;&gt;
                &lt;div&gt;
                    &lt;span&gt;&lt;/span&gt;
                    &lt;p&gt;{ $props.msg }&lt;/p&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        )
    }
}

and I use styils/vue add css in jsx (You can also use another CSS-in-JS library), but i met a problem, in the original css I used keyframes to write animations:

 @keyframes spin {
        to {
            transform: rotateZ(360deg);
        }
    }
span {
        display: block;
        width: 60px;
        height: 60px;
        border: 3px solid transparent;
        border-top-color: #fff;
        border-bottom-color: #fff;
        border-radius: 50%;
        animation: spin ease 1000ms infinite;
    }

How should I write in jsx...

Here is the loading.vue:

&lt;template&gt;
    &lt;div class=&quot;loading&quot;&gt;
        &lt;div&gt;
            &lt;span&gt;&lt;/span&gt;
            &lt;p&gt;{{ msg }}&lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/template&gt;

  
&lt;script setup&gt;
defineProps({
    msg: {
        type: String
    }
});
&lt;/script&gt;

&lt;style lang=&quot;scss&quot; &gt;
.loading {
    top: 0;
    z-index: 101;
    height: 100%;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    position: fixed;
    display: flex;
    justify-content: center;
    align-items: center;

    @keyframes spin {
        to {
            transform: rotateZ(360deg);
        }
    }

    span {
        display: block;
        width: 60px;
        height: 60px;
        border: 3px solid transparent;
        border-top-color: #fff;
        border-bottom-color: #fff;
        border-radius: 50%;
        animation: spin ease 1000ms infinite;
    }

    p {
        color: #fff;
        margin-top: 20px;
    }
}
&lt;/style&gt; 


I want the animation is work

答案1

得分: 1

const Foo = styled('div', {
'@keyframes superAnimation': {
'11.1%': {
opacity: '0.9999'
},
'111%': {
opacity: '1'
}
}
})

// or

import { keyframes } from '@styils/[frame]'

const out = keyframes({
from: {
transform: 'rotate(0deg)'
},
to: {
transform: 'rotate(360deg)'
}
})

const Foo = styled('div', {
animation: 13s ease 1.5s infinite none running ${out}
})

英文:
const Foo = styled(&#39;div&#39;,{
  &#39;@keyframes superAnimation&#39;: {
    &#39;11.1%&#39;: {
      opacity: &#39;0.9999&#39;
    },
    &#39;111%&#39;: {
      opacity: &#39;1&#39;
    }
  }
})
// or
import { keyframes } from &#39;@styils/[frame]&#39;

const out = keyframes({
  from: {
    transform: &#39;rotate(0deg)&#39;
  },
  to: {
    transform: &#39;rotate(360deg)&#39;
  }
})

const Foo = styled(&#39;div&#39;,{
  animation: `13s ease 1.5s infinite none running ${out}`
})

huangapple
  • 本文由 发表于 2023年6月29日 17:21:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定