본문으로 바로가기

메뉴 슬라이딩 효과

category Code Lab 2016. 6. 17. 10:15

Menu with sliding hover

이 글에서는 오버시 슬라이딩 오버효과를 사용해 봅니다.




HTML

html
<div class="menu-container">
	<menu>
		<menuitem> MENU 01</menuitem>
		<menuitem> MENU 02</menuitem>
		<menuitem> MENU 03</menuitem>
		<menuitem> MENU 04</menuitem>
	</menu>
	<div class="sliding-bar"></div>
</div>



CSS

css
@import url(https://fonts.googleapis.com/css?family=Raleway:100,200,300,400,500,600,700,800,900);
* {
  box-sizing: border-box;
  font-family: Raleway;
}
body {
  text-align: center;
  background-color: #f96;
}
.menu-container {
  display: inline-block;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  background-color: #fff;
  z-index: 0;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
}
.menu-container menu {
  display: block;
  position: relative;
  width: 15em;
  padding: .25em;
  border: 2px solid #c99;
  margin: 0;
  z-index: 2;
}
.menu-container menu menuitem {
  display: block;
  height: 4em;
  line-height: 4em;
  padding: 0 1em;
  cursor: pointer;
  background: transparent;
  text-align: left;
  color: #666;
  font-weight: 500;
  letter-spacing: .02em;
}
.menu-container menu menuitem:hover {
  color: #000;
}
.menu-container .sliding-bar {
  position: absolute;
  background-color: #aef;
  transition: all .2s ease;
  z-index: 1;
  opacity: 0;
}



JavaScript

javascript
$(window).ready(function() {
	initMenu();
});

$('menuitem').hover(function() {
	var offset = $(this).offset();
	$('.sliding-bar').offset(offset);
});

$('menu').hover(function() {
	$('.sliding-bar').css({
		opacity: 1
	});
}, function() {
	$('.sliding-bar').css({
		opacity: 0
	});
})

var initMenu = function() {
	var $initElem = $('menuitem:first-of-type');
	var initOffset = $initElem.offset();
	var initSize = {
		width: $initElem.css('width'),
		height: $initElem.css('height')
	}
	$('.sliding-bar')
		.offset(initOffset)
		.css(initSize)
}



Result View

See the Pen 메뉴 슬라이딩 오버 by jaeheekim (@jaehee) on CodePen.



Jaehee's WebClub


'Code Lab' 카테고리의 다른 글

반응형 컬럼 ↔ 탭  (0) 2016.06.20
반응형 탭 메뉴  (0) 2016.06.20
1단 메뉴 jQuery Study #2  (0) 2016.06.15
1단 메뉴 jQuery Study #1  (0) 2016.06.15
simple tab menu  (0) 2016.06.13