skip to Main Content

What is the best way to create a vertical scroll when one card is stacked on top of another, similar to the example shown in the link below?

Link here

I’ve searched some information and examples but haven’t found anything.

If anyone knows or has examples, please share.

2

Answers


  1. Absolute positioning calculated dynamically in Javascript.

    let topc = 30
    Array.from(document.querySelector('.cards').children).forEach(c => {
      hue = Math.floor(Math.random() * 256)
      c.style.backgroundColor = 'hsl(' + hue + ', 50%, 50%)'
      c.style.top = topc + 'px'
      topc += 50
    })
    .cards {
      font-family: sans-serif;
      font-size: 20px;
      color: white;
    }
    .cards>div {
      padding: 10px;
      border-radius: 10px;
      position: absolute;
      width: 300px;
      box-shadow: 0 -2px 5px rgb(0,0,0,0.5);
    }
    <div class="cards">
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id quam eu ipsum viverra gravida.</div>
      <div>Nunc feugiat hendrerit leo, at finibus purus efficitur et. Sed vel semper libero.</div>
      <div>Pellentesque feugiat, tortor sollicitudin tristique elementum, diam lacus mattis libero, eu tincidunt purus ligula nec magna.</div>
      <div>Sed at vulputate enim, ut rutrum nulla. Curabitur fermentum a dolor et cursus.</div>
      <div>Nunc eleifend nec sem ac suscipit. Curabitur ut viverra mi. Nam lacinia sem lacus.</div>
      <div>Integer aliquet ipsum in eros molestie maximus. Praesent erat lacus, suscipit ut rutrum eu, consequat eget ex.</div>
      <div>tMaecenas interdum nunc vitae risus euismod gravida. Pellentesque ultrices pharetra malesuada.he</div>
      <div>Pellentesque lacus magna, hendrerit mollis odio in, volutpat consectetur elit. Nulla facilisi.</div>
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id quam eu ipsum viverra gravida.</div>
      <div>Nunc feugiat hendrerit leo, at finibus purus efficitur et. Sed vel semper libero.</div>
      <div>Pellentesque feugiat, tortor sollicitudin tristique elementum, diam lacus mattis libero, eu tincidunt purus ligula nec magna.</div>
      <div>Sed at vulputate enim, ut rutrum nulla. Curabitur fermentum a dolor et cursus.</div>
      <div>Nunc eleifend nec sem ac suscipit. Curabitur ut viverra mi. Nam lacinia sem lacus.</div>
      <div>Integer aliquet ipsum in eros molestie maximus. Praesent erat lacus, suscipit ut rutrum eu, consequat eget ex.</div>
      <div>tMaecenas interdum nunc vitae risus euismod gravida. Pellentesque ultrices pharetra malesuada.he</div>
      <div>Pellentesque lacus magna, hendrerit mollis odio in, volutpat consectetur elit. Nulla facilisi.</div>
    </div>
    Login or Signup to reply.
  2. use margin-top ‘minus’px

    .card_wrap{
      width:200px;
      height:400px;
      border:solid 1px black;
      padding:20px;
      box-sizing:border-box;
      overflow-y: scroll;
      background:beige;
    }
    .card_wrap .card{  
      background:pink;
      padding:1rem;
      width:100%;
      height:100px;  
      margin-top:-50px;
      box-shadow:inset 0 0 0 2px gray;
      border-radius:8px;
      box-sizing:border-box;
      cursor:pointer;
    }
    .card_wrap .card:hover{
      background:skyblue;
    }
    .card_wrap .card:hover + .card{
      margin-top:0;
    }
    .card_wrap .card:nth-of-type(1){
      margin-top:0;
    ]
    <div class="card_wrap">
      <div class="card">card1</div>
      <div class="card">card2</div>
      <div class="card">card3</div>
      <div class="card">card4</div>
      <div class="card">card5</div>
      <div class="card">card6</div>
      <div class="card">card7</div>
      <div class="card">card8</div>
      <div class="card">card9</div>
      <div class="card">card10</div>
      <div class="card">card11</div>
      <div class="card">card12</div>
      <div class="card">card13</div>
      <div class="card">card14</div>
      <div class="card">card15</div>
      <div class="card">card16</div>
      <div class="card">card17</div>
      <div class="card">card18</div>
      <div class="card">card19</div>
      <div class="card">card20</div>
      <div class="card">card21</div>
      <div class="card">card22</div>
      <div class="card">card23</div>
      <div class="card">card24</div>
      <div class="card">card25</div>
      <div class="card">card26</div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search