Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the CSS scroll behavior with scrollTo in JavaScript

Tags:

javascript

css

I have scroll smooth by default, but for one JavaScript scrollTo() command I'd like to override the CSS 'smooth' behavior and use auto, but the JS won't override the CSS.

What can I do?

like image 819
Costa Michailidis Avatar asked Oct 20 '25 15:10

Costa Michailidis


2 Answers

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 computed 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 computed value of scroll-behavior property is indeed smooth. Therefore, a smooth scroll will happen. To make the condition false, we can override the computed 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>
like image 70
Richard Avatar answered Oct 22 '25 05:10

Richard


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.com/mdn/content/issues/2719. Example: https://jsfiddle.net/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>
like image 29
n0099 Avatar answered Oct 22 '25 04:10

n0099



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!