JQ 智能判断固定导航条或侧边栏

这是一个JQ只能判断固定DIV层的特效代码,通过这个JQ智能代码,你可以设置导航栏、侧边栏、任何DIV层的固定显示;现在蓝叶的站就用的这个JQ智能判断固定的导航条,要看演示的就请下拉页面滚动条,就会看到导航条固定了;使用这个代码需要在页面里引用JQ库的JS文件,现在的网站一般都引用了JQ库代码,如果没有那就打开你网站的模板,在页头或者页位加上<script src="http://libs.baidu.com/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>代码保存,这就成功引用的JQ库代码;具体JQ智能判断固定导航条或侧边栏代码如何使用请见下面的说明:

<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
.topfixed {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	z-index: 9000;
	webkit-box-shadow: 0px 4px 4px #baacc9;
	-moz-box-shadow: 0px 4px 4px #baacc9;
	box-shadow: 0px 4px 4px #baacc9;
	background: #fff;
}
</style>
</head>
<body>
<div id="nav" style=" width:100%; height:20px; background-color:#f60; margin:0 auto;"></div>
<div style="height:10000px;"></div>
<script src="http://libs.baidu.com/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
$(window).scroll(function(){
if($(this).scrollTop()>220){
$('#nav').addClass('topfixed');} 
else {
$('#nav').removeClass('topfixed');
}});
</script>
<!--说明:当下拉滚动条距离头部220px的时候,就会给#nav这个样式的DIV层添加一个CSS样式topfixed使其固定显示。
上面代码里的220就是下拉滚动条距离头部的距离,如何设置请自己修改,#nav是你需要固定的DIV层CSS样式名。的DIV层CSS样式名。-->
</body>
</html>

效果图:

 

猜你喜欢

转载自onestopweb.iteye.com/blog/2288194
jq