Search code examples
jqueryhtmlcsssasspug

Certain element won't fade in when I scroll past a certain portion of a section


I have a little problem with my code.

As you can see, my example has a three sections. More exactly landing-page, section and about. For the last two I used CSS grid to split them each in two subsections.

I'm trying to achieve a transition on scroll using jquery. Like, when I scroll past a 1/3 of section, my left element starts to fade from opacity:0. Respectively this should happen with my right element from my about section, but with my code this happens as soon as I scroll past a 1/3 of section.

I tried changing the class of my right.hidden to right.hide and it stopped fading completely.

Bellow I've attached my code and here is a link to my CodePen

Jade

.landing-page
.section
  .left.hidden
  .right
.about
  .left
  .right.hidden 

Sass

.landing-page
  height: 100vh
  width: 100vw
  background: gray
.section
  height: 100vh
  width: 100vw
  display: grid
  grid-template-columns: repeat(2, 1fr)
  grid-template-areas: 'left right' 'left right'
  .left
    grid-area: left
    background: orangered
    transition: 2000ms
  .left.hidden
    opacity: 0
  .right
    grid-area: right
    background: lightblue
.about
  height: 100vh
  width: 100vw
  display: grid
  grid-template-columns: repeat(2, 1fr)
  grid-template-areas: 'left right' 'left right'
  .left
    grid-area: left
    background: lightgreen
  .right
    grid-area: right
    background: orangered
    transition: 2000ms
  .right.hidden
    opacity: 0

Jquery

$(document).ready(function() {
  var sectionLeftEl = $('.left'),
      sectionRightEl = $('.right'),
      sectionLeftElOffset = sectionLeftEl.offset().top / 3,
      sectionRightElOffset = sectionRightEl.offset().top / 3,
      documentEl = $(document);

  documentEl.on('scroll', function() {
    if (documentEl.scrollTop() > sectionLeftElOffset && sectionLeftEl.hasClass('hidden')) sectionLeftEl.removeClass('hidden');
    if (documentEl.scrollTop() > sectionRightElOffset && sectionRightEl.hasClass('hidden') sectionRightEl.removeClass('hidden');

  });
});

Solution

  • If I understand you correctly, the calculation is wrong (additional of the selectors issue which @Temani Afif mention).

    The section offset (right or left) should be

    sectionTopOffset - 2 / 3 * sectionHeight

    Just imagine (or test it) that when you start to scroll, you can see the top of .section. So you want that whenever you exceed the .section in the 1/3 it's like to say 2 / 3 * .section.height()

    So:

    $(document).ready(function() {
      var sectionLeftEl = $('.section .left'),
        sectionRightEl = $('.about .right'),
        sectionLeftElOffset = sectionLeftEl.offset().top - (2 * sectionLeftEl.height() / 3),
        sectionRightElOffset = sectionRightEl.offset().top - (2 * sectionRightEl.height() / 3),
        documentEl = $(document);
    
      documentEl.on('scroll', function() {
        if (documentEl.scrollTop() > sectionLeftElOffset && sectionLeftEl.hasClass('hidden')) sectionLeftEl.removeClass('hidden');
        if (documentEl.scrollTop() > sectionRightElOffset && sectionRightEl.hasClass('hidden')) sectionRightEl.removeClass('hidden');
      });
    });
    .landing-page {
      height: 100vh;
      width: 100vw;
      background: gray;
    }
    
    .section {
      height: 100vh;
      width: 100vw;
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      grid-template-areas: "left right" "left right";
    }
    
    .section .left {
      grid-area: left;
      background: orangered;
      transition: 2000ms;
    }
    
    .section .left.hidden {
      opacity: 0;
    }
    
    .section .right {
      grid-area: right;
      background: lightblue;
    }
    
    .about {
      height: 100vh;
      width: 100vw;
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      grid-template-areas: "left right" "left right";
    }
    
    .about .left {
      grid-area: left;
      background: lightgreen;
    }
    
    .about .right {
      grid-area: right;
      background: orangered;
      transition: 2000ms;
    }
    
    .about .right.hidden {
      opacity: 0;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="landing-page"></div>
    <div class="section">
      <div class="left hidden"></div>
      <div class="right"></div>
    </div>
    <div class="about">
      <div class="left"></div>
      <div class="right hidden"></div>
    </div>

    https://codepen.io/moshfeu/pen/jxmYyp?editors=0010