0

In the past, i've created a function, to get multiple Node list's properties. Then, with the properties create visual effets. It looks like this:

var elem, celElem, elemTop = [],
  elemMag = [],
  elemBot = [],
  elemCent = [],
  winTop, winMag, winBot, winCent, i, x, len, elemCsokk = [],
  elemNo = [],
  elemCentValt = [],
  elemCentWinCent = [];

function getElem(el, cEl, fn) {
  if (el.length === undefined) {
    el = [el];
    cEl = [cEl];
  }
  elem = el;
  celElem = cEl;
  winTop = window.pageYOffset;
  winMag = window.innerHeight;
  winBot = winTop + winMag;
  winCent = winTop + (winMag / 2);
  for (i = 0, len = elem.length; i < len; i++) {
    elemTop[i] = 0;
    elemMag[i] = elem[i].offsetHeight;
    elemBot[i] = 0;
    elemCent[i] = 0;
    elemCsokk[i] = 0;
    topMag(elem[i]);
    fn(i);
  }

}

function topMag(y) {
  x = y;
  do {
    elemTop[i] += x.offsetTop;
    x = x.offsetParent;
  }
  while (x !== document.body);
  x = y;

  elemBot[i] = elemTop[i] + elemMag[i];
  elemCent[i] = elemTop[i] + (elemMag[i] / 2);
  elemCsokk[i] = (elemBot[i] - winTop) / winMag;

}

function opaCsokk(i) {
  elem[i].style.opacity = elemCsokk[i];
}

var element = document.querySelectorAll("div")

window.onscroll = function() {
  getElem(element, element, opaCsokk)
}

The function works really well, the only problem is, that executing the function every time a scroll happens causes performance issues. So i decided that i rethink the function. What i need to get on every scroll are the properties of the window (winTop, winMag, winCent, winBot). The properties of the elements aren't changing. What i'm thinking about is that i can create a big array (replacement of the getElem function), storing the node lists and the properties of them. In this way the above example looks like this (getting the elemMag of the first node in the node list): bigArray[0]["elemMag"][0] with the function:

var bigArray = [];

function addingToBigArray(elem) {
  var element = elem;
  for (var i = 0; i < element.length; i++) {
    bigArray[bigArray.length] = {
      elemMag[i]: element.offsetHeight,
      elemTop[i]: element.offsetTop
    }
  }
}

But unfortunately an object's property can't be an array. How can i solve this problem, to get the same result of the above example with the function getElem?

2
  • 1
    "an object's property can't be an array" — that is totally false. Do you mean "an object's property name can't be an array"? Commented Mar 14, 2018 at 23:51
  • 1
    if you're up to date with javascript, try: { [elemMag[i]]: element.offsetHeight} Commented Mar 14, 2018 at 23:51

1 Answer 1

2
var bigArray = [];

function addingToBigArray(elem) {
  var element = elem;
  for (var i = 0; i < element.length; i++) {
    bigArray[bigArray.length] = {
      [elemMag[i]]: element.offsetHeight
    }
  }
}

See: []

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Here's a problem for me. The solution works, but inside the object, with a property, i can't use another properties values. A simplier example: var x = { first = [0, 1], second = x.first[0]+1 } It doesn't work. :/
that isnt a valid javascript object declaration
I can't access a property's value with another property in the same object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.