<!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>百姓市场_www.bxsc.net</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$(window).scroll(function() {
var init_pos = $('#bxsc_net').offset().top;
$('#bxsc_net').css("top", $(window).scrollTop()+80);
var window_width = $(window).width(); /*浏览器宽度*/
var window_height = $(window).height(); /*浏览器高度*/
$('#bxsc_net').html(
"浏览宽:"+ window_width + " 浏览高:" + window_height + "<br>" +
"Y 坐标:"+ $('#bxsc_net').offset().top + " Z 坐标:" + $('#bxsc_net').offset().left + "<br>" +
"Y 坐标:"+ $('#bxsc_net').position().top + " Z 坐标:" + $('#bxsc_net').position().left
);
});
});
</script>
</head>
<body>
<div style=" height:3000px;"></div>
<div id="bxsc_net" style="position: absolute; left: 0; top:80px; z-index: 2; background: #a9c9ef; margin-left:6px; width: 300px; height: 200px"></div>
</body>
</html>
获取页面某一元素的绝对X,Y坐标,可以用offset()方法如上图蓝色区域表示
var X = $('#bxsc_net').offset().top;
var Y = $('#bxsc_net').offset().left;
获取相对(父元素)位置如上图蓝色区域表示
var X = $('#bxsc_net').position().top;
var Y = $('#bxsc_net').position().left;
$(".nav-item").hover(function(){
var window_width = $(window).width(); /*浏览器宽度*/
var window_height = $(window).height(); /*浏览器高度*/
$("#bpw").html(
"浏览宽:"+ window_width + " 浏览高:" + window_height + "<br>" +
"offset Y 坐标:"+ $(this).offset().top + " Z 坐标:" + $(this).offset().left + "<br>" +
"position Y 坐标:"+ $(this).position().top + " Z 坐标:" + $(this).position().left
);
})