admin管理员组文章数量:1394520
Is there any native JavaScript function that allow toggling style (not class) on an element?
So far, I'm using this kind of script:
target.addEventListener('click', (e) => {
target.classList.toggle('target--is-visible')
}
Following by the style:
target {
visibility: hidden;
&--is-visible {
visibility: visible;
}
}
And I'd love being allowed to do so:
target.addEventListener('click', (e) => {
target.style.toggle.visibility = 'visible'
}
EDIT
Ternary Operator is the closest of what I'm looking for in term of readability.
And the function sent by the @GuyWhoKnowsStuff uses ternary operator and deserves to be shared :
const div = document.querySelector('div');
function toggleStyle(el, prop, style1, style2) {
el.style[prop] = el.style[prop] === style1 ? style2 : style1;
}
div.addEventListener('click', e => {
toggleStyle(div, 'background', 'red', 'blue');
});
Thanks for your answers!
Is there any native JavaScript function that allow toggling style (not class) on an element?
So far, I'm using this kind of script:
target.addEventListener('click', (e) => {
target.classList.toggle('target--is-visible')
}
Following by the style:
target {
visibility: hidden;
&--is-visible {
visibility: visible;
}
}
And I'd love being allowed to do so:
target.addEventListener('click', (e) => {
target.style.toggle.visibility = 'visible'
}
EDIT
Ternary Operator is the closest of what I'm looking for in term of readability.
And the function sent by the @GuyWhoKnowsStuff uses ternary operator and deserves to be shared :
const div = document.querySelector('div');
function toggleStyle(el, prop, style1, style2) {
el.style[prop] = el.style[prop] === style1 ? style2 : style1;
}
div.addEventListener('click', e => {
toggleStyle(div, 'background', 'red', 'blue');
});
Thanks for your answers!
Share Improve this question edited Oct 11, 2017 at 8:35 Alexis Wollseifen asked Oct 11, 2017 at 8:12 Alexis WollseifenAlexis Wollseifen 4611 gold badge7 silver badges21 bronze badges 3-
1
You can try something like
target.style.visibility = target.style.visibility == 'visible' ? 'hidden' : 'visible'
However toggling a css class is cleaner approach – Satpal Commented Oct 11, 2017 at 8:14 - @Satpal - toggle isn't a style property – Jaromanda X Commented Oct 11, 2017 at 8:14
- Thanks four your ment @Satpal ! It's the kink of easy way I'm looking for. Why isn't it a cleaner approach? In term of readability or performance? – Alexis Wollseifen Commented Oct 11, 2017 at 8:19
3 Answers
Reset to default 7Try this:
const div = document.querySelector('div');
function toggleStyle(el, prop, style1, style2) {
el.style[prop] = el.style[prop] === style1 ? style2 : style1;
}
div.addEventListener('click', e => {
toggleStyle(div, 'background', 'red', 'blue');
});
div {
padding: 40px;
background: red;
}
<div>Click Me</div>
or in your case, I do not believe click event fires if the div
visibility is hidden,
see CSS: Is a hidden object clickable?,
so instead toggle opacity like so:
const div = document.querySelector('div');
function toggleStyle(el, prop, style1, style2) {
el.style[prop] = el.style[prop] === style1 ? style2 : style1;
}
div.addEventListener('mousedown', e => {
toggleStyle(div, 'opacity', '0', '1');
});
div {
padding: 40px;
background: red;
opacity: 1;
}
<div>Click Me</div>
You can also add a prototype like this:
HTMLElement.prototype.toggleVisibility = function()
{
this.style.visibility = this.style.visibility == 'visible' ? 'hidden' : 'visible'
}
and then use like below:
target.toggleVisibility();
It will make it visible and hidden on each invocation.
You could do with ternary operator
for toggle
target.addEventListener('click', (e) => {
target.style.visibility = target.style.visibility == 'visible' ? 'hidden' : 'visible'
}
本文标签: javascriptToggle style in vanillaStack Overflow
版权声明:本文标题:javascript - Toggle style in vanilla - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744101488a2590890.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论