简体   繁体   English

如何间隔更改样式组件的styles?

[英]How to change styles of a styled component at an interval?

So I've got to make a sliding-to-the-left text for a website and I didn't complicate much, just took a codepen from Google and tried to implement it.... except that Codepen uses classic CSS and I'm using styled components.所以我必须为一个网站制作一个向左滑动的文本,我并没有太复杂,只是从谷歌拿了一个 codepen 并尝试实现它....除了 Codepen 使用经典的 CSS 和我'正在使用样式化的组件。 Basically, the code changes the class (which contains the style) of a div that contains the text at a set interval and together with the css animations we have a sliding text (I'll leave both the link and the code).基本上,代码更改了包含设置间隔文本的 div 的 class (其中包含样式),并且与 css 动画一起,我们有一个滑动文本(我将留下链接和代码)。 I know how styled components work, at a basic level ( const Button = styled.button.. ).我知道样式组件是如何工作的,在基本级别( const Button = styled.button.. )。 What I don't know, is how to change that style... the original pen works by changing the class of the component, I can't do that...我不知道的是如何改变那种风格......原来的笔是通过改变组件的class来工作的,我不能这样做......

PS: There is no need for the text to change. PS:文字无需更改。 I'll just leave the default one.我将保留默认的。

Link: CodePen Code:链接: CodePen代码:

 var title = ['<p>Every new beginning comes from some other beginning s end.</p>','<p>Even the genius asks questions.</p>','<p>It s better to burn out, than to fade away.</p>']; var index = 0; function change_title() { var x = title[index]; $('.main').html(x); index++; if (index >= title.length) { index = 0; } }; function change_left() { $('div').removeClass('slide-right').addClass('slide-left'); } function change_right() { $('div').removeClass('slide-left').addClass('slide-right'); change_title(); } function to_left() { setInterval(change_left, 10000); }; function to_right() { setInterval(change_right, 20000); }; to_left(); to_right();
 body { background-color: #add8e6; } h1 { text-align: center; margin: 2.5em;} div.main { width:90%; overflow:hidden; background-color: #2F5168; margin: 2.5em auto; height: auto; border: 2px solid gray; padding: 1.2em 0; font-size: 1.5em; font-family: "Agency FB"; color: #E4F6F8; } div.slide-right p { -moz-animation: 10s slide-right; -webkit-animation: 10s slide-right; -o-animation: 10s slide-right; animation: 10s slide-right; margin:0; } div.slide-left p { -moz-animation: 10s slide-left; -webkit-animation: 10s slide-left; -o-animation: 10s slide-left; animation: 10s slide-left; margin:0; } @-webkit-keyframes slide-right { from { margin-left: 100%;width: 300%; } to {margin-left: 0%;width: 100%;} } @-moz-keyframes slide-right { from { margin-left: 100%;width: 300%; } to {margin-left: 0%;width: 100%;} } @-o-keyframes slide-right { from { margin-left: 100%;width: 300%; } to {margin-left: 0%;width: 100%;} } @keyframes slide-right { from { margin-left: 100%;width: 300%; } to {margin-left: 0%;width: 100%;} } @-webkit-keyframes slide-left { from {margin-left: 0%;width: 100%;} to {margin-left: -100%;width: 300%;} } @-moz-keyframes slide-left { from {margin-left: 0%;width: 100%;} to {margin-left: -100%;width: 300%;} } @-o-keyframes slide-left { from {margin-left: 0%;width: 100%;} to {margin-left: -100%;width: 300%;} } @keyframes slide-left { from {margin-left: 0%;width: 100%;} to {margin-left: -100%;width: 300%;} }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="main slide-right"> <p>It s better to burn out, than to fade away.</p> </div> <h1>An infinite loop of sliding text right to left</h1>

My code:我的代码:

import React from 'react';
import styled from 'styled-components';

const HeroContainer = styled.div`
    width: 100%;
    background-color: #fdf3eb;
    padding: 60px 0px 60px 0px;
`

const HeroBannerText = styled.h1`
    -moz-animation: 10s slide-right;
    -webkit-animation: 10s slide-right;
    -o-animation: 10s slide-right;
    animation: 10s slide-right;
    margin:0;
`

class HeroBanner extends React.Component{
    changeLeft = () => {
        HeroBannerText
    }

    render(){
        return(
            <HeroContainer>
                <HeroBannerText>We'll Unsplash you with images all day long.</HeroBannerText>
            </HeroContainer>
        );
    }
}

export default HeroBanner;

import React from 'react'
import styled, { createGlobalStyle } from 'styled-components'
import Navbar from './components/Navbar'
import HeroBanner from './components/HeroBanner'

const GlobalStyle = createGlobalStyle`
  @font-face{
    font-family: robotoRegular;
    src: url(./fonts/Roboto-Regular.ttf);
  }

  @-webkit-keyframes slide-right { from { margin-left: 100%;width: 300%; }
  to {margin-left: 0%;width: 100%;} }
  @-moz-keyframes slide-right { from { margin-left: 100%;width: 300%; }
  to {margin-left: 0%;width: 100%;} }
  @-o-keyframes slide-right { from { margin-left: 100%;width: 300%; }
  to {margin-left: 0%;width: 100%;} }
  @keyframes slide-right { from { margin-left: 100%;width: 300%; }
  to {margin-left: 0%;width: 100%;} }

  @-webkit-keyframes slide-left { from {margin-left: 0%;width: 100%;}
  to {margin-left: -100%;width: 300%;} }
  @-moz-keyframes slide-left { from {margin-left: 0%;width: 100%;}
  to {margin-left: -100%;width: 300%;} }
  @-o-keyframes slide-left { from {margin-left: 0%;width: 100%;}
  to {margin-left: -100%;width: 300%;} }
  @keyframes slide-left { from {margin-left: 0%;width: 100%;}
  to {margin-left: -100%;width: 300%;} }

  * {
    box-sizing: border-box;
  }

  body {
    margin: 0;
    padding: 0;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
`;

function App() {
  return (
    <>
      <GlobalStyle/>
      <div>
        <Navbar/>
        <HeroBanner/>
      </div>
    </>
  );
}

export default App;

A simple approach to achieve your functionality is to use Passed props and state hooks to dynamically change styles.实现功能的一种简单方法是使用Passed 道具和 state 挂钩来动态更改 styles。 For more complicated logic you could take a look at Theming .对于更复杂的逻辑,您可以查看Theming Example:例子:

const Colored = styled.p`
    color: ${props => props.color}
`

function Component() {
    const [color, setColor] = useState('red');
    
    return (
        <>
            <Colored>Some text here</Colored>
            <Button onClick = {() => setColor('blue')}>Change color to blue</Button>
        </>
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过在 useState 挂钩的帮助下单击组件来更改样式组件 styles? - How to change styled-component styles by clicking on component with help of useState hook? React - 如何使用样式组件向组件添加新样式 - React - How to add new styles to a component using styled-component React Styled组件:如何基于道具添加CSS样式? - React Styled component: how to add css styles based on props? 如何将样式对象作为道具传递给样式组件? - How to spread styles object passed down to a styled component as a prop? 如何使用 styled-components 继承和更改组件 styles - How to inherit and change a components styles with styled-components 如何在 react 和 next js 中使用 Styled 组件创建全局 styles,使用 css 文件和 styled 组件的组合是否更好? - How to Create global styles with Styled components in react and next js and is it better to use combination of css files and styled component? 如何动态更改样式组件的样式? - How to change style of styled-component dynamically? 样式化(组件)不适用于功能组件 styles - styled(Component) does not apply styles on functional component 样式化的组件不会覆盖其他组件的样式 - Styled components not overriding styles from another component 带自定义组件的样式化组件不适用 styles - styled-components with custom component not applying styles
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM