简体   繁体   English

来自jQuery,如何在React.js中制作DOM动画? 如动画进度条?

[英]Coming from jQuery, how does one do DOM animations in React.js? Such as animate a progress Bar?

I have been using React for a couple months now and I came from using a lot of jQuery for DOM animations/manipulation. 我已经使用React几个月了,而我来自使用很多jQuery进行DOM动画/操作。 The manipulations I can handle pretty well but animating things and doing shiny UI stuff seems tricky in React. 我可以很好地处理这些操作,但是在React中动画处理和做闪亮的UI东西似乎很棘手。 Or at least, not as easy as jQuery. 或者至少不像jQuery那样简单。 I don't want to pull jQuery in for animations. 我不想将jQuery用于动画。

Some examples I am trying to achieve: 我正在尝试实现的一些示例:

  1. User scrolls down to my skills section of my Personal Portfolio Page where there are some Bootstrap progress bars. 用户向下滚动到“个人投资组合”页面的“我的技能”部分,其中有一些Bootstrap进度栏。 How would I animate these bars to 'fill up' once the user scrolls to them. 用户滚动到这些条后,我将如何对其进行动画处理以使其“填充”。 I imagine something with detecting position? 我想像有什么发现位置? I could also build my own progress bars so as to have more control. 我也可以建立自己的进度条,以便进行更多控制。

  2. When the user resizes my portfolio section that has a flexbox setup with images that represent my projects, I want the images to reposition themselves slowly, like a transition and not snap to their new spot. 当用户调整具有弹性框设置的我的投资组合部分的大小时,该部分具有代表我的项目的图像,我希望图像缓慢地重新定位自己,就像过渡一样,而不是捕捉到它们的新位置。

  3. Or something simple like have something slide in from the left/right? 还是简单的东西(例如,有东西从左/右滑入)?

I think jQuery spoiled me! 我认为jQuery宠坏了我! I just feel like I don't have as much as control in React. 我只是觉得我在React中没有太多控制权。 I have looked at Transition Groups but that seems pretty limited. 我看过过渡小组,但这似乎很有限。

Here is my code for the progress bar question... 这是我的进度条问题代码...

import React from 'react';

export default (props) => {
const width = `${props.lvl}%`

// setup diff colors for bars
let backgroundColor = (function(lvl){
    if (lvl <=35) {
        return '#ffa64d'
    }
    else if (lvl >= 36 && lvl <= 49) {
        return '#ffff66'
    }
    else {
        return '#47d147'
    }
}(props.lvl))

return (
    <div>
        <h4 style={{display: 'inline'}}>{props.name}</h4>
        <div className="progress">
            <div className='progress-bar' role="progressbar" aria-valuenow="70"
            aria-valuemin="0" aria-valuemax="100" style={{width, backgroundColor}}>
            </div>
        </div>
    </div>
)
}

I think most people have gotten their animations done through CSS transitions; 我认为大多数人都是通过CSS过渡来制作动画的。 it's by far the easiest way. 到目前为止,这是最简单的方法。 However, I have written a custom module that provides functionality that animates elements through manipulation of inline styles stored as part of component state. 但是,我编写了一个自定义模块,该模块提供了通过操纵作为组件状态一部分存储的内联样式来对元素进行动画处理的功能。 You can find it here . 你可以在这里找到它。 You'll have to judge for yourself whether the hassle is worth the functionality. 您必须自己判断麻烦是否值得该功能。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM