简体   繁体   English

设置classList.toggle的条件

[英]Setting conditions for classList.toggle

I have a function that's triggered by onClick . 我有一个由onClick触发的功能。 Here's the example . 这是例子 I want to only be able to trigger the function 'slide1' when 'slide2' is not triggered. 我只想在未触发“ slide2”时才触发函数“ slide1”。 I tried setting up a conditional statement like this: 我试图建立这样的条件语句:

function slide1() {
    btn1.classList.toggle('slide', btn2.className != 'slide');
}

I also tried an if statement like this: 我也尝试过这样的if语句:

function slide1() {
    if(btn2.className != 'slide') {
    btn1.classList.toggle('slide');
    }
}

That didn't work either. 那也不起作用。

I just need a simple way to toggle classes if certain conditions are met; 如果满足某些条件,我只需要一种简单的方法即可切换类; without jQuery or a library. 没有jQuery或库。 Thanks. 谢谢。

 var btn1 = document.getElementById('btn1'); var btn2 = document.getElementById('btn2'); function slide1() { btn1.classList.toggle('slide'); } function slide2() { btn2.classList.toggle('slide'); } 
 * { margin: 0; transition: 1s ease; -webkit-transition: 1s ease; } div { width: 50%; height: 100vh; display: block; position: absolute; text-align: center; cursor: pointer; } #btn1 { background: blue; } #btn2 { background: red; left: 50%; } #btn1.slide { width: 80%; z-index: 999; } #btn2.slide { width: 80%; z-index: 999; left: 20%; } 
 <div id="btn1" onClick="slide1();"> left </div> <div id="btn2" onClick="slide2();"> right </div> 

UPDATE: Here is an expanded example of the problem I'm dealing with. 更新: 是我正在处理的问题的扩展示例。 There are several elements with classes that need to be toggled only under certain circumstances. 类的几个元素仅在某些情况下才需要切换。 If 'panel1' is triggered when 'panel2' has already been triggered, then 'panel1' will cover 'panel2'. 如果在已经触发“ panel2”时触发了“ panel1”,则“ panel1”将覆盖“ panel2”。 and the same with 'panel3'. 与“ panel3”相同。

To answer your question, the proper way to check if an element has a class in JavaScript is element.classList.contains . 为了回答您的问题,检查元素在JavaScript中是否具有类的正确方法是element.classList.contains

So, in your example, you should replace the condition with 因此,在您的示例中,您应将条件替换为

if(btn2.className.contains('slide')) {
  ...
}

As a sidenote, having different functions doing the exact same thing on different elements should be avoided, where possible. 附带说明一下,在可能的情况下,应避免让不同的功能对不同的元素执行完全相同的操作。 Instead of having two functions, you should have only one and use the click event's target: 您不应只有两个功能,而应使用click事件的目标:

 let halves = document.querySelectorAll("div"); function slide(event) { // remove `slide` class from both divs: [].map.call(halves, function(half){ half.classList.remove('slide'); }); // add `slide` class to currently clicked div: event.target.classList.add('slide') } 
 * { margin: 0; transition: 1s ease; -webkit-transition: 1s ease; } div { width: 50%; height: 100vh; display: block; position: absolute; text-align: center; cursor: pointer; } #btn1 { background: blue; } #btn2 { background: red; left: 50%; } #btn1.slide { width: 80%; z-index: 999; } #btn2.slide { width: 80%; z-index: 999; left: 20%; } 
 <div id="btn1" onClick="slide(event);"> left </div> <div id="btn2" onClick="slide(event);"> right </div> 

On a different note, I assume you're aware the selectors used in both your question and my answer are outrageously generic and should never be used in production ready code. 另一方面,我假设您知道问题和答案中使用的选择器都是非常通用的,因此切勿在生产就绪代码中使用。


And as a last note, your CSS is quite faulty but I'm not considering fixing it here, as it wouldn't help anyone except yourself, which goes against the first principle of SO: one answer should help multiple users having the same problem. 最后要注意的是,您的CSS相当有缺陷,但是我没有考虑在此处进行修复,因为它除了您自己之外对其他人都无济于事,这与SO的第一个原则背道而驰:一个答案应该可以帮助多个有相同问题的用户。 Here's how I'd have coded your example: 这是我编码您的示例的方式:

 let br = document.querySelector('#blueRed'); br.addEventListener('click', function(event) { event.target.classList.toggle('slide'); [].map.call(br.querySelectorAll('div'), function(div) { if (div !== event.target) { div.classList.remove('slide'); } }); }) 
 body { margin: 0; } #blueRed { height: 100vh; display: flex; } #blueRed div { text-align: center; display: flex; align-items: center; justify-content: center; transition: flex-grow 1s cubic-bezier(0.4, 0, 0.2, 1); flex-grow: 1; background-color: blue; color: white; cursor: pointer; } #blueRed div:last-child { background-color: red; } #blueRed div.slide { flex-grow: 3; } 
 <div id="blueRed"> <div>left</div> <div>right</div> </div> 

Fiddle here . 在这里摆弄。 Should be prefixed . 应该加上前缀

I think I understand your objective... 我想我明白你的目标...

I condensed the functions into one and start off one button with the className = 'slide' . 我将这些功能压缩为一个,然后使用className = 'slide'开始一个按钮。 If one button is clicked then the class slide always alternates between the two buttons. 如果单击一个按钮,则课程幻灯片将始终在两个按钮之间交替。

Demo 演示版

 var btn1 = document.getElementById('btn1'); var btn2 = document.getElementById('btn2'); function slide() { btn1.classList.toggle('slide'); btn2.classList.toggle('slide'); } 
 * { margin: 0; transition: 1s ease; -webkit-transition: 1s ease; } div { width: 50%; height: 100vh; display: block; position: absolute; text-align: center; cursor: pointer; } #btn1 { background: blue; } #btn2 { background: red; left: 50%; } #btn1.slide { width: 80%; z-index: 999; } #btn2.slide { width: 80%; z-index: 999; left: 20%; } 
 <div id="btn1" onClick="slide();" class='slide'> left </div> <div id="btn2" onClick="slide();"> right </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM