如题,content宽度为1200px,代码初版如下所示:

<script type="text/javascript" >
    function menuFixed(id){
        $('#rightanswer').css('height', document.documentElement.clientHeight*0.86+'px');
        $('#viewerPlaceHolder').css('height', document.documentElement.clientHeight*0.86+'px');
        $('#viewQuestionBankDiv').css('height', document.documentElement.clientHeight*0.86+'px');
        var obj = document.getElementById(id);
        var _getHeight = obj.offsetTop;

        window.onscroll = function(){
            changePos(id,_getHeight);
        }
    }
    function changePos(id,height){
        var obj = document.getElementById(id);
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        if(scrollTop < height){
            obj.style.position = 'relative';
            obj.style.top="0px";
        }else{
            obj.style.position = 'fixed';
            obj.style.top="80px";
            document.getElementById('flowId').style.minHeight='800px';
        }
    }

    window.onload = function(){
        menuFixed('nav_keleyi_com');
        $(".addNewScroll").mCustomScrollbar();
    }
</script>


clinetWidth大于1200px,页面效果,即期望效果: 

wKiom1iqqjTAtHGzAACLKoBOyYk500.png-wh_50

wKioL1iqqjWQZ4l2AAB1KFHR3DE556.png-wh_50


clinetWidth小于1200px,页面效果

wKioL1iqpX3D0WXOAAJJFeO9z3g857.png-wh_50

wKiom1iqpX7xfocNAAIvkU6L7XE650.png-wh_50


代码完善:

<script type="text/javascript" >
    function menuFixed(id){
        $('#rightanswer').css('height', document.documentElement.clientHeight*0.86+'px');
        $('#viewerPlaceHolder').css('height', document.documentElement.clientHeight*0.86+'px');
        $('#viewQuestionBankDiv').css('height', document.documentElement.clientHeight*0.86+'px');
        var obj = document.getElementById(id);
        var _getHeight = obj.offsetTop;

        window.onscroll = function(){
            changePos(id,_getHeight);
        }
    }
    function changePos(id,height){
        var obj = document.getElementById(id);
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        if(scrollTop < height){
            obj.style.position = 'relative';
            obj.style.top="0px";
            document.getElementById('nav_keleyi_com').style.left='0';
        }else{
            obj.style.position = 'fixed';
            obj.style.top="80px";
            document.getElementById('flowId').style.minHeight='800px';

            var cw = document.documentElement.clientWidth;
            if(cw < 1200){ //用于解决 --》 当浏览器宽度小于1200时,因固定定位的文档脱流而使得scroll-x左右拖动后,右侧内容被盖住的问题, ‘20’指的是wrap的padding值
                var sl=-Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
                var sl= sl+20;
                document.getElementById('nav_keleyi_com').style.left= sl +'px';
            }else {
                var fixed_left = parseInt((cw-1200)/2 + 20); //计算出sw大于1200时,左右的margin大小
                document.getElementById('nav_keleyi_com').style.left= fixed_left +'px';
            }
        }
    }
</script>