在用户窗口屏幕的末尾设置Div



我想做的只是让这个div显示在用户屏幕的最后,以显示一些新闻,这应该是一项直接的工作,实际上这个div突然从页面上消失了!如果我放大或缩小浏览器,它就会出现在它应该出现的位置!!问题出在哪里?

<style>
        #news_div{
            background-color: red;
            width:800px;
            height:40px;
            padding:0;
            margin:0;
            position: fixed;
            z-index: 1;
        }
    </style>
</head>
<body>
<div id="news_div"> The news </div>
<script>
$( function(){
    $("#news_div").offset({
        top : $(window).height(),
    })});

好的,top属性指定DIV顶部到窗口顶部的距离。如果将DIVtop属性设置为窗口的top,则DIV正好在窗口之外或可见区域之外。

你必须减去DIV本身的高度才能到达底部:

$(function() {
    $("#news_div").offset({
        top : $(window).height() -$('#news_div').height()
    })
});

最新更新