当按顺序添加/删除类时,由JavaScript控制的CSS3单向转换不起作用(CSS3 one-way transition controlled by JavaScript doesn't work when add/remove class sequentially)
我想以单向方式进行CSS3过渡。 例如,我想通过设置它的
background-color: yellow;
来突出显示一些元素background-color: yellow;
立即,并将其设置回background-color: white;
单向过渡。我尝试使用JavaScript来实现这一点:
const highlightBtn = document.getElementById("highlightBtn"); const highlightToggleBtn = document.getElementById("highlightToggleBtn"); const highlightTimeoutBtn = document.getElementById("highlightTimeoutBtn"); const bodyClassList = document.getElementsByTagName("body")[0].classList; highlightBtn.addEventListener("click", () => { bodyClassList.add("highlight"); bodyClassList.remove("highlight"); /* This doesn't work, either. bodyClassList.toggle("highlight"); bodyClassList.toggle("highlight"); */ }); highlightToggleBtn.addEventListener("click", () => { bodyClassList.toggle("highlight"); }); highlightTimeoutBtn.addEventListener("click", () => { bodyClassList.add("highlight"); setTimeout(() => {bodyClassList.remove("highlight");}); /* This works, too. bodyClassList.toggle("highlight"); setTimeout(() => {bodyClassList.toggle("highlight");}); */ });
body { transition: background-color 1s ease; background-color: white; } body.highlight { transition: background-color 0s ease; background-color: yellow; }
<button id="highlightBtn"> Highlight </button> <button id="highlightToggleBtn"> Highlight Toggle </button> <button id="highlightTimeoutBtn"> Highlight Timeout </button>
问题是,如果我一次切换一次类,转换工作完美。
// This works fine. highlightToggleBtn.addEventListener("click", () => { bodyClassList.toggle("highlight"); });
但是,对于原始目标,我想突出显示该元素,所以我把add / remove添加到同一个元素只是想看单向转换,它失败了。
highlightBtn.addEventListener("click", () => { bodyClassList.add("highlight"); bodyClassList.remove("highlight"); /* This doesn't work, either. bodyClassList.toggle("highlight"); bodyClassList.toggle("highlight"); */ });
但是,如果我使用
setTimeout
,延迟为0 ms,结果是理想的。highlightTimeoutBtn.addEventListener("click", () => { bodyClassList.add("highlight"); setTimeout(() => {bodyClassList.remove("highlight");}); /* This works, too. bodyClassList.toggle("highlight"); setTimeout(() => {bodyClassList.toggle("highlight");}); */ });
为什么第二种方法不起作用? 将
removeClass
放入setTimeout
是最佳解决方案吗? 我也试过把延迟放在我身体的转换CSS中:transition: background-color 1s ease 1ms;
,但它也不起作用。I want to do a CSS3 transition in one-way. For example, I want to highlight some element by setting it's
background-color: yellow;
immediately, and set it back tobackground-color: white;
with one-way transition.I tried to use JavaScript to achieve this like this:
const highlightBtn = document.getElementById("highlightBtn"); const highlightToggleBtn = document.getElementById("highlightToggleBtn"); const highlightTimeoutBtn = document.getElementById("highlightTimeoutBtn"); const bodyClassList = document.getElementsByTagName("body")[0].classList; highlightBtn.addEventListener("click", () => { bodyClassList.add("highlight"); bodyClassList.remove("highlight"); /* This doesn't work, either. bodyClassList.toggle("highlight"); bodyClassList.toggle("highlight"); */ }); highlightToggleBtn.addEventListener("click", () => { bodyClassList.toggle("highlight"); }); highlightTimeoutBtn.addEventListener("click", () => { bodyClassList.add("highlight"); setTimeout(() => {bodyClassList.remove("highlight");}); /* This works, too. bodyClassList.toggle("highlight"); setTimeout(() => {bodyClassList.toggle("highlight");}); */ });
body { transition: background-color 1s ease; background-color: white; } body.highlight { transition: background-color 0s ease; background-color: yellow; }
<button id="highlightBtn"> Highlight </button> <button id="highlightToggleBtn"> Highlight Toggle </button> <button id="highlightTimeoutBtn"> Highlight Timeout </button>
The problem is, if I toggle the class once at a time, transition works perfectly.
// This works fine. highlightToggleBtn.addEventListener("click", () => { bodyClassList.toggle("highlight"); });
However, for the original goal, I want to highlight the element, so I put add/remove to the same element just want to see the one-way transition, it failed.
highlightBtn.addEventListener("click", () => { bodyClassList.add("highlight"); bodyClassList.remove("highlight"); /* This doesn't work, either. bodyClassList.toggle("highlight"); bodyClassList.toggle("highlight"); */ });
But, if I use
setTimeout
with delay is 0 ms, the result is ideal.highlightTimeoutBtn.addEventListener("click", () => { bodyClassList.add("highlight"); setTimeout(() => {bodyClassList.remove("highlight");}); /* This works, too. bodyClassList.toggle("highlight"); setTimeout(() => {bodyClassList.toggle("highlight");}); */ });
Why the second method doesn't work? Is putting the
removeClass
insetTimeout
the best solution? I also tried put a delay in my body's transition CSS like:transition: background-color 1s ease 1ms;
, but it doesn't work, either.
原文:https://stackoverflow.com/questions/36834526
最满意答案
您可以将数据存储在内部存储器中,每次点击服务器时都会删除旧内容。
You can store the data in your internal memory and every time you hit the server delete the old contents.
相关问答
更多-
您可以将数据存储在内部存储器中,每次点击服务器时都会删除旧内容。 You can store the data in your internal memory and every time you hit the server delete the old contents.
-
在android中读取XML文件(Read XML file in android)[2022-04-01]
将xml文件放在assets文件夹中并使用AssetManager获取它: http : //developer.android.com/reference/android/content/res/AssetManager.html 要读取文件,请使用与J2SE相同的java代码。 Put the xml file in the assets folder and fetch it using the AssetManager: http://developer.android.com/reference/ ... -
您可以使用assets文件夹并将其放在那里。 要访问File ,就像这样使用它 InputStream is = getResources().getAssets().open("yourFile.xml"); You can use the assets folder and put it there. To access the File use it like this InputStream is = getResources().getAssets().open("yourFile.xml" ...
-
您的Web服务应该向您发送一些有关文件类型的标识符。 字节数组是用于图像还是用于一般文件。 那么只有你可以知道它是哪种类型的文件。 知道文件类型后,您可以将字节数组转换为所需的文件类型。 您也可以写入文件。 如果要在logcat中打印该xml,可以使用 String xmlData = new String(byte[] data); System.out.println(xmlData) 如果您知道文件格式,请创建一个文件(无论是xml还是图像或任何内容) String extension = ".xm ...
-
是的你可以。 这就是为每个应用程序指定唯一包名限定符的原因。 您可以通过提供其全名限定符来使用任何库资源。 yes you can. This is why you specify a unique package name qualifier for every application. You can use any library resource by providing it's full name qualifier.
-
如何在android中定向更改不同的xml文件(how to load different xml file in orientation change in android)[2022-08-18]
创建两种布局目录来处理方向。 layout-land layout-port将这个xml在这个目录中同名。 如果我有main.xml文件,那么我必须把它放在两个目录中。 谢谢 create two types of layout directories to handle orientation . layout-land layout-port put the xml with the same name in both the directory. if i have main.xml file the ... -
您的APK是只读的。 你不能修改任何东西,包括。 assets/文件夹。 如果您需要将其内容作为可修改的模板,则必须首先将其从assets/复制出来。 Your APK is read only. You cannot modify anything, incl. assets/ folder. If you need its content to be sort of modifiable template, you must copy it out of assets/ first.
-
XML文件中的Android变量(Android Variable in XML file)[2022-02-25]
您将需要通过它的id搜索seekbar创建SeekBar的实例 Seekbar seekbar = (SeekBar) findViewById(R.id.seekbar); 然后,您将设置要设置的值 seekbar.max = yourNewValue; You will want to create an instance of the SeekBar by it's id seekbar Seekbar seekbar = (SeekBar) findViewById(R.id.seekbar); A ... -
您应该将xml文件放在res/raw文件夹中,然后您可以使用getResources().getRawResource(R.raw.resource_id)来获取一个InputStream ,您可以使用所有标准DOM方法处理它(将其包装在InputSource等)。 You should place the xml file in the res/raw folder, and then you can use getResources().getRawResource(R.raw.resource_id) ...
-
在android中读取xml文件(Reading an xml file in android)[2023-05-22]
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class Handler extends DefaultHandler { public String ipAddress; public String port; public StringBuffer sbBuffer; @Overrid ...