admin管理员组文章数量:1323330
I have scroll smooth by default, but for one JavaScript scrollTo() mand I'd like to override the CSS 'smooth' behavior and use auto, but the JS won't override the CSS.
What can I do?
I have scroll smooth by default, but for one JavaScript scrollTo() mand I'd like to override the CSS 'smooth' behavior and use auto, but the JS won't override the CSS.
What can I do?
Share asked Mar 22, 2020 at 3:40 Costa MichailidisCosta Michailidis 8,18818 gold badges84 silver badges136 bronze badges 2-
does the scroll smooth css have
!important
added? – Sachi.Dila Commented Mar 22, 2020 at 3:42 - @Sachi.Dila it does not. – Costa Michailidis Commented Mar 22, 2020 at 4:00
2 Answers
Reset to default 6You can set the inline style of your container's scroll to auto
, then reverting the change by altering the value of html.style.scrollBehavior
before and after scrolling programmatically. JS's ScrollToOptions
's behavior
key-value pair cannot override CSS's scroll-behavior
. The CSSOM draft mentions this:
If the user agent honors the scroll-behavior property and one of the following are true:
• behavior is "auto" and element is not null and its puted value of the scroll-behavior property is smooth
• behavior is smooth
...then perform a smooth scroll of box to position. Otherwise, perform an instant scroll of box to position.
Your user agent honors the scroll-behavior
property; this means that we're going to check one of the two conditions. When you're using window.scroll({..., behavior: 'auto'})
, we're entering the first condition. The behavior
we're setting is indeed auto
, element is indeed not null
, and the puted value of scroll-behavior
property is indeed smooth
. Therefore, a smooth scroll will happen. To make the condition false, we can override the puted value of the scroll-behavior
property to auto
by using an inline style.
Here's a simple example. Scroll programmatically without smooth behavior by clicking the Scroll down 200
button. Scroll smoothly by clicking the links.
function scrollNoSmooth() {
// Setting inline style of scroll-behavior to 'auto' temporarily
html.style.scrollBehavior = 'auto'
// This 'behavior' cannot override the CSS 'scroll-behavior'
// Do scroll
window.scroll({
top: window.scrollY + 200,
left: 0,
// behavior: 'smooth'
})
// Reverting inline style to empty
html.style.scrollBehavior = ''
}
const html = document.querySelector('html')
const fixed = document.querySelector('.fixed')
fixed.addEventListener('click', scrollNoSmooth)
html {
scroll-behavior: smooth;
position: relative;
}
a {
display: block;
height: 400px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.fixed {
display: flex;
justify-content: center;
align-items: center;
position: fixed;
width: 150px;
height: 50px;
border-radius: 5px;
background: #121212;
color: white;
cursor: pointer;
}
<div class="fixed">Scroll down 200</div>
<a name="A" href="#B">A</a>
<a name="B" href="#C">B</a>
<a name="C" href="#A">C</a>
If you only have to support chrome and/or firefox, you can use an undocumented 'behavior: instant' value to override the scroll behavior set by css, more at https://github./mdn/content/issues/2719. Example: https://jsfiddle/fr7m40kw/
document.querySelector('button').addEventListener('click', () => {
const container = document.querySelector('.container')
container.scrollTo({
top: container.scrollTop + 100,
behavior: 'instant'
})
})
.container {
height: 100px;
overflow: auto;
scroll-behavior: smooth;
}
.box {
width: 100px;
height: 50px;
background: #AAD;
margin-bottom: 10px;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<button>
Scroll
</button>
本文标签: How to override the CSS scroll behavior with scrollTo in JavaScriptStack Overflow
版权声明:本文标题:How to override the CSS scroll behavior with scrollTo in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742138388a2422471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论