简体   繁体   English

如何重复 javascript function 以及如何反转一个

[英]How to repeat a javascript function and how to reverse one

I'm a little bit ashamed to admit, that i'm not that good with javascript.我有点羞于承认,我对 javascript 不是那么好。 I need some help.我需要一些帮助。 There's a modal that opens with the help of Javascript, however i'm going to give you some code that is simplified.在 Javascript 的帮助下打开了一个模态,但是我会给你一些简化的代码。

imagine that there's a button somewhere in this universe That's the modal in html and css想象在这个宇宙的某个地方有一个按钮 那是 html 和 css 中的模态

< div class="modal" > < /div >
.modal {width: 0vw;}
.modal-open {width: 50vw;}

Javascript - document.getElementById('that-button').addEventListener('click', function () {
  document.querySelector('.modal').classList.add('modal-open');
});

And there's another button to close it.还有另一个按钮可以关闭它。 Again - imagination is needed再次 - 需要想象力

Javascript - document.getElementById('that-other-button').addEventListener('click', function () {
  document.querySelector('.modal').classList.add('modal-close');
});

.modal-close {width: 0vw;}

Long story short, the modal stays hidden with width:0vw and by pressing the open button, it opens with more width.长话短说,模态保持隐藏的宽度:0vw,按下打开按钮,它以更大的宽度打开。 By pressing the close button, the modal closes.通过按下关闭按钮,模式关闭。

However after that the modal cannot be opened again.但是,此后无法再次打开模态。 I would like to know how.我想知道怎么做。

Second issue: imagine the same scenario however without the closing button.第二个问题:想象同样的场景,但是没有关闭按钮。 Is it possible, to open and close the modal with the open modal button by integrating some reverse function?是否可以通过集成一些反向 function 来使用打开模式按钮打开和关闭模式?

I'd like to thank in advance.我要提前感谢。

With very best regards,致以最诚挚的问候,

Anthony Ivanov安东尼·伊万诺夫

When you open it, it gains the the class modal-open .当您打开它时,它会获得 class modal-open

When you close it, it gains the class modal-close so now it has both classes.当你关闭它时,它会获得 class modal-close所以现在它有两个类。

Clicking the open button again does nothing because it already has that class.再次单击打开按钮没有任何作用,因为它已经有了 class。


Don't use two classes to track the state of something that can be in two states.不要使用两个类来跟踪可能处于两种状态的事物的 state。

Use one class.使用一个 class。 add it to open and remove it to close it. add它以打开并remove它以关闭它。


Is it possible, to open and close the modal with the open modal button by integrating some reverse function?是否可以通过集成一些反向 function 来使用打开模式按钮打开和关闭模式?

In the general case, no.一般情况下,没有。 There's no way to say "Do what this arbitrary function does but backwards".没有办法说“做这个任意的 function 做的事情,但是倒退”。 What a function does might not even be reversible. function 所做的甚至可能不可逆。

In this case, you can toggle the class instead of adding and removing it.这种情况下,您可以toggle class 而不是添加和删除它。

It sounds like the existence of the modal-close class overrides rules set by the modal-open class, so if it gets closed, it'll stay closed.听起来modal-close class 的存在覆盖了modal-open class 设置的规则,所以如果它关闭,它将保持关闭状态。

Rather than having two separate classes, consider using just a single one instead: have it closed by default, and then have .modal-open change the width.与其拥有两个单独的类,不如考虑只使用一个:默认关闭,然后让.modal-open更改宽度。 When you want to close it, don't add another class - instead, remove the existing class:当你想关闭它时,不要添加另一个 class - 相反,删除现有的 class:

document.getElementById('that-other-button').addEventListener('click', function () {
  document.querySelector('.modal').classList.remove('modal-open');
});

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

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