• js实现高度自动的过渡动画


    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Documenttitle>
    8. head>
    9. <style>
    10. * {
    11. padding: 0;
    12. margin: 0;
    13. }
    14. ul {
    15. width: 200px;
    16. height: 0;
    17. /* 动画类型 */
    18. transition: transform;
    19. /* 动画时长 */
    20. transition-duration: .8s;
    21. /* 动画延迟 */
    22. transition-delay: .2s;
    23. /* 动画控制属性 */
    24. transition-property: height;
    25. overflow: hidden;
    26. cursor: pointer;
    27. border-radius: 2px;
    28. box-shadow: 6px 6px 10px rgba(0, 0, 0, 0.2);
    29. }
    30. li {
    31. list-style-type: none;
    32. width: 200px;
    33. height: 40px;
    34. line-height: 40px;
    35. color: #fff;
    36. font-size: 14px;
    37. text-align: center;
    38. background: #1061f8;
    39. }
    40. body {
    41. display: flex;
    42. flex-direction: column;
    43. align-items: center;
    44. }
    45. #btn {
    46. width: 200px;
    47. height: 40px;
    48. line-height: 40px;
    49. margin-top: 100px;
    50. text-align: center;
    51. background: #dfdfdf;
    52. border-radius: 2px;
    53. cursor: pointer;
    54. }
    55. style>
    56. <body>
    57. <p id="btn">toggle菜单p>
    58. <ul id="ul">
    59. <li>菜单一li>
    60. <li>菜单一li>
    61. <li>菜单一li>
    62. <li>菜单一li>
    63. <li>菜单一li>
    64. <li>菜单一li>
    65. <li>菜单一li>
    66. <li>菜单一li>
    67. <li>菜单一li>
    68. ul>
    69. body>
    70. <script>
    71. var btn = document.getElementById('btn');
    72. var menu = document.getElementById('ul');
    73. var heigt = 0
    74. btn.onmouseenter = function () {
    75. // 获取菜单高度(执行JS时浏览器来不及渲染,所有不会闪烁)
    76. if (!heigt) {
    77. menu.style.height = 'auto';
    78. heigt = menu.getBoundingClientRect().height;
    79. }
    80. menu.style.height = 0;
    81. //offsetHeight会强制浏览器重绘(先渲染0px,然后再渲染heigt,如果不加则直接会渲染height,不会渲染0px)
    82. menu.offsetHeight
    83. menu.style.height = heigt + 'px';
    84. }
    85. btn.onmouseleave = function () {
    86. menu.style.height = 0;
    87. }
    88. menu.onmouseenter = function () {
    89. menu.style.height = heigt + 'px';
    90. }
    91. menu.onmouseleave = function () {
    92. menu.style.height = 0;
    93. }
    94. script>
    95. html>

  • 相关阅读:
    背包问题及其拓展
    flyway适配高斯数据库
    霸榜双11!科技创新助力九牧卫浴赢战全渠道
    HashMap 、LinkedHashMap 和TreeMap
    Linux操作系统——面试题-(腾讯,百度,美团,滴滴)
    一文读懂 Linux 网络 IO 模型
    SPA项目开发之首页导航+左侧菜单
    iOS开发Swift-5-自动布局AutoLayout-摇骰子App
    Netty 学习:通信协议和编解码
    STM32F103C8 串口的使用
  • 原文地址:https://blog.csdn.net/Mr_linjw/article/details/134020110