0

I am working on Toggle between adding and removing class name from an element in Javascript.

Below is the code for that:

const yaxis = xyaxis.querySelector('.y-axis');   

yaxis.classList.toggle('y-axis-scroll-bar', .35 <= yaxis.firstElementChild.offsetHeight / window.innerHeight);


Problem Statement:

In the above code yaxis.classList.toggle('y-axis-scroll-bar') seems to add y-axis-scroll-bar on toggle.

I am wondering what this part .35 <= yaxis.firstElementChild.offsetHeight / window.innerHeight of the code is doing above.

1 Answer 1

1

When in doubt, check the docs. The second argument to classList.toggle instructs the intepreter whether to add the class specified in the first argument, or whether to remove it:

toggle( String [, force] )

When only one argument is present: Toggle the class value; i.e., if the class exists then remove it and return false, if not, then add it and return true.

When a second argument is present: If the second argument evaluates to true, add the specified class value, and if it evaluates to false, remove it.

So, in your code, when

.35 <= yaxis.firstElementChild.offsetHeight / window.innerHeight

evaluates to true, the class y-axis-scroll-bar gets added to the element, if the class doesn't exist on it already - otherwise, if it evaluates to false, the class is removed if it exists.

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

3 Comments

Thanks for letting me know. It definitely make sense. I have accepted your answer as well. I am wondering what this line .35 <= yaxis.firstElementChild.offsetHeight / window.innerHeight means here ?
I was having flickering issue on zoom-in/zoom-out or sometimes in the normal mode as well. It was very hard to reproduce so that's why I am not using fiddle in the question. On removing the above line from my code I was able to solve the flickr issue in the chrome.
Not sure how removing this line solved from flcikering issue in the chrome .35 <= yaxis.firstElementChild.offsetHeight / window.innerHeight

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.