News新闻

业界新闻动态、技术前沿
Who are we?

您的位置:首页      JS/JQ/AJAX      拉动悬浮于顶部的JS控制代码

拉动悬浮于顶部的JS控制代码

发布日期:2014-09-18 00:00:00 957

有2个DIV,在未到达位置的时候css控制,一左一右。拉动到某位置的时候,需要他一直悬浮显示,位置不变。代码如下,其中有对浏览器判断:

<script type="text/javascript">
var obj11 = document.getElementById("title_f4");
var top11 = getTop(obj11);
var obj10 = document.getElementById("left_row");
var isIE6 = /msie 6/i.test(navigator.userAgent);
window.onscroll = function(){
var bodyScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (bodyScrollTop > top11){
obj11.style.position = (isIE6) ? "absolute" : "fixed";
obj10.style.position = (isIE6) ? "absolute" : "fixed";
obj11.style.top = (isIE6) ? bodyScrollTop + "px" : "0px";
obj10.style.top = (isIE6) ? bodyScrollTop + "px" : "0px";
obj10.style.margin = (document.body.clientWidth>1200) ? "0px 0px 0px 980px" : "0px 0px 0px 780px";
} else {
obj11.style.position = "static";
obj10.style.position = "static";
obj10.style.margin = "0px";
}
}
function getTop(e){
var offset = e.offsetTop;
if(e.offsetParent != null) offset += getTop(e.offsetParent);
return offset;
}
</script>