js中如何获取某个元素到浏览器最左和最右的距离
<head>
<meta charset="utf-8">
<style>
body,div{margin:0;padding:0;border:0}
#div1{width:300px;height:200px;border:1px solid red;margin-left:50px}
</style>
</head>
<body>
<div id="div1">
div元素
</div>
<script type="text/javascript">
//获取元素
var div = document.getElementById("div1");
//获取左边与父元素的距离
alert('左边距离是:' + div.offsetLeft + ' 像素');
//获取右边距离
alert('右边距离是:' + (screen.width - div.offsetLeft - div.offsetWidth) + ' 像素');
</script>
</body>
js中获取某个元素到浏览器最左和最右的距离的程序代码是:
<!doctype html><html><head><meta charset="UTF-8"><style>
body{margin: 0;padding: 0;}
.mdiv{width: 100px;height: 100px;background-color: red;}
</style></head><body><div style="height: 1000px"></div><div></div><script src="jquery.js"></script> //自行下载<script>//原生//获取div距离顶部的距离
var mTop = document.getElementsByClassName('banner')[0].offsetTop;
//减去滚动条的高度var sTop = document.body.scrollTop;var result = mTop - sTop;console.log(result);//Jquery
mTop = $('.banner')[0].offsetTop;
sTop = $(window).scrollTop();
result = mTop - sTop;
console.log(result);
</script></body>