简体   繁体   English

当父div触摸浏览器底部时,将孩子固定在底部

[英]stick child to bottom when parent div touches the browser bottom

I want to stick child div to bottom when parent div touches the browser bottom. 当父div触摸浏览器底部时,我想将子div固定在底部。

PS : This should happen when the parent div is pushed down not when scrolled down. PS:应该在将父div向下推时而不是向下滚动时发生。

For example in my demo there is a content panel which is hidden. 例如,在我的演示中,有一个隐藏的内容面板。 If you click on expand link you get to see the expanded content (which is pushing the bottom_content div to the bottom). 如果单击扩展链接,您将看到扩展内容(将bottom_content div推到底部)。

But accordion is just an example, there will be some other element which will be pushing the bottom_content div down. 但是手风琴只是一个例子,还会有其他一些因素会降低bottom_content div。 So I dont want to write stick function with reference to accordion. 所以我不想参考手风琴编写stick函数。

It should stick down only when bottom_content div touches the bottom of the browser and when there is no much content in the page then the child div should stay as it is like child of the bottom_content 仅当bottom_content div接触到浏览器底部并且页面中没有太多内容时,它才应停留在该位置,则子div应该保持bottom_content就像bottom_contentbottom_content

Parent div: bottom_content 父级div: bottom_content
Child div: footer 儿童div: footer

Here is my code currently used, which is not proper 这是我目前使用的代码,不合适

$('.expandble_conetnt a').click(function(event){
        event.preventDefault();
        $(this).next('span').slideToggle();     
    }); 

//this is for stick to the bottom
var stickyHeaderbottom = $('.footer').offset().top;

    $(window).scroll(function () {
        if ($(window).scrollTop() > stickyHeaderbottom) {
            $('.footer').css({ position: 'fixed', bottom: '0px', width: '95%', left: '2.5%' });
        } else {
            $('.footer').css({ position: 'static', bottom: '0px', width: '100%' });

        }
    });

DEMO DEMO

The whole idea is to handle .footer position on window scrolling, on window resizing and after .slideToggle() for the list is completed: 整个想法是处理.footer上位置window滚动,在window大小调整后.slideToggle()为列表完成:

Fiddle . 小提琴

var stickyHeaderbottom = $('.footer').offset().top;

$('.expandble_conetnt a').click(function()
{
    $(this).next('span').slideToggle(function()
    {
        handleBottom();
    });
    return false;
}); 

$(window).scroll(function()
{
    handleBottom();
});

$(window).resize(function()
{
    handleBottom();
});

function handleBottom()
{
    if ($(window).height() + $(window).scrollTop() < $(document).height())
    {
        $('.footer').css({ position: 'fixed', bottom: '0', width: '95%', left: '2.5%' });
    }
    else
    {
        $('.footer').css({ position: 'absolute', bottom: '0', width: '100%', left: 0 });
    }
}

Edit . 编辑 Updated fiddle without weird footer jumping after list opening. 更新小提琴 ,打开列表后没有奇怪的页脚跳动。

var stickyHeaderbottom = $('.footer').offset().top;

$('.expandble_conetnt a').click(function()
{
    var toggledElement = $(this).next('span');
    handleBottom(toggledElement.height());
    toggledElement.slideToggle(function()
    {
        handleBottom();
    });
    return false;
}); 

$(window).scroll(function()
{
    handleBottom();
});

$(window).resize(function()
{
    handleBottom();
});

function handleBottom(additionalHeight)
{
    var pageHeight = $(document).height();
    if (additionalHeight)
    {
        pageHeight += additionalHeight;
    }
    if ($(window).height() + $(window).scrollTop() < pageHeight)
    {
        $('.footer').css({ position: 'fixed', bottom: '0', width: '95%', left: '2.5%' });
    }
    else
    {
        $('.footer').css({ position: 'absolute', bottom: '0', width: '100%', left: 0 });
    }
}

您可以使js on.change将div位置更改为绝对位置,并且将bottom更改为left:0 left:0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM