Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 向下滚动时,如何使div固定在底部?_Jquery_Css_Position_Fixed - Fatal编程技术网

Jquery 向下滚动时,如何使div固定在底部?

Jquery 向下滚动时,如何使div固定在底部?,jquery,css,position,fixed,Jquery,Css,Position,Fixed,我创造了一把小提琴 我只想在向下滚动时,将底部的DIV#标签固定到浏览器窗口的底部,直到其底部边框(在我的示例中为底部红色边框)。我该怎么做 提前谢谢 您只需计算滚动偏移+窗口高度,即可获得窗口的底部,然后检查其是否大于元素偏移+元素高度。此外,如果确实希望将元素的下边框固定到下边框,则必须从元素中删除下边框 代码是这样的: $(document).ready(function() { var s = $("#sticker"); var pos = s.offset().to

我创造了一把小提琴

我只想在向下滚动时,将底部的
DIV#标签固定到浏览器窗口的底部,直到其底部边框(在我的示例中为底部红色边框)。我该怎么做


提前谢谢

您只需计算滚动偏移+窗口高度,即可获得窗口的底部,然后检查其是否大于元素偏移+元素高度。此外,如果确实希望将元素的下边框固定到下边框,则必须从元素中删除下边框

代码是这样的:

$(document).ready(function() {
    var s = $("#sticker");
    var pos = s.offset().top+s.height(); //offset that you need is actually the div's top offset + it's height
    $(window).scroll(function() {
        var windowpos = $(window).scrollTop(); //current scroll position of the window
        var windowheight = $(window).height(); //window height
        if (windowpos+windowheight>pos) s.addClass('stick'); //Currently visible part of the window > greater than div offset + div height, add class
        else s.removeClass('stick');
    });
});

我对你的html进行了一些编辑,因此你可以正确地看到它(添加了更多的滚动条),但你可以在

处看到小提琴或全屏版本,如果红线不可见(滚动条还没有到达红线),你希望文本粘到页面底部,一旦你到达红线,文本就应该保持不变?@BeNdErR:我希望每次向下滚动直到我看到红线为止,DIV#贴纸不再浮动,但当我继续向下滚动时会被固定),但当我向上滚动时,它又浮动了。