[关闭]
@xunuo 2018-08-14T16:59:32.000000Z 字数 5202 阅读 808

transition和transform属性

前端


css 3 实现动画效果

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>test-transition</title>
  6. <style>
  7. #test-transition{
  8. position: absolute;
  9. height: 400px;
  10. width: 400px;
  11. top: 0;
  12. left: 0;
  13. right: 0;
  14. bottom: 0;
  15. margin: auto;
  16. background-color: #00438A;
  17. -webkit-transition:background-color 3s;
  18. }
  19. #test-transition:hover{
  20. background-color: #00EE00;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="test-transition">
  26. </div>
  27. </body>
  28. </html>
  1. position: absolute;//使用绝对定位
  2. top: 0;
  3. left: 0;
  4. right: 0;
  5. bottom: 0;
  6. margin: auto;
  1. /*ex:-webkit-transition:background-color 3s;
  2. 可分解为
  3. -webkit-transition-property:background-color;
  4. -webkit-transition-duration: 3s;*/

设置3D场景
- 设置距离与观察位置:

  1. -webkit-perspective:800px;//物体与屏幕的位置
  2. -webkit-perspective-origin:50% 50%;//从屏幕中心观看
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>test-transition</title>
  6. <style>
  7. #test-transform {
  8. -webkit-perspective: 800px;
  9. -webkit-perspective-origin: 50% 50%;
  10. -webkit-transform-style: -webkit-preserve-3d;
  11. }
  12. #transform-body {
  13. width: 200px;
  14. height: 200px;
  15. margin: 0 auto;
  16. background-color: #00458A;
  17. }
  18. .p-design {
  19. margin: auto 500px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="test-transform">
  25. <div id="transform-body">
  26. </div>
  27. </div>
  28. <div class="p-design">
  29. <div style="width: 50%;float: left;">
  30. <p>Translate
  31. <span id="xtrans">0</span>px:</p>
  32. <input id="translatex" type="range" min="-400" max="400" value="0" onmousemove="change_value()" />
  33. <p>Translate
  34. <span id="ytrans">0</span>px:</p>
  35. <input id="translatey" type="range" min="-400" max="400" value="0" onmousemove="change_value()" />
  36. <p>Translate
  37. <span id="ztrans">0</span>px:</p>
  38. <input id="translatez" type="range" min="-400" max="400" value="0" onmousemove="change_value()" />
  39. </div>
  40. <div style="width: 50%;float: right;">
  41. <p>Rotate
  42. <span id="xrotate">0</span>px:</p>
  43. <input id="rotatex" type="range" min="-360" max="360" value="0" onmousemove="change_value()" />
  44. <p>Rotate
  45. <span id="yrotate">0</span>px:</p>
  46. <input id="rotatey" type="range" min="-360" max="360" value="0" onmousemove="change_value()" />
  47. <p>Rotate
  48. <span id="zrotate">0</span>px:</p>
  49. <input id="rotatez" type="range" min="-360" max="360" value="0" onmousemove="change_value()" />
  50. </div>
  51. </div>
  52. <script type="text/javascript">
  53. function change_value() {
  54. var x = document.getElementById("translatex").value;
  55. var y = document.getElementById("translatey").value;
  56. var z = document.getElementById("translatez").value;
  57. var roX = document.getElementById("rotatex").value;
  58. var roY = document.getElementById("rotatey").value;
  59. var roZ = document.getElementById("rotatez").value;
  60. var transform_value = document.getElementById("transform-body");
  61. document.getElementById('transform-body').style.webkitTransform = "translateX(" + x + "px) translateY(" + y + "px) translateZ(" + z + "px) rotateX(" + roX + "deg) rotateY(" + roY + "deg) rotateZ(" + roZ + "deg)";
  62. document.getElementById("xtrans").innerText = x;
  63. document.getElementById("ytrans").innerText = y;
  64. document.getElementById("ztrans").innerText = z;
  65. document.getElementById("xrotate").innerText = roX;
  66. document.getElementById("yrotate").innerText = roY;
  67. document.getElementById("zrotate").innerText = roZ;
  68. }
  69. </script>
  70. </body>
  71. </html>
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>test-transition</title>
  6. <style>
  7. #page-body{
  8. -webkit-perspective: 800px;
  9. -webkit-perspective-origin: 50% 50%;
  10. overflow: hidden;
  11. }
  12. #page-all{
  13. -webkit-transform-style: preserve-3d;/*如果将这行设置在page-body内会使底部覆盖不完全*/
  14. width: 400px;
  15. height: 400px;
  16. position: relative;
  17. margin: 0 auto;
  18. }
  19. .page{
  20. width: 360px;
  21. height: 360px;
  22. background-color: #000000;
  23. position: absolute;
  24. color: #FFFFFF;
  25. padding: 20px;
  26. font-weight: bold;
  27. font-size: 360px;
  28. line-height: 360px;
  29. text-align: center;
  30. }
  31. #page1{
  32. -webkit-transform-origin: bottom;/*使围绕底部翻转*/
  33. -webkit-transition: -webkit-transform 1s linear;/*翻转过程时间为1s 动画为匀速*/
  34. }
  35. #page2,#page3,#page4,#page5{
  36. -webkit-transform-origin: bottom;
  37. -webkit-transition: -webkit-transform 1s linear;
  38. -webkit-transform: rotateX(90deg);/*使2 3 4 5页置于底部*/
  39. }
  40. #cli{
  41. text-align: center;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div id="page-body">
  47. <div id="page-all">
  48. <div id="page1" class="page">1</div>
  49. <div id="page2" class="page">2</div>
  50. <div id="page3" class="page">3</div>
  51. <div id="page4" class="page">4</div>
  52. <div id="page5" class="page">5</div>
  53. </div>
  54. </div>
  55. <br>
  56. <div id="cli">
  57. <a id="prev" href="javascript:prevpage()">上一页</a>
  58. <a id="next" href="javascript:nextpage()">下一页</a>
  59. </div>
  60. <script type="text/javascript">
  61. var curPage=1;
  62. function prevpage(){
  63. if(curPage==1)
  64. return;
  65. var tempPage=document.getElementById("page"+curPage);
  66. tempPage.style.webkitTransform="rotateX(90deg)";
  67. curPage--;
  68. document.getElementById("page"+curPage).style.webkitTransform="rotateX(0deg)";
  69. }
  70. function nextpage(){
  71. if(curPage==5)
  72. return;
  73. var tempPage=document.getElementById("page"+curPage);
  74. tempPage.style.webkitTransform="rotateX(-90deg)";
  75. curPage++;
  76. document.getElementById("page"+curPage).style.webkitTransform="rotateX(0deg)";
  77. }
  78. </script>
  79. </body>
  80. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注