how control/change speed of animation with scroll-snap CSS?
Asked Answered
G

0

8

How can we control the speed of animation with scroll-snap CSS API ?

in this exemple : https://codepen.io/newinweb/pen/EpPgdR

function toggleSnap() {
  var snapEnabled = document.getElementById('carousel').classList.toggle('snap');
  document.getElementById('otherSnappingState').innerText = snapEnabled ? 'off' : 'on';
}

function toggleDirection() {
  var isVertical = document.getElementById('carousel').classList.toggle('vertical');
  document.getElementById('otherScrollState').innerText = isVertical ? 'horizontal' : 'vertical';
}
#carousel {
  /* Ensure that the contents flow horizontally */
  overflow-x: auto;
  white-space: nowrap;
  display: flex;
}

#carousel.vertical {
  flex-direction: column;
}

/* 2018 spec - For Safari 11, Chrome 69+ */
#carousel.snap {
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch; /* Needed to work on iOS Safari */
}

#carousel.snap > div {
  scroll-snap-align: center;
}

#carousel.snap.vertical {
  flex-direction: column;
  scroll-snap-type: y mandatory;
}



/* 2015 spec - For Firefox, Edge, IE */
#carousel.snap {
  scroll-snap-type: mandatory;
  -ms-scroll-snap-type: mandatory;
  scroll-snap-points-x: repeat(100%);
  -ms-scroll-snap-points-x: repeat(100%);
}

#carousel.snap.vertical {
  scroll-snap-points-x: initial;
  -ms-scroll-snap-points-x: initial;
  scroll-snap-points-y: repeat(100%);
  -ms-scroll-snap-points-y: repeat(100%);
}


/* Below here is styling, not important for the actual example, just to make it look nice. No need to look down here! */

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#carousel {
  position: relative;
  width: 100%;
  height: 100%;
}

#carousel > div {
  min-width: 100%;
  min-height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  color: #fff;
  font-size: 20px;
}

#carousel-1 {
  background-color: #e34747;
}

#carousel-2 {
  background-color: #5ab92c;
}

#carousel-3 {
  background-color: #226de2;
}

#controls {
  position: fixed;
  bottom: 10px;
  left: 10px;
}

#controls button {
  padding: 5px 10px;
}
<div id="carousel" class="snap">
  <div id="carousel-1">Item 1<br>Start scrolling ...</div>
  <div id="carousel-2">Item 2</div>
  <div id="carousel-3">Item 3</div>
</div>

<div id="controls">
  <button onclick="toggleSnap()">Turn Snapping <span id="otherSnappingState">off</span></button>
  <button onclick="toggleDirection()">Change scroll to <span id="otherScrollState">vertical</span></button>
</div>

it works fine, but imagine I want the transition between two elements (the scroll action) have a specific duration (5 seconds for exemple)

how can we do that ? Same question for controle delay if it's possible (like with animation CSS)

Griselgriselda answered 8/7, 2021 at 12:25 Comment(4)
Its ridiculous that it seems there's no CSS features to change this yetZobkiw
you can use GSAP snapMusk
@Musk what is this? Can you make an exemple ?Griselgriselda
@Griselgriselda sure. GSAP is popular animation library JS. greensock.com/st-demos and its easy on GSAP. for demos: greensock.com/st-demosMusk

© 2022 - 2024 — McMap. All rights reserved.