"Floating" sidebar inside an element when scrolling

Asked

Viewed 1,259 times

6

I have a layout Bootstrap with a right sidebar and I need this "turn" (up/down) when doing scroll, but not above the header and footer. just "float" inside the div[role=main]

My layout: inserir a descrição da imagem aqui

My code:

<header class="container">
    <div class="row">
        <div class="col-sm-12">Header</div>
    </div>
</header>

<div role="main" class="container">
    <div class="row">
        <div class="col-sm-8 col-lg-9">
            Main Content
        </div>

        <aside class="col-sm-4 col-lg-3">
            <section class="budget-summary">
                <h2 class="text-center">Resumo</h2>                
            </section>
        </aside>
    </div>
</div>

<footer>
    <div class="row">
        <div class="col-sm-12">Footer</div>
    </div>
</footer>

EDIT:

I managed to run the bar with the affix plugin of bootstrap:

$('.budget-summary').affix({
    offset: {
        top: 0,
        bottom: function () {
            return (this.bottom = $('footer#page-footer').outerHeight(true));
        }
    }
});

but now a problem has arisen. The content of the sidebar is dynamic (grows and decreases). which sometimes causes the content of the sidebar not to be shown in full, as exemplified by the following figure:

inserir a descrição da imagem aqui

there’s a way around this?

  • Miguel, did you solve the problem?

  • partially...

  • What is missing?

  • is in question. when content is increased, it gets cut

  • 1

    Miguel, looking at his Dit, seems to be the case for a new, separate question rather than an issue in this one. Because the problem has now changed, right? The new question does not invalidate the answers you have already received?

  • you are right. open a new http://answall.com/questions/44972/affix-numa-barra-lateral-dynamica

  • Miguel has now seen your new question. Have you tested the answer I gave? in that answer the code adapts to the height of the sidebar.

  • yes, I tried, but it had mts bugs, and since bootstrap already brings a plugin for k keria, it is easier to use it. thx

Show 3 more comments

2 answers

6

You will need to use Javascript to read the scroll and change the position of this sidebar.

A suggestion (using jQuery because I assume that using Bootstrap has jQuery) is:

var main = $('div[role=main]').outerHeight();
var posicaoFooter = $('footer').offset().top;
var sidebarDiv = $('aside');

$(window).on('scroll', function () {
    var scroll = $(this).scrollTop();
    if (scroll + sidebarDiv.outerHeight() < main) sidebarDiv.css('margin-top', scroll);
});

Example: http://jsfiddle.net/s8h9eeL0/

In this code when the scroll happens it will compare the scroll value + the height of the sidebar and will change the position while that value together is less than the total height of the main. You may need CSS adjustments but I think this is the idea you need and you can see in the example how it works.

2


  • thanks for the reply. I tried this way but it didn’t work properly. I added the affix-top The sidebar and the bar started to rotate, but when you scroll it shrinks, when you get to the bottom of the page you get the normal width, and it stops working (again at the bottom of the page). when I add the position:absolute it stops working at all. http://prntscr.com/5hx3md http://prntscr.com/5hx3ul http://prntscr.com/5hx44o

  • @Miguelborges I updated the answer because I was wrong about the affix-top, it is not a requirement for the functioning. Only with the images you have placed it is not possible to find out what the problem is. Could you link to the site or create a Jsfiddle? Also try using the browser console to see what changes the css in the element in each state, maybe what you need is just to add the property width with a fixed value or 100%.

  • thanks. the problem was that it had to indicate a width and put the following style so it does not jump when it reaches the bottom (.affix-bottom { position: absolute; }). however now another problem has arisen. I updated the question

  • @Miguelborges the problem as to the height happens by the fact of this position: absolute in the .affix-bottom whereas the plugin arrow the attribute top element. Try to find solutions to your previous problem that I believe is easier to solve. Use the browser console and make attempts with your knowledge. You can even learn new things about CSS when trying to solve these problems. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.