HTML+CSS3+JS 实现的下拉菜单
程序员文章站
2022-04-20 07:52:15
实现效果html
dropdown menu<...
实现效果
html
<div class="container"> <h1 class="title">dropdown menu</h1> <ul> <li class="dropdown"> <a href="#" data-toggle="dropdown">first menu <i class="icon-arrow"></i></a> <ul class="dropdown-menu"> <li><a href="#">home</a></li> <li><a href="#">about us</a></li> <li><a href="#">services</a></li> <li><a href="#">contact</a></li> </ul> </li> <li class="dropdown"> <a href="#" data-toggle="dropdown">second menu <i class="icon-arrow"></i></a> <ul class="dropdown-menu"> <li><a href="#">home</a></li> <li><a href="#">about us</a></li> <li><a href="#">services</a></li> <li><a href="#">contact</a></li> </ul> </li> <li class="dropdown"> <a href="#" data-toggle="dropdown">third menu <i class="icon-arrow"></i></a> <ul class="dropdown-menu"> <li><a href="#">home</a></li> <li><a href="#">about us</a></li> <li><a href="#">services</a></li> <li><a href="#">contact</a></li> </ul> </li> </ul> <p class="text-center"> see this same menu only with css3: <a href="https://codepen.io/pedronauck/pen/jaluz" target="_blank">https://codepen.io/pedronauck/pen/jaluz</a> </p> </div>
css
@import "compass/css3"; @import url("https://fonts.googleapis.com/css?family=lato:300,400,700,900"); @import url(https://fonts.googleapis.com/css?family=pacifico); body { font-family: "lato", helvetica, arial; font-size: 16px; } .text-center { text-align: center; } *, *:before, *:after { -webkit-border-sizing: border-box; -moz-border-sizing: border-box; border-sizing: border-box; } .container { width: 350px; margin: 50px auto; & > ul { list-style: none; padding: 0; margin: 0 0 20px 0; } } // ============================================================================= // mixins and variables // ============================================================================= $blue: #2980b9; $gray: #eee; @mixin ul-nostyle { list-style: none; padding: 0; margin: 0; } @mixin double-shadow($color) { @include box-shadow(0 1px 0 lighten($color, 10%) inset, 0 -1px 0 darken($color, 10%) inset); } @mixin hover-style($color) { &:hover { background: lighten($color, 3%); } } @mixin animation($content) { animation: $content; -moz-animation: $content; -webkit-animation: $content; } @mixin keyframes($name) { @keyframes #{$name} { @content; } @-moz-keyframes #{$name} { @content; } @-webkit-keyframes #{$name} { @content; } } // ============================================================================= // classes // ============================================================================= .title { font-family: 'pacifico'; font-weight: norma; font-size: 40px; text-align: center; line-height: 1.4; color: $blue; } .dropdown { a { text-decoration: none; } [data-toggle="dropdown"] { position: relative; display: block; color: white; background: $blue; @include double-shadow($blue); @include hover-style($blue); @include text-shadow(0 -1px 0 rgba(0,0,0,0.3)); padding: 10px; } .icon-arrow { position: absolute; display: block; font-size: 0.7em; color: #fff; top: 14px; right: 10px; &.open { @include transform(rotate(-180deg)); @include transition(transform .6s); } &.close { @include transform(rotate(0deg)); @include transition(transform .6s); } &:before { content: '\25bc'; } } .dropdown-menu { max-height: 0; overflow: hidden; @include ul-nostyle; li { padding: 0; a { display: block; color: darken($gray, 50%); background: $gray; @include double-shadow($gray); @include hover-style($gray); @include text-shadow(0 -1px 0 rgba(255,255,255,0.3)); padding: 10px 10px; } } } .show, .hide { @include transform-origin(50%, 0%); } .show { display: block; max-height: 9999px; @include transform(scaley(1)); @include animation(showanimation .5s ease-in-out); @include transition(max-height 1s ease-in-out); } .hide { max-height: 0; @include transform(scaley(0)); @include animation(hideanimation .4s ease-out); @include transition(max-height .6s ease-out); } } @include keyframes(showanimation) { 0% { @include transform(scaley(0.1)); } 40% { @include transform(scaley(1.04)); } 60% { @include transform(scaley(0.98)); } 80% { @include transform(scaley(1.04)); } 100% { @include transform(scaley(0.98)); } 80% { @include transform(scaley(1.02)); } 100% { @include transform(scaley(1)); } } @include keyframes(hideanimation) { 0% { @include transform(scaley(1)); } 60% { @include transform(scaley(0.98)); } 80% { @include transform(scaley(1.02)); } 100% { @include transform(scaley(0)); } }
js
// dropdown menu var dropdown = document.queryselectorall('.dropdown'); var dropdownarray = array.prototype.slice.call(dropdown,0); dropdownarray.foreach(function(el){ var button = el.queryselector('a[data-toggle="dropdown"]'), menu = el.queryselector('.dropdown-menu'), arrow = button.queryselector('i.icon-arrow'); button.onclick = function(event) { if(!menu.hasclass('show')) { menu.classlist.add('show'); menu.classlist.remove('hide'); arrow.classlist.add('open'); arrow.classlist.remove('close'); event.preventdefault(); } else { menu.classlist.remove('show'); menu.classlist.add('hide'); arrow.classlist.remove('open'); arrow.classlist.add('close'); event.preventdefault(); } }; }) element.prototype.hasclass = function(classname) { return this.classname && new regexp("(^|\\s)" + classname + "(\\s|$)").test(this.classname); };
以上就是html+css3+js 实现的下拉菜单的详细内容,更多关于html+css3+js 下拉菜单的资料请关注其它相关文章!