Pit of transition and display in CSS3

Source: Internet
Author: User

Do not know people do CSS3 animation when you have encountered this situation?

Use opacity to achieve fading effects. Oh! good! All normal to a chestnut:

<!DOCTYPE html> 
    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21st
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
Code laycode-v1.1

Or with animation, write in Keyframe. You don't write code here.

But inevitably we will encounter such demand, also will encounter: we all know opacity:0 time, in fact, he still occupies the page space, he will obscure the layer below him (not visual). And some of the onclick events that bind to him also happen. At this time we want to opacity:0, hide him, with Display:none. Well, let's just add a few words to the CSS and become

.div1{     background: red;}.div2 {     width:100px;     height:100px;     background:blue;     transition:opacity 2s;     -moz-transition:opacity 2s; /* Firefox 4 */     -webkit-transition:opacity 2s; /* Safari and Chrome */     -o-transition:opacity 2s; /* Opera */     opacity: 0;     display: none;} .div1:hover .div2 {     opacity:1;     
    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21st
Code laycode-v1.1

Open the browser to see the effect first. I rub .... Fade in and out of the effect is all gone ... This is simply a destructive effect, which is also the big pits that were encountered during the animation process

The smart yards certainly have a way to solve this situation. We all know that the effect of visibility is in fact similar to display to a certain extent, certainly to some extent. Then why is it only a certain extent similar, because it is still occupied space, then this time crossing will say, so use with opacity no difference

But the smart yards must think again, at this time, he will not obscure to the following layer, that is, he and opacity such a different existence, onclick and other events will not happen.

For us, the next visibility, commonly used is the visible and hidden two values. But it can also be a number, equal to 0 when the equivalent of hidden, and as long as more than 0 o'clock, visibility is visible. Then we take advantage of this.

.div1{     background: red;}.div2 {     width:100px;     height:100px;     background:blue;     transition:all 2s;     -moz-transition:all 2s; /* Firefox 4 */     -webkit-transition:all 2s; /* Safari and Chrome */     -o-transition:all 2s; /* Opera */     visibility: hidden} .div1:hover .div2 {     
    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
Code laycode-v1.1

Browsing, you will find that this time there will be a delay hidden effect. Well, I found something new.

What about the effect of the gradient ....

The smart yards were reminded of opacity ... How do you use it together?

.div1{     background: red;}.div2 {     width:100px;     height:100px;     background:blue;     transition:all 2s;     -moz-transition:all 2s; /* Firefox 4 */     -webkit-transition:all 2s; /* Safari and Chrome */     -o-transition:all 2s; /* Opera */     visibility: hidden;     opacity: 0} .div1:hover .div2 {     visibility: visible;     
    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21st
Code laycode-v1.1

Browse First ...!!!!! Everything's fine.

In fact, visibility will be applied after opacity comes into effect. For example, when hiding, the first opacity to 0, and then apply the Visibility:hidden this time is hidden, related events will not happen. Looks like Display:none was abandoned at this time.

Of course, some people will say Visibility:hidden always occupy the page space, display can reduce the page repaint, or to use display:none how to do it

The smart yards think again: turn display into a block and delay the execution of transition animations

Of course, it's going to need JS.

These are the pits found in display and transition ...!

Pit of transition and display in CSS3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.