Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change CSS of inner div when scroll reaches that div

I am attempting to implement a scroll function where the CSS of the inner div's change when it reaches a certain height from the top.

var $container = $(".inner-div");
var containerTop = $container.offset().top;
var documentTop = $(document).scrollTop();
var wHeight = $(window).height();
var minMaskHeight = 0;
var descriptionMax = 200;
var logoMin = -200;
var maskDelta = descriptionMax - minMaskHeight;
var $jobOverview = $container.find(".right");
var $jobLogo = $container.find(".left");

var curPlacementPer = ((containerTop - documentTop) / wHeight) * 100;
var topMax = 85;
var center = 20;
var bottomMax = -15;

//console.log("Placement: " + curPlacementPer);

function applyChanges(perOpen) {
  var maskHeightChange = maskDelta * (perOpen / 100);
  var opacityPer = perOpen / 100;
  var newDescriptionLeft = descriptionMax - maskHeightChange;
  var newLogoLeft = logoMin + maskHeightChange;
  if (newDescriptionLeft <= 0) newDescriptionLeft = 0;
  if (newLogoLeft >= 0) newLogoLeft = 0;
  if (opacityPer >= 1) opacityPer = 1;
  $jobOverview.css({
    transform: "translate(" + newDescriptionLeft + "%,-50%)",
    opacity: opacityPer
  });
  $jobLogo.css({
    transform: "translate(" + newLogoLeft + "%,-50%)",
    opacity: opacityPer
  });
}

if (window.innerWidth > 640) {
  $container.removeClass("mobile");
  // console.log("Placement: " + curPlacementPer);

  if (curPlacementPer <= topMax /*&& curPlacementPer >= center*/ ) {
    var perOpen = ((topMax - curPlacementPer) / 25) * 100;
    applyChanges(perOpen);
  } else if (curPlacementPer < center /*&& curPlacementPer >= bottomMax*/ ) {
    var perOpen = (((bottomMax - curPlacementPer) * -1) / 25) * 100;
    applyChanges(perOpen);
  } else {
    $jobOverview.css({
      transform: "translate(200%,-50%)",
      opacity: "0"
    });
    $jobLogo.css({
      transform: "translate(-300%,-50%)",
      opacity: "0"
    });
  }
<div class="outer-div">
  <div class="inner-div first">
    <div class="left"></div>
    <div class="right"></div>
  </div>
  <div class="inner-div second">
    <div class="left"></div>
    <div class="right"></div>
  </div>
  <div class="inner-div third">
    <div class="left"></div>
    <div class="right"></div>
  </div>
  <div class="inner-div fourth">
    <div class="left"></div>
    <div class="right"></div>
  </div>
</div>

Currently, all of the inner div's gets changed at the same time.
I noticed that when I change the $container class to equal '.first' and specify it more, it works.

Is there any way to make the inner div's change separately, relative to its height from the top? Any way I can iterate the scroll function so I can add more inner div's in the future and not have to worry about changing my scroll function?

like image 868
Etep Avatar asked Sep 26 '17 20:09

Etep


People also ask

How do I keep my Div fixed when scrolling?

The interesting rule here is the ' position: fixed ', that makes the DIV stay fixed on the screen. The ' top: 50% ' and ' right: 0 ' determine where the DIV is displayed, in this case: 50% down from the top of the window, and a constant 0px from the right.

How do I move an inner div?

To move the inner div container to the centre of the parent div we have to use the margin property of style attribute. We can adjust the space around any HTML element by this margin property just by providing desired values to it.

Which position will keep the element in the same place regardless of scrolling?

A fixed position element is positioned relative to the viewport, or the browser window itself. The viewport doesn't change when the window is scrolled, so a fixed positioned element will stay right where it is when the page is scrolled.


1 Answers

In raw JavaScript, this is my answer:

// Define the element -- The '#fooBar' can be changed to anything else.
var element = document.querySelector("#fooBar");

// Define how much of the element is shown before something happens.
var scrollClipHeight = 0 /* Whatever number value you want... */;

// Function to change an element's CSS when it is scrolled in.
const doSomething = function doSomething() {

    /** When the window vertical scroll position plus the
     *   window's inner height has reached the
     *   top position of your element.
    */
    if (
           (window.innerHeight + window.scrollY) - (scrollClipHeight || 0) >= 
           element.getBoundingClientRect().top
    )
        // Generally, something is meant to happen here.
        element.style = "/* Yay, some CSS! */"
};

// Call the function without an event occurring.
doSomething();

// Call the function when the 'window' scrolls.
addEventListener("scroll", doSomething, false)

This is the method I use. If there are other methods, I'd love to see them as well but this is my answer for now.

like image 74
Lapys Avatar answered Sep 20 '22 19:09

Lapys