How to implement animated TAB switching

php中世界最好的语言
Release: 2018-05-18 15:24:55
Original
2077 people have browsed it

This time I will show you how to implement animated TAB switching, what are the precautions for implementing animated TAB switching, the following is a practical case, let's take a look.

The designer gave a rendering of tab switching. Although it is a small function, front-end engineers still have many details to pay attention to when implementing it. I wrote a demo for your reference.

The final effect is as follows:

HTML5+CSS3 做一个灵动的动画 TAB 切换效果

In order for the gif animation to show details, I extended the animation time to 3 seconds

Implementation ideas

Interval vertical lines, because they are not vertical, so borders cannot be used. I'm going to use pseudo-elements.

There are only 3 vertical bars, but there are 4 li s. This is simple and can be selected using the :not(:first-child) selector.

The switching background color changes, because I want to have an effect from small to large, so I can’t directly use the background color to achieve it. I am also going to use pseudo elements to achieve it.

If the size of the pseudo element is used to control it, the calculation will be more complicated, so I want to use box-shadow shadow to achieve it.

Okay, that’s it. Let’s start writing the code, as follows:

HTML code

  <p class="m">
    <ul class="tab">
      <li><a href="">导航1</a></li>
      <li><a href="">导航2</a></li>
      <li><a href="">导航3</a></li>
      <li><a href="">导航4</a></li>
    </ul>
  </p>
Copy after login

The above code structure has been written before Yes, I think it’s ok, so I won’t make any adjustments. There is no cumbersome code.

CSS code

.m { margin: 100px; }
.tab { width: 400px; margin: 0 auto; border: 1px solid #ddd; height: 40px; text-align: center; line-height: 40px; background: #fff; border-radius: 10px; overflow: hidden; }
.tab li { float: left; width: 100px; position: relative; overflow: hidden; }
.tab li:before, .tab li:after, .tab li a { -webkit-transition: all 0.25s ease-in-out; transition: all 0.25s ease-in-out; }
.tab li:before, .tab li:after { content: ""; display: block; }
.tab li:not(:first-child):after { background: #ddd; height: 20px; width: 1px; left: 0; top: 10px; position: absolute; }
.tab li a { display: block; position: relative; z-index: 2; color: #000; font-size: 14px; }
.tab li:before { width: 0; height: 0; top: 50%; left: 50%; z-index: 1; position: absolute; }
.tab li:hover a { color: #fff; }
.tab li:hover:before { box-shadow: 0 0 0 100px #36bc99; }
.tab li:hover + li:after, .tab li:hover:after { height: 0; top: 20px; }
Copy after login

Code analysis:

Animation implementation is very simple, just use the transition attribute.

To control your own pseudo-elements and the pseudo-elements of the next sibling element, just use the + selector.

Other codes are relatively clear and simple, you can analyze them yourself.

It is very simple to achieve this effect. The focus is on daily accumulation and flexible matching of various parameters. Thinking of the implementation method and finally writing the code is very fast. And there is no high point of knowledge in it.

The reason why CSS is difficult is not that you don’t know how to do it, but that you don’t know how to match it.

Actually, only 99% of the design effect is restored. One of the two lines is inside the background and the other is outside the background. What should I do if I want to put both dividing lines inside the background? What about implementation? You can think about it.

Let’s use scss. The css above is compiled. In fact, it is very convenient and fast to implement it with scss, and the code is more readable.

The demonstration is as follows:

.m {
  margin: 100px;
}
.tab {
  width: 400px;margin: 0 auto;border: 1px solid $cdd;height: 40px;text-align: center;line-height: 40px;
  background: $cff;border-radius: 10px;overflow: hidden;
  li {
    float: left;width: 100px;position: relative;overflow: hidden;
    &:before,&:after,a {@include dz();}
    &:before,&:after {
      content: "";display: block;
    }
    &:not(:first-child) {
      &:after {
        background: $cdd;height: 20px;width: 1px;left: 0;top: 10px;position: absolute;
      }
    }
    a {
      display: block;position: relative;z-index: 2;color: $c00;font-size: 14px;
    }
    &:before {
      width: 0;height: 0;top: 50%;left: 50%;z-index: 1;position: absolute;
    }
    &:hover {
      a {color: $cff;}
      &:before {
        box-shadow: 0 0 0 100px $cyan;
      }
      & + li:after,&:after {
        height: 0;top: 20px;
      }
    }
  }
}
Copy after login

Of course, in this code, I used color variables and mixin to mix in the code. You cannot use it directly.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of pointer-events in css3

Detailed explanation of the use of focus-within

The above is the detailed content of How to implement animated TAB switching. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!