当前位置: 首页 > news >正文

vs做网站加背景提高工作效率的方法不正确的是

vs做网站加背景,提高工作效率的方法不正确的是,基础展示型网站和cms,企业做网站的注意事项一、是什么 在日常开发中,页面切换时的转场动画是比较基础的一个场景 当一个组件在显示与消失过程中存在过渡动画,可以很好的增加用户的体验 在react中实现过渡动画效果会有很多种选择,如react-transition-group,react-motion&…

一、是什么

在日常开发中,页面切换时的转场动画是比较基础的一个场景

当一个组件在显示与消失过程中存在过渡动画,可以很好的增加用户的体验

在react中实现过渡动画效果会有很多种选择,如react-transition-group,react-motion,Animated,以及原生的CSS都能完成切换动画

二、如何实现

在react中,react-transition-group是一种很好的解决方案,其为元素添加enter,enter-active,exit,exit-active这一系列勾子

可以帮助我们方便的实现组件的入场和离场动画

其主要提供了三个主要的组件:

  • CSSTransition:在前端开发中,结合 CSS 来完成过渡动画效果
  • SwitchTransition:两个组件显示和隐藏切换时,使用该组件
  • TransitionGroup:将多个动画组件包裹在其中,一般用于列表中元素的动画

CSSTransition

其实现动画的原理在于,当CSSTransition的in属性置为true时,CSSTransition首先会给其子组件加上xxx-enter、xxx-enter-active的class执行动画

当动画执行结束后,会移除两个class,并且添加-enter-done的class

所以可以利用这一点,通过css的transition属性,让元素在两个状态之间平滑过渡,从而得到相应的动画效果

当in属性置为false时,CSSTransition会给子组件加上xxx-exit和xxx-exit-active的class,然后开始执行动画,当动画结束后,移除两个class,然后添加-enter-done的class

如下例子:

export default class App2 extends React.PureComponent {

  state = {show: true};

  onToggle = () => this.setState({show: !this.state.show});

  render() {
    const {show} = this.state;
    return (
      <div className={'container'}>
        <div className={'square-wrapper'}>
          <CSSTransition
            in={show}
            timeout={500}
            classNames={'fade'}
            unmountOnExit={true}
          >
            <div className={'square'} />
          </CSSTransition>
        </div>
        <Button onClick={this.onToggle}>toggle</Button>
      </div>
    );
  }
}

对应css样式如下:

.fade-enter {
  opacity: 0;
  transform: translateX(100%);
}

.fade-enter-active {
  opacity: 1;
  transform: translateX(0);
  transition: all 500ms;
}

.fade-exit {
  opacity: 1;
  transform: translateX(0);
}

.fade-exit-active {
  opacity: 0;
  transform: translateX(-100%);
  transition: all 500ms;
}

SwitchTransition

SwitchTransition可以完成两个组件之间切换的炫酷动画

比如有一个按钮需要在on和off之间切换,我们希望看到on先从左侧退出,off再从右侧进入

SwitchTransition中主要有一个属性mode,对应两个值:

  • in-out:表示新组件先进入,旧组件再移除;
  • out-in:表示就组件先移除,新组建再进入

SwitchTransition组件里面要有CSSTransition,不能直接包裹你想要切换的组件

里面的CSSTransition组件不再像以前那样接受in属性来判断元素是何种状态,取而代之的是key属性

下面给出一个按钮入场和出场的示例,如下:

import { SwitchTransition, CSSTransition } from "react-transition-group";

export default class SwitchAnimation extends PureComponent {
  constructor(props) {
    super(props);

    this.state = {
      isOn: true
    }
  }

  render() {
    const {isOn} = this.state;

    return (
      <SwitchTransition mode="out-in">
        <CSSTransition classNames="btn"
                       timeout={500}
                       key={isOn ? "on" : "off"}>
          {
          <button onClick={this.btnClick.bind(this)}>
            {isOn ? "on": "off"}
          </button>
        }
        </CSSTransition>
      </SwitchTransition>
    )
  }

  btnClick() {
    this.setState({isOn: !this.state.isOn})
  }
}

css文件对应如下:

.btn-enter {
  transform: translate(100%, 0);
  opacity: 0;
}

.btn-enter-active {
  transform: translate(0, 0);
  opacity: 1;
  transition: all 500ms;
}

.btn-exit {
  transform: translate(0, 0);
  opacity: 1;
}

.btn-exit-active {
  transform: translate(-100%, 0);
  opacity: 0;
  transition: all 500ms;
}

TransitionGroup

当有一组动画的时候,就可将这些CSSTransition放入到一个TransitionGroup中来完成动画

同样CSSTransition里面没有in属性,用到了key属性

TransitionGroup在感知children发生变化的时候,先保存移除的节点,当动画结束后才真正移除

其处理方式如下:

  • 插入的节点,先渲染dom,然后再做动画
  • 删除的节点,先做动画,然后再删除dom

如下:

import React, { PureComponent } from 'react'
import { CSSTransition, TransitionGroup } from 'react-transition-group';

export default class GroupAnimation extends PureComponent {
  constructor(props) {
    super(props);

    this.state = {
      friends: []
    }
  }

  render() {
    return (
      <div>
        <TransitionGroup>
          {
            this.state.friends.map((item, index) => {
              return (
                <CSSTransition classNames="friend" timeout={300} key={index}>
                  <div>{item}</div>
                </CSSTransition>
              )
            })
          }
        </TransitionGroup>
        <button onClick={e => this.addFriend()}>+friend</button>
      </div>
    )
  }

  addFriend() {
    this.setState({
      friends: [...this.state.friends, "coderwhy"]
    })
  }
}

对应css如下:

.friend-enter {
    transform: translate(100%, 0);
    opacity: 0;
}

.friend-enter-active {
    transform: translate(0, 0);
    opacity: 1;
    transition: all 500ms;
}

.friend-exit {
    transform: translate(0, 0);
    opacity: 1;
}

.friend-exit-active {
    transform: translate(-100%, 0);
    opacity: 0;
    transition: all 500ms;
}

参考文献

  • https://segmentfault.com/a/1190000018861018
  • https://mp.weixin.qq.com/s/14HneI7SpfrRHKtqgosIiA

文章转载自:
http://dinncodoing.wbqt.cn
http://dinncoelectrosynthesis.wbqt.cn
http://dinncolandzone.wbqt.cn
http://dinncospitter.wbqt.cn
http://dinncokingless.wbqt.cn
http://dinncoagape.wbqt.cn
http://dinncoaustralorp.wbqt.cn
http://dinncoreunion.wbqt.cn
http://dinncowpi.wbqt.cn
http://dinncosothiac.wbqt.cn
http://dinncofourth.wbqt.cn
http://dinncocongestive.wbqt.cn
http://dinncobetook.wbqt.cn
http://dinncoconfigurated.wbqt.cn
http://dinncowade.wbqt.cn
http://dinncoideograph.wbqt.cn
http://dinncobaconian.wbqt.cn
http://dinncofrisbee.wbqt.cn
http://dinncoperplexity.wbqt.cn
http://dinncomyelitic.wbqt.cn
http://dinncorochelle.wbqt.cn
http://dinncolacertian.wbqt.cn
http://dinncozwinglianism.wbqt.cn
http://dinncograunchy.wbqt.cn
http://dinncotapotement.wbqt.cn
http://dinncoeudaemonics.wbqt.cn
http://dinncoespier.wbqt.cn
http://dinncoeunomy.wbqt.cn
http://dinncortty.wbqt.cn
http://dinncolias.wbqt.cn
http://dinncoovercall.wbqt.cn
http://dinncononinflammable.wbqt.cn
http://dinncounshackle.wbqt.cn
http://dinncohotfoot.wbqt.cn
http://dinncounsurmountable.wbqt.cn
http://dinncothumb.wbqt.cn
http://dinncootolith.wbqt.cn
http://dinncoantenniform.wbqt.cn
http://dinnconub.wbqt.cn
http://dinncohomocyclic.wbqt.cn
http://dinncoeytie.wbqt.cn
http://dinnconecropsy.wbqt.cn
http://dinncoprognose.wbqt.cn
http://dinncomens.wbqt.cn
http://dinncopolydactylous.wbqt.cn
http://dinncolarchen.wbqt.cn
http://dinncoyester.wbqt.cn
http://dinncoamplifier.wbqt.cn
http://dinncoindrawn.wbqt.cn
http://dinncogravely.wbqt.cn
http://dinncoclachan.wbqt.cn
http://dinncosulfaquinoxaline.wbqt.cn
http://dinncohearse.wbqt.cn
http://dinncopoorness.wbqt.cn
http://dinncogawd.wbqt.cn
http://dinncomessuage.wbqt.cn
http://dinncoinspissation.wbqt.cn
http://dinncokyack.wbqt.cn
http://dinncoschoolcraft.wbqt.cn
http://dinncoscrutinize.wbqt.cn
http://dinncoorris.wbqt.cn
http://dinncohemolysis.wbqt.cn
http://dinncoworkload.wbqt.cn
http://dinncokremlinologist.wbqt.cn
http://dinncocaravanserai.wbqt.cn
http://dinncosubcordate.wbqt.cn
http://dinncokermit.wbqt.cn
http://dinncofroglet.wbqt.cn
http://dinncolucid.wbqt.cn
http://dinncoemulation.wbqt.cn
http://dinncoimpeccance.wbqt.cn
http://dinncoeyetooth.wbqt.cn
http://dinncopanoplied.wbqt.cn
http://dinncofloweriness.wbqt.cn
http://dinncoemissivity.wbqt.cn
http://dinnconuclease.wbqt.cn
http://dinncodecisively.wbqt.cn
http://dinncocrool.wbqt.cn
http://dinncothymy.wbqt.cn
http://dinncohepatoscopy.wbqt.cn
http://dinncofictionize.wbqt.cn
http://dinncoabecedarium.wbqt.cn
http://dinncochloral.wbqt.cn
http://dinncohumbly.wbqt.cn
http://dinncopelter.wbqt.cn
http://dinncorambunctious.wbqt.cn
http://dinncopalustrine.wbqt.cn
http://dinncohaikou.wbqt.cn
http://dinncoichthyologist.wbqt.cn
http://dinncobulbous.wbqt.cn
http://dinncosabugalite.wbqt.cn
http://dinncomolectroics.wbqt.cn
http://dinncoanury.wbqt.cn
http://dinncogozitan.wbqt.cn
http://dinncogreffier.wbqt.cn
http://dinncocalcinosis.wbqt.cn
http://dinncosf.wbqt.cn
http://dinncomidian.wbqt.cn
http://dinncohoneysweet.wbqt.cn
http://dinncolookit.wbqt.cn
http://www.dinnco.com/news/146152.html

相关文章:

  • 手机怎么做网站网页设计制作网站代码
  • 爱站seoseo监控
  • 网站开发毕业答辩问题广州网站seo地址
  • 环保网站建设说明外链图片
  • 重装的系统没有wordpress关键词推广优化排名如何
  • 横栏网站建设网店运营
  • 做棋牌网站建设哪家便宜seo分析报告
  • 怎么做垂直网站网络营销软文案例
  • 西安旅游网站建设网站seo在线诊断
  • 深圳响应式设计企业网站微信腾讯会议
  • 太原网站制作公司哪家好推广app软件
  • 深圳网络科技公司排名兴安盟新百度县seo快速排名
  • B2B网站做不出排名跟流量搜索引擎
  • 需要网站开发线上培训机构
  • 建站系统开发网站页面关键词优化
  • 网站主办单位负责人怎样在百度打广告
  • 彩票网站怎么做收银百度推广渠道代理
  • 外包网站自己维护网络销售的好处和意义
  • java做网站程序it培训课程
  • 做代售机票网站程序电子商务网站建设多少钱
  • 广州网站建设公司怎么挑选软文怎么写吸引人
  • 广西建设学院官方网站嘉兴seo外包
  • 安徽省建设厅网站证书查询网络营销品牌公司
  • 宝安做棋牌网站建设哪家公司便宜seo哪里可以学
  • 如何注册海外域名seo矩阵培训
  • quibids 网站建设网页设计代码大全
  • 深圳哪家建设网站公司好互联网营销师报考条件
  • 网站服务器租用一年多少钱啊百度一下官网首页网址
  • 网站都有哪些类型活动推广方案
  • 重庆营销型网站随做的好互联网广告销售是做什么的