前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JS-制作可伸缩的水平菜单栏

JS-制作可伸缩的水平菜单栏

作者头像
xing.org1^
发布2018-05-17 16:40:52
4.5K0
发布2018-05-17 16:40:52
举报
文章被收录于专栏:前端说吧前端说吧
代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
<meta name="author" content="郭菊锋,702004176@qq.com" />
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>可伸缩的导航菜单</title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
				font-size: 14px;
			}
 
			a {
				color: #333;
				text-decoration: none
			}
 
			ul {
				list-style: none;
				height: 30px;
				border-bottom: 5px solid #F60;
				margin-top: 20px;
				padding-left: 50px;
			}
 
			ul li {
				float: left
			}
 
			ul li a {
				display: block;
				height: 30px;
				text-align: center;
				line-height: 30px;
				width: 120px;
				background: #efefef;
				margin-left: 1px;
			}
 
			a.on,
			a:hover {
				background: #F60;
				color: #fff;
			}
		</style>
		<script>
			window.onload = function() {
				var aA = document.getElementsByTagName('a');
				for(var i = 0; i < aA.length; i++) {
					aA[i].onmouseover = function() {
						var This = this;
						clearInterval(This.time);//一开始要清楚定时器!!删了一下试试还是可以的呀。
						This.time = setInterval(function() {
							This.style.width = This.offsetWidth + 8 + "px";
							if(This.offsetWidth >= 160)//这里,只有一行代码,所以不加花括号也是可以的。
								clearInterval(This.time);
						}, 30)
					}
					aA[i].onmouseout = function() {//和over时的效果一样,就是缩回来罢了
						clearInterval(this.time);
						var This = this;
						this.time = setInterval(function() {
							This.style.width = This.offsetWidth - 8 + "px";
							if(This.offsetWidth <= 120) {//offsetWidth还有这个妙用,这的是元素的实际宽度吗?
								This.style.width = '120px';
								clearInterval(This.time);
							}
						}, 30)
					}
				}
			}
		</script>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
		<!--调用jq-->
		<!--<script>
			//		jq的制作方法
			$(function() { //$()==js里面的:window.onload = function(){};让页面加载完成之后再进行调用函数。
				$('a').hover(
					//$('x')选择器,选择标签是x的,括号里的引号里添加的是标签名
					//hover方法,表示鼠标经过的时候的效果
					//下设两个函数,一个鼠标移入动作,一个鼠标移出动作。
					function() {
						$(this).stop().animate({
							"width": "160px"
						}, 200); //jq自带的animate动画效果
					},
					function() {
						$(this).stop().animate({
							"width": "120px"
						}, 200);
					}
				)
			})
		</script>-->
	</head>
	<body>
		<ul>
			<li>
				<a class="on" href="#">首  页</a>
			</li>
			<li>
				<a href="#">新闻快讯</a>
			</li>
			<li>
				<a href="#">产品展示</a>
			</li>
			<li>
				<a href="#">售后服务</a>
			</li>
			<li>
				<a href="#">联系我们</a>
			</li>
		</ul>
		<p>来自慕课网教程
			<a>http://www.imooc.com/video/93</a>
		</p>
	</body>
	
	原生js代码
	<script>
 2             window.onload = function() {
 3                 var aA = document.getElementsByTagName('a');
 4                 for(var i = 0; i < aA.length; i++) {
 5                     aA[i].onmouseover = function() {
 6                         var This = this;
 7                         clearInterval(This.time);
 8                         This.time = setInterval(function() {
 9                             This.style.width = This.offsetWidth + 8 + "px";
10                             if(This.offsetWidth >= 160)
11                                 clearInterval(This.time);
12                         }, 30)
13                     }
14                     aA[i].onmouseout = function() {
15                         clearInterval(this.time);
16                         var This = this;
17                         this.time = setInterval(function() {
18                             This.style.width = This.offsetWidth - 8 + "px";
19                             if(This.offsetWidth <= 120) {
20                                 This.style.width = '120px';
21                                 clearInterval(This.time);
22                             }
23                         }, 30)
24                     }
25                 }
26             }
27         </script>
	</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-09-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档