简体   繁体   中英

CSS “position: sticky” on horizontal scroll

I'm trying to build a horizontal scroll with multiple content boxes that belong to one title. So I'd like the title to stay while the content scrolls past it, until the next section with a new title comes.

Here's what I was trying to do: https://jsfiddle.net/kjo4duts/23/

 * { box-sizing: border-box; } body { margin: 0; padding: 0; } .scroll { display: flex; flex-direction: row; width: 100vw; height: 100px; background: yellow; overflow: scroll; } .item { flex-shrink: 0; width: 200px; height: 100%; margin-right: 20px; } .scroll .item .title { position: sticky; left: 0; top: 0; width: 100%; height: 40px; } .item .content { width: 100%; height: 60px; border: 3px solid green; }
 <div class="scroll"> <div class="item"> <div class="title"> Title 1 </div> <div class="content"> Content </div> </div> <div class="item"> <div class="title"> </div> <div class="content"> Content </div> </div> <div class="item"> <div class="title"> Title 2 </div> <div class="content"> Content </div> </div> <div class="item"> <div class="title"> </div> <div class="content"> Content </div> </div> <div class="item"> <div class="title"> </div> <div class="content"> Content </div> </div> </div>

Is there a way to address the "position: sticky" property to the outer parent ( .scroll )? Or is there a smooth way to do it in JavaScript? I tried to change the HTML structure, but with Flexbox you need a container for each box to get a horizontal layout..

Thanks in advance!

Edit: For anyone with the same problem. The solution is to add a relative position to the outer parent and the change the HTML structure a bit.

See updated Fiddle: https://jsfiddle.net/r2czqwn7/20/

Parent ( .scroll ) has to be position: relative at first as you can check below but I would consider different structure (all contents with same title could be in same div) to stick it over multiple items.

 * { box-sizing: border-box; } body { margin: 0; padding: 0; } .scroll { position: relative; display: flex; flex-direction: row; width: 100vw; height: 100px; background: yellow; overflow: scroll; } .item { flex-flow: column; flex-shrink: 0; width: 200px; height: 100%; margin-right: 20px; } .scroll .item .title { position: sticky; display: inline; left: 0; top: 0; width: 100%; height: 40px; } .item .content { width: 100%; height: 60px; border: 3px solid green; } .item .content:first-child { margin-top: 1rem; }
 <div class="scroll"> <div class="item"> <div class="title"> Title 1 </div> <div class="content"> Content </div> </div> <div class="item"> <div class="content"> Content </div> </div> <div class="item"> <div class="title"> Title 2 </div> <div class="content"> Content </div> </div> <div class="item"> <div class="content"> Content </div> </div> <div class="item"> <div class="content"> Content </div> </div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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