经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
CSS3变形透视动画-3 -cyy
来源:cnblogs  作者:陈莺莺呀  时间:2020/11/16 10:08:14  对本文有异议

perspective透视规则与函数不同应用体验:

perspective加在父元素和加在子元素的区别:

加在父元素上是整体观察所有子元素,加在子元素上是独立观察子元素;

 

perspective规则和transform:perspective()函数的区别:
perspective加在父元素上,不影响父元素本身;
transform:perspective()函数会影响父元素本身
 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6.  
  7. <style>
  8. *{
  9. margin:0;
  10. padding:0;
  11. box-sizing:border-box;
  12. }
  13. body{
  14. width:100vw;
  15. height:100vh;
  16. background:#3848a2;
  17. display:flex;
  18. justify-content:center;
  19. align-items:center;
  20. }
  21. main{
  22. display:flex;
  23. /* perspective加在父元素上,就是整体观察所有子元素 */
  24. /* 只影响整体子元素,不影响父元素本身 */
  25. perspective:900px;
  26. border:3px solid red;
  27. /* 会影响父元素本身 */
  28. transform:perspective(600px) rotateY(45deg);
  29. }
  30. div{
  31. width:200px;
  32. height:200px;
  33. margin-right:100px;
  34. /* perspective(透视距离),透视距离越小,透视越明显 */
  35. /* transform:perspective(),独立观察子元素 */
  36. transform:perspective(900px) rotateY(45deg);
  37. }
  38. div:nth-child(1){
  39. background:pink;
  40. }
  41. div:nth-child(2){
  42. background:lightblue;
  43. }
  44. main:hover div:nth-child(1){
  45. }
  46. main:hover div:nth-child(2){
  47. transform:scaleX(2);
  48. transform:scaleY(.5);
  49. transform:scale(2,.5);
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <main>
  55. <div></div>
  56. <div></div>
  57. </main>
  58. </body>
  59. </html>

 

 

preserve-3d呈现三维空间视角:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6.  
  7. <style>
  8. *{
  9. margin:0;
  10. padding:0;
  11. box-sizing:border-box;
  12. }
  13. body{
  14. width:100vw;
  15. height:100vh;
  16. background:#3848a2;
  17. display:flex;
  18. justify-content:center;
  19. align-items:center;
  20. }
  21. main{
  22. display:flex;
  23. border:3px solid red;
  24. width:400px;
  25. height:300px;
  26. /* 设置3d效果 */
  27. transform-style:preserve-3d;
  28. /* 给父元素加上透视和适当旋转,才能看到z轴的变化 */
  29. transform:perspective(900px) rotateY(45deg);
  30.  
  31. }
  32. div{
  33. width:200px;
  34. height:200px;
  35. margin-right:100px;
  36. position:absolute;
  37. }
  38. div:nth-child(1){
  39. background:pink;
  40. }
  41. div:nth-child(2){
  42. background:lightblue;
  43.  
  44. /* 三维空间需要操作z轴 */
  45. transform:translateZ(300px);
  46. }
  47. main:hover div:nth-child(1){
  48. }
  49. main:hover div:nth-child(2){
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <main>
  55. <div></div>
  56. <div></div>
  57. </main>
  58. </body>
  59. </html>

 

 

3D旋转图集:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6.  
  7. <style>
  8. *{
  9. margin:0;
  10. padding:0;
  11. box-sizing:border-box;
  12. }
  13. body{
  14. width:100vw;
  15. height:100vh;
  16. background:#3848a2;
  17. display:flex;
  18. justify-content:center;
  19. align-items:center;
  20. }
  21. main{
  22. display:flex;
  23. border:3px solid red;
  24. width:200px;
  25. height:200px;
  26. transform-style:preserve-3d;
  27. transform:perspective(900px) rotateX(-45deg);
  28. transition:1s;
  29. transform-origin:center center -200px;
  30.  
  31. }
  32. article{
  33. width:400px;
  34. height:400px;
  35. border:1px solid red;
  36. display:flex;
  37. justify-content:center;
  38. align-items:center;
  39. }
  40. article:hover main{
  41. transform:perspective(900px) rotateX(-45deg) rotateY(555deg);
  42.  
  43. }
  44. div{
  45. width:200px;
  46. height:200px;
  47. position:absolute;
  48. overflow:hidden;
  49. transform-origin:center center -200px;
  50.  
  51. }
  52. div>img{
  53. height:100%;
  54. }
  55. div:nth-child(1){
  56. transform:rotateY(90deg);
  57. }
  58. div:nth-child(2){
  59. transform:rotateY(180deg);
  60. }
  61. div:nth-child(3){
  62. transform:rotateY(270deg);
  63. }
  64. div:nth-child(4){
  65. transform:rotateY(360deg);
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <article>
  71. <main>
  72. <div><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605069498580&di=557f5cf3f788d1761e7b992aefeffc99&imgtype=0&src=http%3A%2F%2Fww2.sinaimg.cn%2Fthumb150%2F6f807ba3gw1f4v0m2ll0bj20ku0ku77i.jpg" alt=""></div>
  73. <div><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605069499855&di=4526f815cf47b8fc29e2120fcd8f2144&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn23%2F400%2Fw600h600%2F20181023%2F2f0e-hmuuiyw6041112.jpg" alt=""></div>
  74. <div><img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2809686336,965929022&fm=26&gp=0.jpg" alt=""></div>
  75. <div><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605069504608&di=e26f4483ed372b10a6e87626616620c1&imgtype=0&src=http%3A%2F%2Fpic.5577.com%2Fup%2F2017-7%2F15010420363446897.jpg%2521360_360" alt=""></div>
  76. </main>
  77. </article>
  78. </body>
  79. </html>

 

还有一个平面版本:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6.  
  7. <style>
  8. *{
  9. margin:0;
  10. padding:0;
  11. box-sizing:border-box;
  12. }
  13. body{
  14. width:100vw;
  15. height:100vh;
  16. background:#3848a2;
  17. display:flex;
  18. justify-content:center;
  19. align-items:center;
  20. }
  21. main{
  22. display:flex;
  23. border:3px solid red;
  24. width:200px;
  25. height:200px;
  26. transform-style:preserve-3d;
  27. transform:perspective(900px);
  28. transition:1s;
  29. transform-origin:center center -200px;
  30.  
  31. }
  32. article{
  33. width:400px;
  34. height:400px;
  35. border:1px solid red;
  36. display:flex;
  37. justify-content:center;
  38. align-items:center;
  39. }
  40. article:hover main{
  41. transform:perspective(900px) rotateY(90deg);
  42.  
  43. }
  44. div{
  45. width:200px;
  46. height:200px;
  47. position:absolute;
  48. overflow:hidden;
  49. transform-origin:center center -200px;
  50.  
  51. }
  52. div>img{
  53. height:100%;
  54. }
  55. div:nth-child(1){
  56. transform:rotateY(90deg);
  57. }
  58. div:nth-child(2){
  59. transform:rotateY(180deg);
  60. }
  61. div:nth-child(3){
  62. transform:rotateY(270deg);
  63. }
  64. div:nth-child(4){
  65. transform:rotateY(360deg);
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <article>
  71. <main>
  72. <div><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605069498580&di=557f5cf3f788d1761e7b992aefeffc99&imgtype=0&src=http%3A%2F%2Fww2.sinaimg.cn%2Fthumb150%2F6f807ba3gw1f4v0m2ll0bj20ku0ku77i.jpg" alt=""></div>
  73. <div><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605069499855&di=4526f815cf47b8fc29e2120fcd8f2144&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn23%2F400%2Fw600h600%2F20181023%2F2f0e-hmuuiyw6041112.jpg" alt=""></div>
  74. <div><img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2809686336,965929022&fm=26&gp=0.jpg" alt=""></div>
  75. <div><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605069504608&di=e26f4483ed372b10a6e87626616620c1&imgtype=0&src=http%3A%2F%2Fpic.5577.com%2Fup%2F2017-7%2F15010420363446897.jpg%2521360_360" alt=""></div>
  76. </main>
  77. </article>
  78. </body>
  79. </html>

 

perspective-origin 调整观看视角实例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6.  
  7. <style>
  8. *{
  9. margin:0;
  10. padding:0;
  11. box-sizing:border-box;
  12. }
  13. body{
  14. width:100vw;
  15. height:100vh;
  16. background:#3848a2;
  17. display:flex;
  18. justify-content:center;
  19. align-items:center;
  20. }
  21. main{
  22. display:flex;
  23. border:3px solid red;
  24. width:400px;
  25. height:300px;
  26. transform-style:preserve-3d;
  27. perspective:900px;
  28. transition:2s;
  29.  
  30. }
  31. body:hover main{
  32. /* 物体本身移动 */
  33. /* transform:rotateY(60deg); */
  34.  
  35. /* 视角移动 */
  36. perspective-origin:center left;
  37. perspective-origin:2000px -300px;
  38. }
  39. div{
  40. width:200px;
  41. height:200px;
  42. position:absolute;
  43. }
  44. div:nth-child(1){
  45. background:pink;
  46. transform:rotateY(60deg);
  47. }
  48. div:nth-child(2){
  49. background:lightblue;
  50. transform:rotateY(60deg) translateZ(-300px);
  51. }
  52. </style>
  53. </head>
  54. <body>
  55. <main>
  56. <div></div>
  57. <div></div>
  58. </main>
  59. </body>
  60. </html>

 

 

3D立方体动画:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6.  
  7. <style>
  8. *{
  9. margin:0;
  10. padding:0;
  11. box-sizing:border-box;
  12. }
  13. body{
  14. width:100vw;
  15. height:100vh;
  16. background:#3848a2;
  17. display:flex;
  18. justify-content:center;
  19. align-items:center;
  20. }
  21. main{
  22. display:flex;
  23. /* border:3px solid #ddd; */
  24. width:200px;
  25. height:200px;
  26. transform-style:preserve-3d;
  27. transform:perspective(900px);
  28. transition:2s;
  29. transform-origin:center center 100px;
  30. }
  31. body:hover main{
  32. transform:perspective(900px) rotateY(150deg);
  33. }
  34. div{
  35. width:200px;
  36. height:200px;
  37. position:absolute;
  38. background:#8bc34a;
  39. font-size:4em;
  40. display:flex;
  41. justify-content:center;
  42. align-items:center;
  43. transform-origin:center center 100px;
  44. opacity:.8;
  45. }
  46. div:nth-child(1){
  47. background:#c34a8b;
  48. transform:rotateY(90deg);
  49. }
  50. div:nth-child(2){
  51. background:#4a8bc3;
  52. transform:rotateY(180deg);
  53. }
  54. div:nth-child(3){
  55. background:#8bc34a;
  56. transform:rotateY(270deg);
  57. }
  58. div:nth-child(4){
  59. background:#4ac3bf;
  60. transform:rotateY(360deg);
  61. }
  62. div:nth-child(5){
  63. background:#c34a4e;
  64. transform-origin:top;
  65. transform:rotateX(90deg);
  66. }
  67. div:nth-child(6){
  68. background:#ff893b;
  69. transform-origin:bottom;
  70. transform:rotateX(-90deg);
  71. }
  72. </style>
  73. </head>
  74. <body>
  75. <main>
  76. <div>1</div>
  77. <div>2</div>
  78. <div>3</div>
  79. <div>4</div>
  80. <div>5</div>
  81. <div>6</div>
  82. </main>
  83. </body>
  84. </html>

 

三维幻灯片:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6.  
  7. <style>
  8. *{
  9. margin:0;
  10. padding:0;
  11. box-sizing:border-box;
  12. }
  13. body{
  14. width:100vw;
  15. height:100vh;
  16. background:#3848a2;
  17. display:flex;
  18. justify-content:center;
  19. align-items:center;
  20. }
  21. main{
  22. display:flex;
  23. /* border:3px solid #ddd; */
  24. width:400px;
  25. height:200px;
  26. transform-style:preserve-3d;
  27. transform:perspective(900px);
  28. transition:4s;
  29. /* 左右旋转 */
  30. transform-origin:200px center -100px;
  31. /* 上下旋转 */
  32. transform-origin:200px 100px -100px;
  33. }
  34. body:hover main{
  35. /* 左右旋转 */
  36. transform:perspective(900px) rotateY(360deg);
  37. /* 上下旋转 */
  38. transform:perspective(900px) rotateX(360deg);
  39. }
  40. div{
  41. position:absolute;
  42. background:#8bc34a;
  43. font-size:4em;
  44. display:flex;
  45. justify-content:center;
  46. align-items:center;
  47. transform-origin:center center 100px;
  48. opacity:.8;
  49. }
  50. div:nth-child(1){
  51. width:400px;
  52. height:200px;
  53. background:#c34a8b;
  54. }
  55. div:nth-child(2){
  56. width:400px;
  57. height:200px;
  58. background:#4a8bc3;
  59. transform:translateZ(-200px);
  60. }
  61. div:nth-child(3){
  62. width:200px;
  63. height:200px;
  64. background:#8bc34a;
  65. transform-origin:left;
  66. transform:rotateY(90deg);
  67. }
  68. div:nth-child(4){
  69. width:200px;
  70. height:200px;
  71. background:#4ac3bf;
  72. transform-origin:right;
  73. transform:translateX(200px) rotateY(-90deg);
  74. }
  75. div:nth-child(5){
  76. width:400px;
  77. height:200px;
  78. background:#c34a4e;
  79. transform-origin:top;
  80. transform:rotateX(-90deg);
  81. }
  82. div:nth-child(6){
  83. width:400px;
  84. height:200px;
  85. background:#ff893b;
  86. transform-origin:bottom;
  87. transform:rotateX(90deg);
  88. }
  89. </style>
  90. </head>
  91. <body>
  92. <main>
  93. <div>1</div>
  94. <div>2</div>
  95. <div>3</div>
  96. <div>4</div>
  97. <div>5</div>
  98. <div>6</div>
  99. </main>
  100. </body>
  101. </html>

 

backface-visibility 控制元素背面隐藏效果:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6.  
  7. <style>
  8. *{
  9. margin:0;
  10. padding:0;
  11. box-sizing:border-box;
  12. }
  13. body{
  14. width:100vw;
  15. height:100vh;
  16. background:#3848a2;
  17. display:flex;
  18. justify-content:center;
  19. align-items:center;
  20. }
  21. main{
  22. display:flex;
  23. justify-content:center;
  24. align-items:center;
  25. border:3px solid red;
  26. width:400px;
  27. height:400px;
  28. perspective:900px;
  29. }
  30. body:hover main div:nth-child(1){
  31. transform:rotateY(-180deg);
  32. }
  33. body:hover main div:nth-child(2){
  34. transform:rotateY(0deg);
  35. }
  36. div{
  37. width:200px;
  38. height:200px;
  39. position:absolute;
  40. display:flex;
  41. justify-content:center;
  42. align-items:center;
  43. transition:2s;
  44. /* 设置背面不可见 */
  45. backface-visibility:hidden;
  46. }
  47. div:nth-child(1){
  48. background:pink;
  49. }
  50. div:nth-child(2){
  51. background:lightblue;
  52. transform:rotateY(180deg);
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <main>
  58. <div>1</div>
  59. <div>2</div>
  60. </main>
  61. </body>
  62. </html>

 

如果旋转加在父元素上,需要设置三维效果
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6.  
  7. <style>
  8. *{
  9. margin:0;
  10. padding:0;
  11. box-sizing:border-box;
  12. }
  13. body{
  14. width:100vw;
  15. height:100vh;
  16. background:#3848a2;
  17. display:flex;
  18. justify-content:center;
  19. align-items:center;
  20. }
  21. main{
  22. display:flex;
  23. justify-content:center;
  24. align-items:center;
  25. border:3px solid red;
  26. width:400px;
  27. height:400px;
  28. perspective:900px;
  29. transition:2s;
  30. /* 如果旋转加在父元素上,需要设置三维效果 */
  31. transform-style:preserve-3d;
  32. }
  33. body:hover main{
  34. transform:rotateY(-180deg);
  35. }
  36. div{
  37. width:200px;
  38. height:200px;
  39. position:absolute;
  40. display:flex;
  41. justify-content:center;
  42. align-items:center;
  43. /* 设置背面不可见 */
  44. backface-visibility:hidden;
  45. }
  46. div:nth-child(1){
  47. background:pink;
  48. }
  49. div:nth-child(2){
  50. background:lightblue;
  51. transform:rotateY(180deg);
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <main>
  57. <div>1</div>
  58. <div>2</div>
  59. </main>
  60. </body>
  61. </html>

 

移动端3D登录注册切换页面:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  8.  
  9. <style>
  10. *{
  11. margin:0;
  12. padding:0;
  13. box-sizing:border-box;
  14. }
  15. main{
  16. width:100vw;
  17. height:100vh;
  18. transform-style:preserve-3d;
  19. transform:perspective(900px);
  20. transition:2s;
  21. }
  22. main.login{
  23. transform:perspective(900px) rotateY(0deg);
  24. }
  25. main.register{
  26. transform:perspective(900px) rotateY(180deg);
  27. }
  28. /* body:hover main{
  29. transform:perspective(900px) rotateY(180deg);
  30. } */
  31. div{
  32. position: absolute;
  33. width:100%;
  34. height:100%;
  35. background:#000;
  36. display:flex;
  37. flex-direction:column;
  38. justify-content: center;
  39. align-items: center;
  40. font-size:4em;
  41. color:white;
  42. text-transform:uppercase;
  43. backface-visibility:hidden;
  44. transition:1s;
  45.  
  46. }
  47. div:nth-child(1){
  48. background:#8a4af3;
  49. }
  50. div:nth-child(2){
  51. background:#f34a5e;
  52. transform:rotateY(180deg);
  53. }
  54. div span{
  55. font-size:.3em;
  56. color:#ddd;
  57. }
  58. div i[class^='fa']{
  59. font-size:1.5em;
  60. }
  61. nav{
  62. width:100%;
  63. position:absolute;
  64. text-align:center;
  65. bottom:60px;
  66. }
  67. nav a{
  68. padding:10px 20px;
  69. background:#666666;
  70. margin-right:10px;
  71. text-decoration: none;
  72. color:white;
  73. }
  74.  
  75.  
  76. </style>
  77. </head>
  78. <body>
  79. <main>
  80. <div>
  81. <i class="fa fa-home" aria-hidden="true"></i>
  82. login
  83. <span>cyy login</span>
  84. </div>
  85. <div>
  86. <i class="fa fa-user" aria-hidden="true"></i>
  87. register
  88. <span>cyy register</span>
  89. </div>
  90. </main>
  91. <nav>
  92. <a href="javascript:;" onclick="change('login')">登录</a>
  93. <a href="javascript:;" onclick="change('register')">注册</a>
  94. </nav>
  95.  
  96. <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>
  97. <script>
  98. function change(t){
  99. switch(t){
  100. case 'login':
  101. $('main').removeClass().addClass('login');
  102. break;
  103. case 'register':
  104. $('main').removeClass().addClass('register');
  105. break;
  106. }
  107. }
  108. </script>
  109. </body>
  110. </html>

原文链接:http://www.cnblogs.com/chenyingying0/p/13958560.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号