Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use <ul> to display scrolling contents?

I came across https://minimill.co/ and saw it as a good example to what I'm trying to achieve.

I made an attempt to display the listed items like the site:

.wrap {
    display: block;
    list-style: none;
    position: relative;
    padding: 0;
    margin: 0;
    border: 0;

    li {
        background-color: green;
    }
}

.content {
    margin: 0 auto;
    max-width: 66rem;
    width: 90%;
    padding: 0;
    border: 0;
    position: relative;
}

.right-details {
    display: inline-block;
    float: right;
    box-size: border-box;
    width: 33.33333%;
}

.left-img {
    display: inline-block;
    float: left;
    box-sizing: border-box;
    width: 66.66666%;

    img {
        width: 50px;
    }
}
<ul class="wrap">
    <li>
        <div class="content">
            <div class="left-img">
                <img src="/assets/img/macbook-image.png"/>
            </div>
            <h2 class="right-details">
                Item 1
            </h2>
        </div>
    </li>
    <li>
        <div>
            <h2>
                Item 2
            </h2>
        </div>
    </li>
</ul>

But the first <li> disappears.

How can I display my contents in one long scroll-like how https://minimill.co/ is doing? Am I doing it correctly as implemented on the site? Any guidance or insight on mimicking it more closely would be appreciated.

like image 517
Jo Ko Avatar asked Feb 14 '17 20:02

Jo Ko


People also ask

How do I add a scroll bar in UL?

defining the max-height to 63 will make the ul behave normally if it stays under 63px high, but if it exceeds that, it will overflow and show a scroll bar. If you want to show more items, just increase the max-height to the desired height.

How do you make a scrolling list in HTML?

It's kind of a tricky problem, but one easy way to solve it is to follow a few simple steps: (1) List all of your items in individual div s. (2) Put all items into a containing div , and limit the height of that div to a fixed size. (3) Then, style the containing div to set overflow: scroll .

How do I scroll from an element to a view?

Say your page displays a list of names and you want a certain person to be highlighted and scrolled into view. There's a browser API for that: Element. scrollIntoView() , which scrolls an element into view.


1 Answers

You should use min-height:100vh instead of height:100vh;. Please check my fiddle

// select all elements with data-background attribute
var lis = document.querySelectorAll("[data-background]");
// create empty array
var heights = [];
// use for loop to "discover" all of the elements in lis array
for(var i = 0; i < lis.length; i++){
  // get element's distance from top
  var distanceFromTop = lis[i].offsetTop;
  // get value from data-backgrount attribute
  var background = lis[i].getAttribute("data-background");
  // push background and distance to heights array
  heights.push({background: background, distance: distanceFromTop});
};

// check if page was scrolled
window.addEventListener("scroll", function(evt){
    // if page was scrolled what's the user's distance from top
	var distanceFromTop = this.scrollY;
 
  // find distances in heights array
  heights.forEach(function(height) {
    // check if user reached another checkpoint
    if(height.distance < distanceFromTop) {
            // if so, change the background to value that we got from data-background attribute
            // 
			document.body.className = height.background;
    }
  });
});
body {
  transition: background-color .8s ease;
  -webkit-transition: background-color .8s ease;
}

body.blue { background-color: #39f; }
body.red { background-color: #FF351A; }
body.dark { background-color: #222; }
body.yellow { background-color: #fd3; }
body.deep-blue { background-color: #417ABA; }
body.white { background-color: #fff; }
body.beige { background-color: #F7D693; }

li {
  min-height: 100vh;
  list-style-type:none;
}
<body class="blue">
  <ul>
    <li data-background="blue"></li>
    <li data-background="red"></li>
    <li data-background="dark"></li>
    <li data-background="yellow"></li>
    <li data-background="deep-blue"></li>
    <li data-background="white"></li>
    <li data-background="beige"></li>
  </ul>
</body>

You should use min-height:100vh instead of height:100vh;. Please check my fiddle

like image 167
Mattonit Avatar answered Sep 29 '22 09:09

Mattonit