Js 滚动

function getPosition() {
    var top    = document.body.scrollTop;  
    var left   = document.body.scrollLeft;
    var height = document.body.clientHeight;
    var width  = document.body.clientWidth;
    return {top:top,left:left,height:height,width:width};
}

function showDiv(){
    var width  = 50;  //弹出框的宽度
    var height = 200;  //弹出框的高度
    
    var leftDiv    = document.getElementById("leftDiv");
    leftDiv.style.display  = "block";
    leftDiv.style.position = "absolute";
    leftDiv.style.zindex   = "999";
    leftDiv.style.width    = width + "px";
    leftDiv.style.height   = height + "px";
    var Position = getPosition();
    leftadd = (Position.width-width)/2;
    topadd  = (Position.height-height)/2;
    leftDiv.style.top  = (Position.top  + 10)  + "px";
    leftDiv.style.left = 20 + "px";
    
    //右边的     层
    var rightDiv    = document.getElementById("rightDiv");
    rightDiv.style.display  = "block";
    rightDiv.style.position = "absolute";
    rightDiv.style.zindex   = "999";
    rightDiv.style.width    = width + "px";
    rightDiv.style.height   = height + "px";
    var Position = getPosition();
    leftadd = (Position.width-width)/2;
    topadd  = (Position.height-height)/2;
    rightDiv.style.top  = (Position.top  + 10)  + "px";
    rightDiv.style.left = (Position.width-100) + "px";
    
    window.onscroll = function (){        
        var Position   = getPosition();
        leftDiv.style.top  = (Position.top  + 10)  +"px";
        leftDiv.style.left = 20+"px";
        //alert(Position.top);
        rightDiv.style.top  = (Position.top  + 10)  +"px";
        rightDiv.style.left = (Position.width-100) +"px";
    };
    
}

 

你可能感兴趣的:(js)