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

258
Views
Section with fade effect on scroll working fine by scrolling down, but looks not so good by scrolling up

currently I am trying to create a page where individual fixed images fade in and out as you scroll. Above the images is text. Fading in while scrolling down works very well so far, but unfortunately fading out doesn't look so nice. This is because I have arranged the images with Position: sticky and they are only shown when they are 100% in the viewport. By scrolling up this works just fine, but scrolling down you can see the upper edge of the image while it is moving out of view.

Unfortunately I'm not a javascript professional and I can't find the page where I found the code i'm currently using. Otherwise I would have looked there again or linked it here.

It would be great if it would be enough to extend the javascript code.

This is what it looks like now: https://codepen.io/nejou/pen/yLvggoe

This is the script:

    (function(){
        function hasClass(el, cls) {
            if (el.className.match('(?:^|\\s)'+cls+'(?!\\S)')) { return true; } 
            }
        function addClass(el, cls) {
            if (!el.className.match('(?:^|\\s)'+cls+'(?!\\S)')){ el.className += ' '+cls; } 
            }
        function delClass(el, cls) {
            el.className = el.className.replace(new RegExp('(?:^|\\s)'+cls+'(?!\\S)'),'');
            }
    
        function elementFromTop(elem, classToAdd, distanceFromTop, unit) {
            var winY = window.innerHeight || document.documentElement.clientHeight,
                distTop = elem.getBoundingClientRect().top,
                distPercent = Math.round((distTop / winY) * 100),
                distPixels = Math.round(distTop),
                distUnit;
            distUnit = unit == 'percent' ? distPercent : distPixels;
            if (distUnit <= distanceFromTop) {
                if (!hasClass(elem, classToAdd)) { addClass(elem, classToAdd); }
            } else {
                delClass(elem, classToAdd);
                }
            }
        // params: element id, class to add, distance from top, unit ('percent' or 'pixels')
    
        window.addEventListener('scroll', function() {
            elementFromTop(document.getElementById('fade-background-01'), 'active', 0, 'pixels');
            }, false);
        window.addEventListener('scroll', function() {
            elementFromTop(document.getElementById('fade-background-02'), 'active', 0, 'pixels');
            }, false);
        window.addEventListener('scroll', function() {
            elementFromTop(document.getElementById('fade-background-03'), 'active', 0, 'pixels');
            }, false);
        window.addEventListener('scroll', function() {
            elementFromTop(document.getElementById('fade-background-04'), 'active', 0, 'pixels');
            }, false);
    })();
    

Thanks in advance

about 4 years ago · Juan Pablo Isaza
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!