• CSS3之两面翻转的盒子案例


    目录

                    第一步:不多说,首先我们先来一个框架

                    第二步:添加两个盒子

                    第三步:这里的话我们先给最大盒子添加一下样式

                    第四步:我们给两个div小盒子添加一下样式

                    第五步:添加背景颜色

                    第七步:调整代码

                    第八步:制作动画

                    第九步:优化


     

     

                    第一步:不多说,首先我们先来一个框架

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. head>
    7. <body>
    8. body>
    9. html>

                    第二步:添加两个盒子

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. head>
    7. <body>
    8. <div class="bodys">
    9. <div class="first">我是翻转前div>
    10. <div class="two">我是翻转后div>
    11. div>
    12. body>
    13. html>

            第三步:这里的话我们先给最大盒子添加一下样式

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. head>
    7. <style>
    8. #bodys {
    9. position: relative;
    10. width: 350px;
    11. height: 350px;
    12. transform-style: preserve-3d; /*这个属性就是让其出现翻转效果的核心*/
    13. margin: 200px auto;
    14. }
    15. style>
    16. <body>
    17. <div id="bodys">
    18. <div class="first">我是翻转前div>
    19. <div class="two">我是翻转后div>
    20. div>
    21. body>
    22. html>

    这里的话我给大合照加了一个宽高,和外边距主要是改变一下位置,方便我们看变化,其中

     transform-style: preserve-3d;属性

            第四步:我们给两个div小盒子添加一下样式

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. head>
    7. <style>
    8. #bodys {
    9. position: relative;
    10. width: 350px;
    11. height: 350px;
    12. transform-style: preserve-3d;
    13. /*这个属性就是让其出现翻转效果的核心*/
    14. margin: 200px auto;
    15. }
    16. .first,
    17. .two {
    18. position: absolute;
    19. top: 0;
    20. left: 0;
    21. width: 100%;
    22. height: 100%;
    23. text-align: center;
    24. line-height: 300px;
    25. color: aqua;
    26. border-radius: 50%;
    27. font-size: 20px;
    28. }
    29. style>
    30. <body>
    31. <div id="bodys">
    32. <div class="first">我是翻转前div>
    33. <div class="two">我是翻转后div>
    34. div>
    35. body>
    36. html>

     

           这里出现这样的原因是因为我们还没有添加背景颜色,现在给添加上 

            第五步:添加背景颜色

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. head>
    7. <style>
    8. #bodys {
    9. position: relative;
    10. width: 350px;
    11. height: 350px;
    12. transform-style: preserve-3d;
    13. /*这个属性就是让其出现翻转效果的核心*/
    14. margin: 200px auto;
    15. }
    16. .first,
    17. .two {
    18. position: absolute;
    19. top: 0;
    20. left: 0;
    21. width: 100%;
    22. height: 100%;
    23. font-size: 30px;
    24. text-align: center;
    25. line-height: 300px;
    26. color: aqua;
    27. border-radius: 50%;
    28. }
    29. .first {
    30. background-color: chocolate;
    31. }
    32. .two {
    33. background-color: crimson;
    34. }
    35. style>
    36. <body>
    37. <div id="bodys">
    38. <div class="first">我是翻转前div>
    39. <div class="two">我是翻转后div>
    40. div>
    41. body>
    42. html>

     

     大家可以发现,虽然说我们的动画还没开始动,但现在是不是应该就相当于动画做好后,反翻转前,所以是不是应该显示的是翻转前的圆?这里我们就要用到z-index

            第七步:调整代码

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. head>
    7. <style>
    8. #bodys {
    9. position: relative;
    10. width: 350px;
    11. height: 350px;
    12. transform-style: preserve-3d;
    13. /*这个属性就是让其出现翻转效果的核心*/
    14. margin: 200px auto;
    15. }
    16. .first,
    17. .two {
    18. position: absolute;
    19. top: 0;
    20. left: 0;
    21. width: 100%;
    22. height: 100%;
    23. font-size: 30px;
    24. text-align: center;
    25. line-height: 300px;
    26. color: aqua;
    27. border-radius: 50%;
    28. }
    29. .first {
    30. background-color: chocolate;
    31. z-index:999;
    32. }
    33. .two {
    34. background-color: crimson;
    35. }
    36. style>
    37. <body>
    38. <div id="bodys">
    39. <div class="first">我是翻转前div>
    40. <div class="two">我是翻转后div>
    41. div>
    42. body>
    43. html>

     

     这样就达到了我们想要的效果,那么准备工作都做好了,所以我们现在就开始做动画啦

            第八步:制作动画

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. head>
    7. <style>
    8. #bodys {
    9. position: relative;
    10. width: 350px;
    11. height: 350px;
    12. /*这个属性就是让其出现翻转效果的核心*/
    13. margin: 200px auto;
    14. transform-style: preserve-3d;
    15. transition: all 0.5s;
    16. }
    17. #bodys:hover {
    18. transform: rotateY(180deg);
    19. }
    20. .first,
    21. .two {
    22. position: absolute;
    23. top: 0;
    24. left: 0;
    25. width: 100%;
    26. height: 100%;
    27. font-size: 30px;
    28. text-align: center;
    29. line-height: 300px;
    30. color: aqua;
    31. border-radius: 50%;
    32. }
    33. .first {
    34. background-color: chocolate;
    35. z-index: 999;
    36. }
    37. .two {
    38. background-color: crimson;
    39. }
    40. style>
    41. <body>
    42. <div id="bodys">
    43. <div class="first">我是翻转前div>
    44. <div class="two">我是翻转后div>
    45. div>
    46. body>
    47. html>

     这里我们给#bodys添加了一个transform: rotateY(180deg); 目前的话我们已经可以初步实现翻转了,但背景色和文字还没有改变,所以我们要继续添加

            第九步:优化

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. head>
    7. <style>
    8. #bodys {
    9. position: relative;
    10. width: 350px;
    11. height: 350px;
    12. /*这个属性就是让其出现翻转效果的核心*/
    13. margin: 200px auto;
    14. transform-style: preserve-3d;
    15. transition: all 0.5s;
    16. }
    17. #bodys:hover {
    18. transform: rotateY(180deg);
    19. }
    20. .first,
    21. .two {
    22. position: absolute;
    23. top: 0;
    24. left: 0;
    25. width: 100%;
    26. height: 100%;
    27. font-size: 30px;
    28. text-align: center;
    29. line-height: 300px;
    30. color: aqua;
    31. border-radius: 50%;
    32. }
    33. .first {
    34. background-color: chocolate;
    35. z-index: 999;
    36. }
    37. .two {
    38. background-color: crimson;
    39. transform: rotateY(180deg);
    40. }
    41. style>
    42. <body>
    43. <div id="bodys">
    44. <div class="first">我是翻转前div>
    45. <div class="two">我是翻转后div>
    46. div>
    47. body>
    48. html>

     这里的话我们给。two添加了一个翻转,原因是因为前面的代码实现的效果都是.first的内容,所以我们想要。two实现,就要给他加一个翻转,这里是个人理解,初步接触这一块,不确定一定对

             下方附上全部代码

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. head>
    7. <style>
    8. #bodys {
    9. position: relative;
    10. width: 350px;
    11. height: 350px;
    12. /*这个属性就是让其出现翻转效果的核心*/
    13. margin: 200px auto;
    14. transform-style: preserve-3d;
    15. transition: all 0.5s;
    16. }
    17. #bodys:hover {
    18. transform: rotateY(180deg);
    19. }
    20. .first,
    21. .two {
    22. position: absolute;
    23. top: 0;
    24. left: 0;
    25. width: 100%;
    26. height: 100%;
    27. font-size: 30px;
    28. text-align: center;
    29. line-height: 300px;
    30. color: aqua;
    31. border-radius: 50%;
    32. }
    33. .first {
    34. background-color: chocolate;
    35. z-index: 999;
    36. }
    37. .two {
    38. background-color: crimson;
    39. transform: rotateY(180deg);
    40. }
    41. style>
    42. <body>
    43. <div id="bodys">
    44. <div class="first">我是翻转前div>
    45. <div class="two">我是翻转后div>
    46. div>
    47. body>
    48. html>

  • 相关阅读:
    使用Go和Node.js构建用静态脚本进行数据解析的TCP服务器
    08.23递归 以及python算法(快排,冒泡,选择)
    LVS集群
    【Linux】程序地址空间
    第三阶段第一章——PySpark实战
    Mysql-Xtrabackup备份恢复
    【Rust日报】2022-12-05 探索 docker 的 WASM 技术预览
    The ‘<‘ operator is reserved for future use. 错误解决
    shell脚本入门
    vue2.6 + ts 使用vuex
  • 原文地址:https://blog.csdn.net/tea_tea_/article/details/125897494