Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
JavaScript to change CSS dynamically when scrolling

I want to change some css properties when scrolling. This worked with jQuery and I can't make it work using JavaScript.

This is the function that reads and set CSS properties:

function parallel_height_js(){
  let scroll_top = window.scrollY;
  console.log('scroll top = ',scroll_top)
  let header_height = document.getElementsByClassName("sample-header-section")[0].clientHeight;
  console.log('Header height = ',header_height)
  document.getElementsByClassName("text-section")[0].style.marginTop = header_height;
  console.log('text margin top = ',document.getElementsByClassName("text-section")[0].style.marginTop)
  document.getElementsByClassName("sample-header")[0].style.height = header_height - scroll_top;
  console.log('header height = ',document.getElementsByClassName("sample-header")[0].style.height)
}

Afterwards, I execute the following to execute the function at scrolling or resizing:

parallel_height_js();

window.onscroll = parallel_height_js;
window.onresize = parallel_height_js;

In the browser console I get this from console.log:

scroll top =  8
Header height =  517
text margin top =  
header height =  

I checked that I am accessing the right elements when trying to set the CSS value, but this CSS value is not changing.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The error was because I didn't add 'px' string when setting new CSS values. So, the js code would be:

import './style.css'

function parallel_height_js(){
  let scroll_top = window.scrollY;
  let header_height = document.getElementsByClassName("sample-header-section")[0].clientHeight;
  document.getElementsByClassName("text-section")[0].style.marginTop = header_height+'px';
  document.getElementsByClassName("sample-header")[0].style.height = (header_height - scroll_top)+'px';
}
parallel_height_js();

window.onscroll = parallel_height_js;
window.onresize = parallel_height_js;

HTML:

<header>
        <div class="sample-header">
          <div class="...">
            <input type="checkbox" id="menu" />
            <label for="menu"></label>
            <div class="menu-content">
              <ul>
                <li><a href="">Contact</a> </li>
                <li><a href="">About Us</a> </li>
              </ul>
            </div>
          </div>
          <div class="sample-header-section">
            <h1>...</h1>
            <h2>...</h2>
          </div>
        </div>
      </header>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!