简体   繁体   English

动画反应选项卡式导航栏

[英]Animate react tabbed navigation bar

I want to create a nice sliding animation when a user clicks to another tab.当用户单击另一个选项卡时,我想创建一个漂亮的滑动动画。 I am using React and React router.我正在使用 React 和 React 路由器。 Im using flexbox to make the tabs dynamically fill up the space of the container, since different users will see a different number of tabs depending on what they are permissioned to see.我使用 flexbox 使标签动态填充容器的空间,因为不同的用户将看到不同数量的标签,这取决于他们被允许看到的内容。 How can I add animations when I don't know the number of tabs there will be?当我不知道标签的数量时,如何添加动画?

Im looking for an effect similar to this.我正在寻找与此类似的效果。 https://codepen.io/burntcustard/pen/PwGgJz https://codepen.io/burntcustard/pen/PwGgJz

Here is my React Tab component:这是我的 React Tab 组件:

return (
  <div className={STYLES.tabbedNavbar}>
    { tabs.map( (tab, i) => {
      return (
        <div className={STYLES.container}>
          <Link
            key={i}
            to={tab.routePath}
            className={STYLES.tab}
            activeClassName={STYLES.activeTab}
            target={tab.newTab ? "_blank" : ""}>
            <div className={STYLES.label} onClick={this.handleClick}> { tab.label } </div>
          </Link>
           <div className={STYLES.slider} ref={ ref => this.slider = ref }></div>
        </div>
      );

Here is my sass for the React component:这是我对 React 组件的 sass:

.tabbedNavbar
  display: flex
  justify-content: space-between
  width: 100%
  border-bottom: 2px solid $medium-gray
  line-height: 16px
  margin: 20px 0 30px 0

.tab
  min-width: 105px
  text-align: center
  color: $header-gray
  &:hover
    color: $header-gray
  &:focus
    text-decoration: none
    color: $header-gray

.activeTab
  color: $base-green
  border-bottom: 4px solid $base-green
  margin-bottom: -2px
  &:hover
    color: $base-green
  &:focus
    color: $base-green
    text-decoration: none

One way would be to, onClick, get the left position of the tab relative to the container.一种方法是,onClick,获取选项卡相对于容器的left位置。 Then, set left on your slider to the same value.然后,将滑块上的left设置为相同的值。 Should work with however many tabs you have.无论您拥有多少标签,都应该使用。 Make sure to add a transition: position 500ms ease to your slider to make the movement smooth.确保添加一个transition: position 500ms ease到您的滑块以使移动平滑。

EDIT:编辑:

Quick and dirty JSFiddle example快速而肮脏的 JSFiddle 示例

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

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