分享

How to spice up your menu with CSS3 | Codrops

 温柔一哮 2012-05-03

How to spice up your menu with CSS3

Quick tip on how to spice up your menu with CSS3: add an image to every menu item and slide it out on hover.
Advertisement

CSS3MenuHoverEffect

View demo Download source

In this new category called “Tips and Tricks” we will introduce some quick and interesting methods around web development and web design. In today’s tip we’ll show you how to spice up your menu by adding a neat hover effect to it. The idea is to slide an image out to the right when hovering over a menu item.

Each menu item (which is a unordered list item in this case) will have an anchor containing two spans and an image:

<ul class="mh-menu">
	<li>
		<a href="#">
			<span>Art Director</span>
			<span>Henry James</span>
		</a>
		<img src="images/1.jpg" alt="image01"/>
	</li>
	<!-- ... -->
</ul>

We’ll give .mh-menu li a display block and rgba(255,255,255, 0.8) as background color. When we hover over a list item, we’ll color the background into rgba(225,239,240, 0.4) which is a light blue:

.mh-menu li:hover a{
	background: rgba(225,239,240, 0.4);
}

The second span will also change its color on hover, but we want to change each item into a different color. So, we’ll add a color transition and get each different element with the nth-child selector:

.mh-menu li a span:nth-child(2){
	/*...*/
	transition: color 0.2s linear;
}
.mh-menu li:nth-child(1):hover span:nth-child(2){
	color: #ae3637;
}
.mh-menu li:nth-child(2):hover span:nth-child(2){
	color: #c3d243;
}
.mh-menu li:nth-child(3):hover span:nth-child(2){
	color: #d38439;
}
.mh-menu li:nth-child(4):hover span:nth-child(2){
	color: #8e7463;
}

The image will slide to the right side, so initially, it will have a left of 0px. We’ll also add a transition for its opacity; it will animate from 0 (initial value) to 1:

.mh-menu li img{
	position: absolute;
	z-index: 1;
	left: 0px;
	top: 0px;
	opacity: 0;
	transition: left 0.4s ease-in-out, opacity 0.6s ease-in-out;
}
.mh-menu li:hover img{
	left: 300px;
	opacity: 1;
}

And voilà, there we have our nice slide out effect!
Make sure, that the z-index of the anchor is set to something higher than the image so that it slides under the anchor and not on top of it.

Alternatively, we can make the background color of the anchor become opaque on hover, i.e. completely white (demo 2), or color each child differently (demo 3).

The illustrations in the demo are by Bartosz Kosowski (CC BY-NC 3.0).

View demo Download source

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多