这两天做了一个简单的小网页,由于是初次接触HTML5和CSS,只实现了最基本的功能,后续会再对功能样式进行填充优化,这里记录一下。
实现效果:可通过切换上面的导航栏实现左侧导航栏的切换,并通过左侧导航栏的切换实现右侧具体导航内容的展示。
代码如下:
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
/*<!--横向导航栏的宽度、高度和整体样式-->*/
#TopNav {
height: 40px;
background: #b3d4fc;
border-radius: 5px;
padding-left: 300px;
}
/*<!--导航各个栏目的宽度、高度和对齐方式-->*/
#TopNav ul li {
list-style-type: none;
width: 110px;
height: 40px;
float: left;
text-align: left;
line-height: 40px;
}
/*<!--导航选项不显示下划线-->*/
#TopNav ul li a{
text-decoration: none;
}
/*<!--左侧导航栏样式-->*/
#sidebar_left {
background: #cccccc;
padding: 10px;
height: 500px;
width: 150px;
}
/*<!--左侧导航栏导航项样式-->*/
#sidebar_left nav ul li {
list-style-type: none;
width: 90px;
font-size: 20px;
padding-top: 15px;
}
/*<!--导航选项不显示下划线-->*/
#sidebar_left ul li a{
text-decoration: none;
}
#sidebar_right {
width:350px;
float:left;
padding:10px;
margin-left: 190px;
margin-top: 60px;
font-size: 20px;
align-content: center;
}
/*<!--页脚样式-->*/
#footer {
background-color:#b3d4fc;
color:white;
clear:both;
text-align:center;
border-radius: 5px;
padding:5px;
}
</style>
<body>
<div id="TopNav">
<nav>
<ul>
<li><a href="#/DefaultPage">首页</a></li>
<li><a href="#/SendInfo">信息发布</a></li>
<li><a href="#/Club">俱乐部</a></li>
<li><a href="#/Appraisal">民主测评</a></li>
</ul>
</nav>
</div>
<div id="SendInfo" style="display: none">
<nav>
<ul>
<li><a href="#/ActivityDynamic">活动动态</a></li>
<li><a href="#/EmployeeBenefits">职工福利</a></li>
</ul>
</nav>
</div>
<div id="Club" style="display:none">
<nav>
<ul>
<li><a href="#/FootBall">足球</a></li>
<li><a href="#/BasketBall">篮球</a></li>
<li><a href="#/Badminton">羽毛球</a></li>
<li><a href="#/Yoga">瑜伽</a></li>
</ul>
</nav>
</div>
<div id="Appraisal" style="display:none">
<nav>
<ul>
<li><a href="#/Suggestions">合理建议</a></li>
<li><a href="#/Questionnaire">调查问卷</a></li>
</ul>
</nav>
</div>
<div id="sidebar_right">
<p>PostManagementSystem</p>
</div>
<div id="sidebar_left">
<nav>
<ul>
<li><a href="#/ActivityDynamic">活动动态</a></li>
<li><a href="#/EmployeeBenefits">职工福利</a></li>
</ul>
</nav>
</div>
<div id="ActivityDynamics" style="display: none">
<p>This is Activity Dynamics!</p>
</div>
<div id="Employeebenefits" style="display: none">
<p>This is Employee benefits!</p>
</div>
<div id="FootBall" style="display: none">
<p>This is Football!</p>
</div>
<div id="BasketBall" style="display: none">
<p>This is BasketBall!</p>
</div>
<div id="Badminton" style="display: none">
<p>This is Badminton!</p>
</div>
<div id="Yoga" style="display: none">
<p>This is Yoga!</p>
</div>
<div id="Suggestions" style="display: none">
<p>This is Suggestions!</p>
</div>
<div id="Questionnaire" style="display: none">
<p>This is Questionnaire!</p>
</div>
<div id="footer">
Copyright www.bbcc.cn
</div>
</body>
<script>
(function(){
var Router = function () {
this.routes = {};//保存路由
this.curUrl = '';//获取当前的hash值
}
Router.prototype.init = function () {
//hashchange事件,当hash变化时,调用reloadPage方法
//第一个this指向window,bind里面的this指向调用这个函数的对象,具体使用方法可以百度
window.addEventListener('hashchange', this.reloadPage.bind(this));
}
Router.prototype.reloadPage = function () {
this.curUrl = location.hash.substring(1) || '/';//获取当前hash的值(去掉#)
this.routes[this.curUrl](); //运行当前hsah值对应的函数
}
Router.prototype.map = function( key, callback ){ //保存路由对应的函数:
this.routes[key] = callback; //key表示hash的值(去掉#),callback表示当前hash对应的函数
}
window.oRou = Router;
})
(function(){
var Router = function () {
this.routes = {};//保存路由
this.curUrl = '';//获取当前的hash值
}
Router.prototype.init = function () {
//hashchange事件,当hash变化时,调用reloadPage方法
//第一个this指向window,bind里面的this指向调用这个函数的对象,具体使用方法可以百度
window.addEventListener('hashchange', this.reloadPage.bind(this));
}
Router.prototype.reloadPage = function () {
this.curUrl = location.hash.substring(1) || '/';//获取当前hash的值(去掉#)
this.routes[this.curUrl](); //运行当前hsah值对应的函数
}
Router.prototype.map = function( key, callback ){ //保存路由对应的函数:
this.routes[key] = callback; //key表示hash的值(去掉#),callback表示当前hash对应的函数
}
window.oRou = Router;
})
var oRouter2 = new oRou();
oRouter2.init();
oRouter2.map('/DefaultPage',function () {
var leftNav = document.getElementById("sidebar_left");
var sendInfoControl = document.getElementById("SendInfo").innerHTML;
leftNav.innerHTML = sendInfoControl;
})
oRouter2.map('/SendInfo',function () {
var leftNav = document.getElementById("sidebar_left");
var sendInfoControl = document.getElementById("SendInfo").innerHTML;
leftNav.innerHTML = sendInfoControl;
var rightNav = document.getElementById("sidebar_right");
var ActivityDynamicControl = document.getElementById("ActivityDynamics").innerHTML;
rightNav.innerHTML = ActivityDynamicControl;
})
oRouter2.map('/Club',function () {
var leftNav = document.getElementById("sidebar_left");
var clubControl = document.getElementById("Club").innerHTML;
leftNav.innerHTML = clubControl;
var rightNav = document.getElementById("sidebar_right");
var footBallControl = document.getElementById("FootBall").innerHTML;
rightNav.innerHTML = footBallControl;
})
oRouter2.map('/Appraisal',function () {
var leftNav = document.getElementById("sidebar_left");
var appraisalControl = document.getElementById("Appraisal").innerHTML;
leftNav.innerHTML = appraisalControl;
var rightNav = document.getElementById("sidebar_right");
var suggestionsControl = document.getElementById("Suggestions").innerHTML;
rightNav.innerHTML = suggestionsControl;
})
oRouter2.map('/ActivityDynamic',function () {
var rightNav = document.getElementById("sidebar_right");
var ActivityDynamicControl = document.getElementById("ActivityDynamics").innerHTML;
rightNav.innerHTML = ActivityDynamicControl;
})
oRouter2.map('/EmployeeBenefits',function () {
var rightNav = document.getElementById("sidebar_right");
var employeeBenefitsControl = document.getElementById("Employeebenefits").innerHTML;
rightNav.innerHTML = employeeBenefitsControl;
})
oRouter2.map('/FootBall',function () {
var rightNav = document.getElementById("sidebar_right");
var footBallControl = document.getElementById("FootBall").innerHTML;
rightNav.innerHTML = footBallControl;
})
oRouter2.map('/BasketBall',function () {
var rightNav = document.getElementById("sidebar_right");
var basketBallControl = document.getElementById("BasketBall").innerHTML;
rightNav.innerHTML = basketBallControl;
})
oRouter2.map('/Badminton',function () {
var rightNav = document.getElementById("sidebar_right");
var badmintonControl = document.getElementById("Badminton").innerHTML;
rightNav.innerHTML = badmintonControl;
})
oRouter2.map('/Yoga',function () {
var rightNav = document.getElementById("sidebar_right");
var yogaControl = document.getElementById("Yoga").innerHTML;
rightNav.innerHTML = yogaControl;
})
oRouter2.map('/Suggestions',function () {
var rightNav = document.getElementById("sidebar_right");
var SuggestionsControl = document.getElementById("Suggestions").innerHTML;
rightNav.innerHTML = SuggestionsControl;
})
oRouter2.map('/Questionnaire',function () {
var rightNav = document.getElementById("sidebar_right");
var QuestionnaireControl = document.getElementById("Questionnaire").innerHTML;
rightNav.innerHTML = QuestionnaireControl;
})
</script>
</html>