HTML+CSS3+JS 如何实现的下拉菜单

近处的大树枝叶茂盛。小草叶子又肥又美。小花绚丽多姿。一阵阵风吹来,花草树木,一起奏出了森林交响曲,一只只蝴蝶随着交响曲跳起了舞呢。

实现效果

html

<div class="container">
  <h1 class="title">Dropdown Menu</h1>
  <ul>
    <li class="dropdown">
      <a rel="nofollow noopener noreferrer" href="#" data-toggle="dropdown">First Menu <i class="icon-arrow"></i></a>
      <ul class="dropdown-menu">
        <li><a rel="nofollow noopener noreferrer" href="#">Home</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">About Us</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">Services</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">Contact</a></li>
      </ul>
    </li>
    <li class="dropdown">
      <a rel="nofollow noopener noreferrer" href="#" data-toggle="dropdown">Second Menu <i class="icon-arrow"></i></a>
      <ul class="dropdown-menu">
        <li><a rel="nofollow noopener noreferrer" href="#">Home</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">About Us</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">Services</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">Contact</a></li>
      </ul>
    </li>
    <li class="dropdown">
      <a rel="nofollow noopener noreferrer" href="#" data-toggle="dropdown">Third Menu <i class="icon-arrow"></i></a>
      <ul class="dropdown-menu">
        <li><a rel="nofollow noopener noreferrer" href="#">Home</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">About Us</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">Services</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">Contact</a></li>
      </ul>
    </li>
  </ul>
  <p class="text-center">
    See this same menu only with CSS3: <a rel="nofollow noopener noreferrer" 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 下拉菜单的资料请关注其它相关文章!

您可能有感兴趣的文章
CSS如何使用Flex和Grid布局如何实现3D骰子

纯CSS打字动画的如何实现示例

CSS中如何使用grid布局如何实现一套模板多种布局

css3手动如何实现pc端横向滚动

css如何实现右上角飘带效果的完整代码