$(window).resize(function(){
if($(window).width()<1150){ //浏览器宽度小于155时
$("#div1").css({
"position":"absolute" //改变样式
});
}else{
$("#div1").css({
"position":"fixed", //改变样式
});
}
});
$(window).scroll(function(){
if($(window).scrollTop()>90){ //垂直滚动条钓offset 大于90时。
$("#div1").css({
"top":"0", //修改相关div样式
});
}else{
$(".left-nav").css({
"top":"92px", //修改相关div样式
});
}
});
类似与淘宝的导航固定
<!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>
<script src="jquery%201.11.1/jquery-1.11.1.min.js"language="javascript" type="text/javascript"></script>
<style>
*{
margin:0px;
padding:0px;
}
.nav{
background:#000000;
height:57px;
line-height:57px;
color:#ffffff;
text-align:center;
font-family:"微软雅黑";
font-size:30px;
}
.fixedNav{
position:fixed;
top:0px;
left:0px;
width:100%;
z-index:100000;
/*IE6就要加下面两行代码*/
/*position:absolute;*/
/*_top:expression(eval(document.documentElement.scrollTop));*/
}
</style>
</head>
<body>
<div class="header" style="background:#CCCC00;height:100px;"></div>
<div class="nav">
<p>类似与淘宝的导航固定</p>
</div>
<div class="content" style="background:#0099FF; height:2000px;"></div>
<script type="text/javascript">
$(function(){
$(window).scroll(function() {
if($(window).scrollTop()>=100){
$(".nav").addClass("fixedNav");
}else{
$(".nav").removeClass("fixedNav");
}
});
});
</script>
</body>
</html>
转载于:https://my.oschina.net/lskweb/blog/285122