Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css - How to slow down inertia/momentum scrolling on mobile

Tags:

html

css

scroll

I am working on a scrollable card gallery of sorts. For this I am using a CSS grid structure. Something along the line of:

<div class="cs-wrapper">
    <div class="cs-container">
        <div id="card-1" class="cs-card-container">
            <div class="cs-card">
            </div>
        </div>
        <div id="card-2" class="cs-card-container">
            <div class="cs-card">
            </div>
        </div>
        ...
    </div>
</div>

Using the css

.cs-content {
    height:100vh;
    position: relative;

    display: grid;
    grid-template-columns: var(--cs-container-grid-gap) 1fr var(--cs-container-grid-gap);
    grid-gap: var(--container-grid-gap);
    z-index:10;
}
.cs-container {
    display: grid;
    position: relative;
    grid-gap: 0vw; 
    grid-template-columns:repeat(auto-fill, 100vw); 
    grid-auto-flow: column;
    grid-auto-columns: calc( 100% / var(--cs-cards-per-screen)); 
    grid-template-rows: 1fr;

    -webkit-overflow-scrolling: touch; 
    scroll-behavior: smooth;
    overflow-scrolling: touch; 
    overflow-x: scroll;
    scroll-snap-type: x mandatory;
}

.cs-container > div {   
    height: 100%;
    width: 100%;
    position: relative;
    margin:0;
    overflow: auto;

    scroll-snap-align: start;
}
.cs-card-container {
    height: 100%;
    width: 100%;
    position: relative;
    margin:0;
    overflow: auto;
}

Now the code works as intended, the cards are positioned right and the scroll works.

However the inertia/momentum scrolling on mobile tends to scroll a bit to fast, when scrolling on mobile you have to swipe very sensibly to get to the next card and not skip 1 or 2 cards (you have to be able to swipe trough multipe cards if you'd wish to, so a stop on each card isn't an option).

Now I am looking for a way to slow down the scroll speed so it becomes usable

I'd prefer a CSS only solution if possible. Things I have tried:

Playing around with

.cs-container {
  perspective: 1px;
  transform-style: preserve-3d;
}
.cs-container > div {   
  transform: translateZ(-1px) scale(2);
  margin-top:-10px;
  margin-left:-10px;
  top:-10px;
}

this did seem to slow the scroll down a bit, however the scroll acted unpredictable and as the divs are now in the background and scroll half as fast. as soon as cs-container has scrolled to the end only half the cards have been scrolled through (also the scroll snapp is now off by half a card)

Javascript

I tried a couple javascript solutions I found, but all of them relied on the scrolling being done with the mousewheel, so for mobile this didn't work

like image 267
Eric Avatar asked Oct 10 '19 09:10

Eric


People also ask

What is Webkit overflow scrolling touch?

The -webkit-overflow-scrolling CSS property controls whether or not touch devices use momentum-based scrolling for a given element.

How do I turn off auto scroll in CSS?

Disable in the browser: go to chrome://flags/#enable-scroll-anchoring and set "Scroll anchoring" to "Disabled".

What is momentum scroll?

Web pages on iOS by default have a “momentum” style scrolling where a flick of the finger sends the web page scrolling and it keeps going until eventually slowing down and stopping as if friction is slowing it down.


1 Answers

Did not run the code myself, but it sounds like you are looking for the property called:

{scroll-snap-stop: always}

like image 126
Noex98 Avatar answered Oct 19 '22 23:10

Noex98