英文:
Disable CSS scroll snap after certain point
问题
OK, 所以我有7个部分,前4个部分是CMYK,最后3个部分是RGB。
我正在尝试停止在RGB部分上进行滚动捕捉。
如果我将scroll-snap-align: start;
应用于CMYK部分,当我滚动到.bg-key
(黑色)部分的末尾时,它总是强制我回到.bg-key
(黑色)的开头,当我尝试向下滚动时...
如果我尝试在.bg-red
部分使用scroll-snap-type: none;
,使用波浪号和星号来获取.bg-red
之后的所有元素,什么都不会发生?每个部分仍然会捕捉?请看这里...
有人能看出我在哪里出错了吗...?
如果可能的话,我希望能够在某一点之后禁用CSS捕捉。谢谢!
英文:
OK so I have 7 sections, first 4 sections are CMYK, and last 3 sections are RGB.
I am trying to stop scroll snapping on sections RGB.
If I apply scroll-snap-align: start;
to CMYK sections, when I scroll to end of section .bg-key
(black) it always forces me to snap back to start of .bg-key
(black) as I try to scroll down...
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
html {
overflow-y: scroll;
scroll-snap-type: y mandatory;
scroll-behavior: smooth;
}
section {
height: 100vh;
position: relative;
color: white;
}
.bg-cyan {
background: cyan;
scroll-snap-align: start;
}
.bg-magenta {
background: magenta;
scroll-snap-align: start;
}
.bg-yellow {
background: yellow;
color: black;
scroll-snap-align: start;
}
.bg-key {
background: black;
scroll-snap-align: start;
}
.bg-red {
background: red;
}
.bg-blue {
background: blue;
}
.bg-green {
background: green;
}
<!-- language: lang-html -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet"/>
<main>
<section class="bg-cyan">CYAN</section>
<section class="bg-magenta">MAGENTA</section>
<section class="bg-yellow">YELLOW</section>
<section class="bg-key">BLACK</section>
<section class="bg-red">RED</section>
<section class="bg-blue">BLUE</section>
<section class="bg-green">GREEN</section>
</main>
<!-- end snippet -->
See fiddle... https://jsfiddle.net/joshmoto/qday0r9o/4/
<br/>
If I try using scroll-snap-type: none;
on .bg-red
section using tilde asterisk to get all elements after .bg-red
, nothing happens? Every section still snaps? See here...
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
html {
overflow-y: scroll;
scroll-snap-type: y mandatory;
scroll-behavior: smooth;
}
section {
height: 100vh;
position: relative;
scroll-snap-align: start;
color: white;
}
/* Disable scroll snapping after .bg-red section */
section.bg-red ~ * {
scroll-snap-type: none;
}
.bg-cyan {
background: cyan;
}
.bg-magenta {
background: magenta;
}
.bg-yellow {
background: yellow;
color: black;
}
.bg-key {
background: black;
}
.bg-red {
background: red;
}
.bg-blue {
background: blue;
}
.bg-green {
background: green;
}
<!-- language: lang-html -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet"/>
<main>
<section class="bg-cyan">CYAN</section>
<section class="bg-magenta">MAGENTA</section>
<section class="bg-yellow">YELLOW</section>
<section class="bg-key">BLACK</section>
<section class="bg-red">RED</section>
<section class="bg-blue">BLUE</section>
<section class="bg-green">GREEN</section>
</main>
<!-- end snippet -->
See fiddle... https://jsfiddle.net/joshmoto/mbey5p6s/38/
<br/>
Can anyone see where I'm going wrong here...?
Would love to be able to disable css snapping after a certain point if possible?
Thanks!
答案1
得分: 1
当我将HTML元素中的 "scroll-snap-type" 更改为 "both" 时,在您的第一个示例中,黑色部分不会回弹。但目前的情况下,可用性很差。
英文:
When I change the "scroll-snap-type" to "both" on the html element, in your first example the black section does not snap back.
But as it is, the usability is poor.
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-css -->
html {
overflow-y: scroll;
scroll-snap-type: both;
scroll-behavior: smooth;
}
section {
height: 100vh;
position: relative;
color: white;
}
.bg-cyan {
background: cyan;
scroll-snap-align: start;
}
.bg-magenta {
background: magenta;
scroll-snap-align: start;
}
.bg-yellow {
background: yellow;
color: black;
scroll-snap-align: start;
}
.bg-key {
background: black;
scroll-snap-align: start;
}
.bg-red {
background: red;
}
.bg-blue {
background: blue;
}
.bg-green {
background: green;
}
<!-- language: lang-html -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet"/>
<main>
<section class="bg-cyan">CYAN</section>
<section class="bg-magenta">MAGENTA</section>
<section class="bg-yellow">YELLOW</section>
<section class="bg-key">BLACK</section>
<section class="bg-red">RED</section>
<section class="bg-blue">BLUE</section>
<section class="bg-green">GREEN</section>
</main>
<!-- end snippet -->
答案2
得分: 1
I had to use jQuery in the end to do a little hack to set HTML css scroll-snap-type
value to initial
...
$(window).on('scroll', function() {
if ($(window).scrollTop() > $('.bg-key').offset().top) {
$('html').addClass('no-scroll-snap');
} else {
$('html').removeClass('no-scroll-snap');
}
});
html {
overflow-y: scroll;
scroll-snap-type: y mandatory;
scroll-behavior: smooth;
}
html.no-scroll-snap {
scroll-snap-type: initial;
}
section {
height: 100vh;
position: relative;
scroll-snap-align: start;
color: white;
}
.bg-cyan {
background: cyan;
}
.bg-magenta {
background: magenta;
}
.bg-yellow {
background: yellow;
color: black;
}
.bg-key {
background: black;
}
.bg-red {
background: red;
}
.bg-blue {
background: blue;
}
.bg-green {
background: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet" />
<main>
<section class="bg-cyan">CYAN</section>
<section class="bg-magenta">MAGENTA</section>
<section class="bg-yellow">YELLOW</section>
<section class="bg-key">BLACK</section>
<section class="bg-red">RED</section>
<section class="bg-blue">BLUE</section>
<section class="bg-green">GREEN</section>
</main>
英文:
I had to use jQuery in the end to do a little hack to set HTML css scroll-snap-type
value to initial
...
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(window).on('scroll', function() {
if ($(window).scrollTop() > $('.bg-key').offset().top) {
$('html').addClass('no-scroll-snap');
} else {
$('html').removeClass('no-scroll-snap');
}
});
<!-- language: lang-css -->
html {
overflow-y: scroll;
scroll-snap-type: y mandatory;
scroll-behavior: smooth;
}
html.no-scroll-snap {
scroll-snap-type: initial;
}
section {
height: 100vh;
position: relative;
scroll-snap-align: start;
color: white;
}
.bg-cyan {
background: cyan;
}
.bg-magenta {
background: magenta;
}
.bg-yellow {
background: yellow;
color: black;
}
.bg-key {
background: black;
}
.bg-red {
background: red;
}
.bg-blue {
background: blue;
}
.bg-green {
background: green;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet" />
<main>
<section class="bg-cyan">CYAN</section>
<section class="bg-magenta">MAGENTA</section>
<section class="bg-yellow">YELLOW</section>
<section class="bg-key">BLACK</section>
<section class="bg-red">RED</section>
<section class="bg-blue">BLUE</section>
<section class="bg-green">GREEN</section>
</main>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论