[英]Toggle between adding/removing class name from an element in javascript
I am working on Toggle between adding and removing class name from an element in Javascript. 我正在在Java语言的元素中添加和删除类名之间进行切换。
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. 在上面的代码中,
yaxis.classList.toggle('y-axis-scroll-bar')
似乎在切换时添加了y-axis-scroll-bar。
I am wondering what this part .35 <= yaxis.firstElementChild.offsetHeight / window.innerHeight
of the code is doing above. 我想知道这部分
.35 <= yaxis.firstElementChild.offsetHeight / window.innerHeight
上面的代码在做什么。
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: classList.toggle
的第二个参数指示解释器是添加第一个参数中指定的类还是删除它:
toggle( String [, force] )
When only one argument is present: Toggle the class value;
仅存在一个参数时:切换类的值;否则,返回0。 ie, if the class exists then remove it and return
false
, if not, then add it and returntrue
.即,如果该类存在,则将其删除并返回
false
;如果不存在,则将其添加并返回true
。When a second argument is present: If the second argument evaluates to
true
, add the specified class value, and if it evaluates tofalse
, remove it.当存在第二个参数时:如果第二个参数的值为
true
,则添加指定的类值;如果第二个参数的值为false
,则将其删除。
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. 如果该类的值不为
true
,则将y-axis-scroll-bar
添加到元素上;否则,如果该类的值为false
,则将该类删除(如果存在)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.