滚动贴合

coderljw 2024-10-13 CSS
  • Magic
  • 特效
小于 1 分钟

<main>
  <section></section>
  <section></section>
  <section></section>
</main>
1
2
3
4
5
main {
  height: 400px;
  scroll-snap-type: y mandatory;
  overflow-y: scroll;
}

section {
  height: 400px;
  background: center/cover no-repeat;
  scroll-snap-align: start;
}

section:nth-child(1) {
  background-image: url(../../images/Wives/BingBing-3.webp);
}

section:nth-child(2) {
  background-image: url(../../images/Wives/BingBing-4.webp);
}

section:nth-child(3) {
  background-image: url(../../images/Wives/BingBing-5.webp);
}

::-webkit-scrollbar {
  width: 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

旋转后横向滚动

<div class="container">
  <main>
    <section>
      <div class="img"></div>
    </section>
    <section>
      <div class="img"></div>
    </section>
    <section>
      <div class="img"></div>
    </section>
  </main>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
.container {
  height: 400px;
  overflow: hidden;
  --demo-width: 778px; /* 为了适配屏幕,宽度使用 JS 动态设置 */
}

main {
  height: var(--demo-width);
  scroll-snap-type: y mandatory;
  overflow-y: scroll;
  transform: rotate(-90deg) translateX(calc(-1 * var(--demo-width)));
  transform-origin: left top;
}

section {
  width: var(--demo-width);
  height: var(--demo-width);
  scroll-snap-align: start;
  transform: rotate(90deg) translateY(calc(-1 * var(--demo-width)));
  transform-origin: left top;
}

.img {
  width: var(--demo-width);
  height: 400px;
  background: center/cover no-repeat;
}

section:nth-child(1) .img {
  background-image: url(../../images/Wives/BingBing-3.webp);
}

section:nth-child(2) .img {
  background-image: url(../../images/Wives/BingBing-4.webp);
}

section:nth-child(3) .img {
  background-image: url(../../images/Wives/BingBing-5.webp);
}

::-webkit-scrollbar {
  width: 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const container = document.querySelector('.container')

const setDemoWidth = () => {
  demoWidth = container.parentNode.offsetWidth
  if (!demoWidth) return setDemoWidth()
  container.style.setProperty('--demo-width', `${demoWidth}px`)
}

// 此 DEMO 中 onload 事件无效,可恶
setTimeout(setDemoWidth)
window.onresize = setDemoWidth
1
2
3
4
5
6
7
8
9
10
11
以父之名
周杰伦.mp3