admin管理员组

文章数量:1323684

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
Add a ment  | 

2 Answers 2

Reset to default 6

You 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