经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS3 » 查看文章
HTML+CSS3+JS 实现的下拉菜单_css3_CSS
来源:jb51  时间:2020/11/23 11:51:09  对本文有异议

实现效果

html

  1. <div class="container">
  2. <h1 class="title">Dropdown Menu</h1>
  3. <ul>
  4. <li class="dropdown">
  5. <a href="#" data-toggle="dropdown">First Menu <i class="icon-arrow"></i></a>
  6. <ul class="dropdown-menu">
  7. <li><a href="#">Home</a></li>
  8. <li><a href="#">About Us</a></li>
  9. <li><a href="#">Services</a></li>
  10. <li><a href="#">Contact</a></li>
  11. </ul>
  12. </li>
  13. <li class="dropdown">
  14. <a href="#" data-toggle="dropdown">Second Menu <i class="icon-arrow"></i></a>
  15. <ul class="dropdown-menu">
  16. <li><a href="#">Home</a></li>
  17. <li><a href="#">About Us</a></li>
  18. <li><a href="#">Services</a></li>
  19. <li><a href="#">Contact</a></li>
  20. </ul>
  21. </li>
  22. <li class="dropdown">
  23. <a href="#" data-toggle="dropdown">Third Menu <i class="icon-arrow"></i></a>
  24. <ul class="dropdown-menu">
  25. <li><a href="#">Home</a></li>
  26. <li><a href="#">About Us</a></li>
  27. <li><a href="#">Services</a></li>
  28. <li><a href="#">Contact</a></li>
  29. </ul>
  30. </li>
  31. </ul>
  32. <p class="text-center">
  33. See this same menu only with CSS3: <a href="https://codepen.io/pedronauck/pen/jaluz" target="_blank">https://codepen.io/pedronauck/pen/jaluz</a>
  34. </p>
  35. </div>

css

  1. @import "compass/css3";
  2.  
  3. @import url("https://fonts.googleapis.com/css?family=Lato:300,400,700,900");
  4. @import url(https://fonts.googleapis.com/css?family=Pacifico);
  5.  
  6. body {
  7. font-family: "Lato", Helvetica, Arial;
  8. font-size: 16px;
  9. }
  10.  
  11. .text-center {
  12. text-align: center;
  13. }
  14.  
  15. *, *:before, *:after {
  16. -webkit-border-sizing: border-box;
  17. -moz-border-sizing: border-box;
  18. border-sizing: border-box;
  19. }
  20.  
  21. .container {
  22. width: 350px;
  23. margin: 50px auto;
  24. & > ul {
  25. list-style: none;
  26. padding: 0;
  27. margin: 0 0 20px 0;
  28. }
  29. }
  30.  
  31. // =============================================================================
  32. // Mixins and Variables
  33. // =============================================================================
  34.  
  35. $blue: #2980B9;
  36. $gray: #EEE;
  37.  
  38. @mixin ul-nostyle {
  39. list-style: none;
  40. padding: 0;
  41. margin: 0;
  42. }
  43.  
  44. @mixin double-shadow($color) {
  45. @include box-shadow(0 1px 0 lighten($color, 10%) inset, 0 -1px 0 darken($color, 10%) inset);
  46. }
  47.  
  48. @mixin hover-style($color) {
  49. &:hover {
  50. background: lighten($color, 3%);
  51. }
  52. }
  53.  
  54. @mixin animation($content) {
  55. animation: $content;
  56. -moz-animation: $content;
  57. -webkit-animation: $content;
  58. }
  59.  
  60. @mixin keyframes($name) {
  61. @keyframes #{$name} { @content; }
  62. @-moz-keyframes #{$name} { @content; }
  63. @-webkit-keyframes #{$name} { @content; }
  64. }
  65.  
  66. // =============================================================================
  67. // Classes
  68. // =============================================================================
  69.  
  70. .title {
  71. font-family: 'Pacifico';
  72. font-weight: norma;
  73. font-size: 40px;
  74. text-align: center;
  75. line-height: 1.4;
  76. color: $blue;
  77. }
  78.  
  79. .dropdown {
  80. a {
  81. text-decoration: none;
  82. }
  83.  
  84. [data-toggle="dropdown"] {
  85. position: relative;
  86. display: block;
  87. color: white;
  88. background: $blue;
  89. @include double-shadow($blue);
  90. @include hover-style($blue);
  91. @include text-shadow(0 -1px 0 rgba(0,0,0,0.3));
  92. padding: 10px;
  93.  
  94. }
  95. .icon-arrow {
  96. position: absolute;
  97. display: block;
  98. font-size: 0.7em;
  99. color: #fff;
  100. top: 14px;
  101. right: 10px;
  102.  
  103. &.open {
  104. @include transform(rotate(-180deg));
  105. @include transition(transform .6s);
  106. }
  107. &.close {
  108. @include transform(rotate(0deg));
  109. @include transition(transform .6s);
  110. }
  111.  
  112. &:before {
  113. content: '\25BC';
  114. }
  115. }
  116.  
  117. .dropdown-menu {
  118. max-height: 0;
  119. overflow: hidden;
  120. @include ul-nostyle;
  121.  
  122. li {
  123. padding: 0;
  124.  
  125. a {
  126. display: block;
  127. color: darken($gray, 50%);
  128. background: $gray;
  129. @include double-shadow($gray);
  130. @include hover-style($gray);
  131. @include text-shadow(0 -1px 0 rgba(255,255,255,0.3));
  132. padding: 10px 10px;
  133. }
  134. }
  135. }
  136.  
  137. .show, .hide {
  138. @include transform-origin(50%, 0%);
  139. }
  140.  
  141. .show {
  142. display: block;
  143. max-height: 9999px;
  144. @include transform(scaleY(1));
  145. @include animation(showAnimation .5s ease-in-out);
  146. @include transition(max-height 1s ease-in-out);
  147. }
  148.  
  149. .hide {
  150. max-height: 0;
  151. @include transform(scaleY(0));
  152. @include animation(hideAnimation .4s ease-out);
  153. @include transition(max-height .6s ease-out);
  154. }
  155. }
  156.  
  157. @include keyframes(showAnimation) {
  158. 0% {
  159. @include transform(scaleY(0.1));
  160. }
  161. 40% {
  162. @include transform(scaleY(1.04));
  163. }
  164. 60% {
  165. @include transform(scaleY(0.98));
  166. }
  167. 80% {
  168. @include transform(scaleY(1.04));
  169. }
  170. 100% {
  171. @include transform(scaleY(0.98));
  172. }
  173. 80% {
  174. @include transform(scaleY(1.02));
  175. }
  176. 100% {
  177. @include transform(scaleY(1));
  178. }
  179. }
  180.  
  181. @include keyframes(hideAnimation) {
  182. 0% {
  183. @include transform(scaleY(1));
  184. }
  185. 60% {
  186. @include transform(scaleY(0.98));
  187. }
  188. 80% {
  189. @include transform(scaleY(1.02));
  190. }
  191. 100% {
  192. @include transform(scaleY(0));
  193. }
  194. }

js

  1. // Dropdown Menu
  2. var dropdown = document.querySelectorAll('.dropdown');
  3. var dropdownArray = Array.prototype.slice.call(dropdown,0);
  4. dropdownArray.forEach(function(el){
  5. var button = el.querySelector('a[data-toggle="dropdown"]'),
  6. menu = el.querySelector('.dropdown-menu'),
  7. arrow = button.querySelector('i.icon-arrow');
  8.  
  9. button.onclick = function(event) {
  10. if(!menu.hasClass('show')) {
  11. menu.classList.add('show');
  12. menu.classList.remove('hide');
  13. arrow.classList.add('open');
  14. arrow.classList.remove('close');
  15. event.preventDefault();
  16. }
  17. else {
  18. menu.classList.remove('show');
  19. menu.classList.add('hide');
  20. arrow.classList.remove('open');
  21. arrow.classList.add('close');
  22. event.preventDefault();
  23. }
  24. };
  25. })
  26.  
  27. Element.prototype.hasClass = function(className) {
  28. return this.className && new RegExp("(^|\\s)" + className + "(\\s|$)").test(this.className);
  29. };

以上就是HTML+CSS3+JS 实现的下拉菜单的详细内容,更多关于HTML+CSS3+JS 下拉菜单的资料请关注w3xue其它相关文章!

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

本站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号