Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow scrolling until last element

I have a div with some element inside it and I would like to allow the scrolling of the div until the last element.

This is what happens when I scroll: enter image description here

And this is how I would like to make it: enter image description here

Is it possible to do it?

like image 523
MeV Avatar asked Dec 11 '22 22:12

MeV


2 Answers

Well, it is quite simple without any javascript:

HTML:

<div>
    <section>hello</section>
    <section>hello</section>
    <section>hello</section>
    <section>hello</section>
    <section>hello</section>
    <section>hello</section>
    <section>hello</section>
</div>

CSS:

section { height: 100px; }

section:last-child { height: 100%; }

div {
    overflow: scroll;
    height: 400px;
    border: 1px solid black;
}

See fiddle. The concept is just to use the parent div height as a height for the last item.

like image 181
smnbbrv Avatar answered Dec 23 '22 09:12

smnbbrv


Try achieve this using JS. Set a bottom margin to a last category equal to wrapper height minus last category height.

var wrapperHeight = $("#wrapper").innerHeight();
var lastCategory = $(".category:last-child");
var lastCategoryHeight = lastCategory.height();
var bottomMargin = wrapperHeight - lastCategoryHeight;
lastCategory.css({margin: "0 0 "+bottomMargin+"px 0"});

DEMO

like image 34
phts Avatar answered Dec 23 '22 09:12

phts