调用 document.documentElement.offsetTop 就可以,这个是你浏览器当前内容距离顶部的坐标onscroll 和 onresize 你自己定义一下就可以
解决方案 »
- js效果请高手指点!!!!
- IP地址转化为市级城市地址
- iframe1.document.form1.submit()这样不行,那要怎样才可以呢,就是父页面要提交iframe里页面的表单
- 关于如何让一个自动添加的option的值放在第一位的问题
- 简单问题(50分)!动态生成的层在不同分辨率中定位的问题!
- 如何用javascript改变当前窗口的属性
- 求救一下:关于判断上传图片大小的问题
- 动态的显示表格!
- 【求助】blank与_blank有区别吗?
- SOS:怎样实现在窗口一打开就让它最小化?
- 关于jquery的一个问题。
- jquery easy ui combobox function()参数问题
<!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=gb2312" />
<title>固定浮动侧边栏</title>
<style type="text/css">
body {
margin: 10px auto;
font-family: sans-serif;
width: 500px;
}
div {
border-radius: 5px;
box-shadow: 1px 2px 5px rgba(0,0,0,0.3);
border: 1px solid #ccc;
padding: 5px;
}
#sidebarWrap {
height: 400px;
width: 210px;
float: right;
position: relative;
box-shadow: none;
border: none;
margin: 0;
padding: 0;
}
#main {
width: 270px;
height: 4000px;
}
#footer {
clear: both;
margin: 10px 0;
}
#sidebar {
width: 200px;
height: 300px;
position: absolute;
}
#header {
height: 200px;
margin-bottom: 10px;
}
#sidebar.fixed {
position: fixed;
top: 0;
}
#footer { height: 600px; }
</style>
</head><body>
<div id="header">header</div>
<div id="sidebarWrap">
<div id="sidebar">Sidebar</div>
</div>
<div id="main">
Main
</div>
<div id="footer">
footer
</div>
</body>
</html>
<script src="../Js/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/, 0));
var footTop = $('#footer').offset().top - parseFloat($('#footer').css('marginTop').replace(/auto/, 0)); var maxY = footTop - $('#sidebar').outerHeight(); $(window).scroll(function(evt) {
var y = $(this).scrollTop();
if (y > top) {
if (y < maxY) {
$('#sidebar').addClass('fixed').removeAttr('style');
} else {
$('#sidebar').removeClass('fixed').css({
position: 'absolute',
top: (maxY - top) + 'px'
});
}
} else {
$('#sidebar').removeClass('fixed');
}
});
});
</script>