源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 326|回复: 19

[CSS] css 背景图片定位在菜单效果中的应用实例

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2009-12-17 21:23:17 | 显示全部楼层 |阅读模式
这是一款效果不错的菜单,在设计方面具有鲜明的风格。其实不管外在表现形式如何变化,经过分析,WEB界面元素都可以分解成具有语义的HTML编码。 这款菜单大家需要注意HOVER状态下背景图片定位发生了变化,从而实现了这样的效果。

本实例是一位国外的朋友所写,不进行任何翻译,直接发出来,大家可以偿试阅读,如果遇到困难可以求助于翻译软件。
不断的提高自己的英文阅读能力也是我们一直所提倡的,让我们一起努力吧!

Overview
  Here are the required graphics to assembe the menu


1. Main background
  Open the Photoshop file. Turn off the menu text Layer Group and save the main background as menu-bg.jpg.


2. Button graphics
  Turn off the background Layer Group and leave only the menu text layers visible. Make a rectangle selection cover the "home" item, go to menu Edit > Copy Merged (Cmd + Shift + C).


  Create a new file and take note of the file dimension (w x h), in my case the "home" graphic is 144 x 58px. Paste the "home" graphic in the new file. Go to menu Image > Canvas Size, adjust the image height x 2 (58 + 58 = 116px). Duplicate the home graphic layer and align it to the bottom. Erase the highlight strokes in the upper layer.


  Here is how the hover effect will work. We will set the link button to 144 x 58px, when mouseover, we will shift the background image from top to bottom.


  Repeat this step for the other buttons. You should have the follow graphics:


3. HTML source
  When you are done with the graphics, let’s start coding. Start with an un-ordered list <ul>.
  ● note there is an id="menu" assigned to the<ul> tag
  ● an unique class name assigned to each link <a>
  ● an empty <span> tag (the purpose of this is to make the mouseover effect)

div css xhtml xml Example Source Code Example Source Code [www.52css.com] <ul id="menu">
   <li><a href="#" class="home">Home <span></span></a></li>
   <li><a href="#" class="about">About <span></span></a></li>
   <li><a href="#" class="rss">RSS <span></span></a></li>
</ul>
#menu
  Reset the menu to no padding, no margin, and no list-style. Specify the width and height same dimension as the menu-bg.jpg. Then attach the menu background image. The key point to remember here is set the position property to relative.

 Example Source Code [www.52css.com] #menu {
   list-style: none;
   padding: 0;
   margin: 0;
   width: 774px;
   height: 210px;
   background: url(images/menu-bg.jpg) no-repeat;
   position: relative;
}
#menu span
  Specify the span element to display:none (so they will be invisible by default). Specify position:absolute, so we can place the mouseover GIF image on exact position.

 Example Source Code [www.52css.com] #menu span {
   display: none;
   position: absolute;
}
#menu a
  The key point here is the text-indent property. We specify the text-indent property with a negative value (-900%), so the text will be hidden.

 Example Source Code [www.52css.com] #menu a {
   display: block;
   text-indent: -900%;
   position: absolute;
   outline: none;
}
#menu a:hover
  When mouseover the link, we want to shift the background image from top to bottom.

 Example Source Code [www.52css.com] #menu a:hover {
   background-position: left bottom;
}
#menu a:hover span
  When mouseover the link, we want the span element to display:block.

 Example Source Code [www.52css.com] #menu a:hover span {
   display: block;
}
#menu .home
  Specify the width, height, and background image. Since we already specified all <a> element postition:absolute in previous step, now just say where the .home button should be by specifying the left and top property.

 Example Source Code [www.52css.com] #menu .home {
   width: 144px;
   height: 58px;
   background: url(images/home.gif) no-repeat;
   left: 96px;
   top: 73px;
}
#menu .home span
  Here we are specifying the width, height, background, and position of the span element of .home (mouseover GIF image)

 Example Source Code [www.52css.com] #menu .home span {
   width: 86px;
   height: 14px;
   background: url(images/home-over.gif) no-repeat;
   left: 28px;
   top: -20px;
}
#menu .about
  Copy the .home rules and rename them to .about. Now just change the width, height, background, left, and top property.

 Example Source Code [www.52css.com] #menu .about {
   width: 131px;
   height: 51px;
   background: url(images/about.gif) no-repeat;
   left: 338px;
   top: 97px;
}
#menu .about span {
   width: 40px;
   height: 12px;
   background: url(images/about-over.gif) no-repeat;
   left: 44px;
   top: 54px;
}
#menu .rss
  Repeat this step for .rss

 Example Source Code [www.52css.com] #menu .rss {
   width: 112px;
   height: 47px;
   background: url(images/rss.gif) no-repeat;
   left: 588px;
   top: 94px;
}
#menu .rss span {
   width: 92px;
   height: 20px;
   background: url(images/rss-over.gif) no-repeat;
   left: 26px;
   top: -20px;
}
All in one:

 Example Source Code [www.52css.com] #menu {
   list-style: none;
   padding: 0;
   margin: 0;
   width: 774px;
   height: 210px;
   background: url(images/menu-bg.jpg) no-repeat;
   position: relative;
}
#menu span {
   display: none;
   position: absolute;
}
#menu a {
   display: block;
   text-indent: -900%;
   position: absolute;
   outline: none;
}
#menu a:hover {
   background-position: left bottom;
}
#menu a:hover span {
   display: block;
}

#menu .home {
   width: 144px;
   height: 58px;
   background: url(images/home.gif) no-repeat;
   left: 96px;
   top: 73px;
}
#menu .home span {
   width: 86px;
   height: 14px;
   background: url(images/home-over.gif) no-repeat;
   left: 28px;
   top: -20px;
}

#menu .about {
   width: 131px;
   height: 51px;
   background: url(images/about.gif) no-repeat;
   left: 338px;
   top: 97px;
}
#menu .about span {
   width: 40px;
   height: 12px;
   background: url(images/about-over.gif) no-repeat;
   left: 44px;
   top: 54px;
}
#menu .rss {
   width: 112px;
   height: 47px;
   background: url(images/rss.gif) no-repeat;
   left: 588px;
   top: 94px;
}
#menu .rss span {
   width: 92px;
   height: 20px;
   background: url(images/rss-over.gif) no-repeat;
   left: 26px;
   top: -20px;
}
回复

使用道具 举报

0

主题

2万

回帖

115

积分

注册会员

Rank: 2

积分
115
发表于 2023-6-6 14:29:00 | 显示全部楼层
女生看了弄丢了卡萨诺的卡洛斯
回复 支持 反对

使用道具 举报

6

主题

2万

回帖

247

积分

中级会员

Rank: 3Rank: 3

积分
247
发表于 2023-6-29 03:35:53 | 显示全部楼层
挺不错的东西
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

221

积分

中级会员

Rank: 3Rank: 3

积分
221
发表于 2023-8-26 22:29:43 | 显示全部楼层
。。。。。。。。。。。。。。。
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

347

积分

中级会员

Rank: 3Rank: 3

积分
347
发表于 2023-9-8 21:28:56 | 显示全部楼层
554411515451555
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

61

积分

注册会员

Rank: 2

积分
61
发表于 2023-9-12 02:09:57 | 显示全部楼层
啦啦啦啦啦啦哈哈哈
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

186

积分

注册会员

Rank: 2

积分
186
发表于 2024-4-18 10:32:31 | 显示全部楼层
很不错的玩意
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-4-20 12:46:26 | 显示全部楼层
建军节建军节建军节建军节
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-6-1 05:24:28 | 显示全部楼层
儿童服务绯闻绯闻绯闻
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-7-17 12:18:30 | 显示全部楼层
1312315458748777
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-6 08:56 , Processed in 0.073052 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表