[英]How do I prevent a javascript function from running when a DOM element from an array is already selected?
I have a script that makes gives me a dynamic header depending on which bubble in an array is currently selected. 我有一个脚本,该脚本根据当前选择数组中的哪个气泡为我提供了动态标头。 However, when a bubble is already selected and the user clicks on that same bubble, I can't stop the js function from running and it changes the opacity of the header to 0 temporarily.
但是,当已经选择一个气泡并且用户单击相同的气泡时,我无法停止js函数的运行,并且它会暂时将标头的不透明度更改为0。 In this case I'd like for the opacity of the header to remain as 1. This seems rather simple but I can't figure it out.
在这种情况下,我希望标题的不透明度保持为1。这似乎很简单,但我无法弄清楚。 The code on CodePen.
CodePen上的代码。
HTML: HTML:
<div id="bubbles">
<div onclick="bubbles(0); clearInterval(intrvl);" style="background:#da2225;"></div>
<div onclick="bubbles(1); clearInterval(intrvl);"></div>
<div onclick="bubbles(2); clearInterval(intrvl);"></div>
</div>
<div id="bubblecontent">
<h2>Header 1</h2>
</div>
Javascript: Javascript:
function _(x){return document.getElementById(x);}
var ba, bi=0, intrvl;
var bca = [
'<h2>Header 1</h2>',
'<h2>Header 2</h2>',
'<h2>Header 3</h2>',
];
function bubbles(bi){
_("bubblecontent").style.opacity = 0;
for(var i=0; i < ba.length; i++){
ba[i].style.background = "#ccc";
}
ba[bi].style.background = "#da2225";
setTimeout(function(){
_("bubblecontent").innerHTML = bca[bi];
_("bubblecontent").style.opacity = 1;
}, 300);
}
window.addEventListener("load", function(){
ba = _("bubbles").children;
});
In function bubbles, add conditional if the bi is the same as previous one: 在功能气泡中,如果bi与上一个相同,则添加条件:
var prev = -1;
function bubbles(bi){
if (bi != prev){
// put the rest of your bubbles(bi) code put here
}
prev = bi;
}
You can use HTMLElement.dataset
to store variable of active element, set first <div>
child of #bubbles
element to "true"
, at click
event of div
element set all #bubbles
div
children ba
dataset.active
to "false"
, set current element ba[bi]
.dataset.active
to "true"
您可以使用
HTMLElement.dataset
存储有源元件的变量,第一组<div>
的孩子#bubbles
元素"true"
,在click
的事件div
设置的所有元素#bubbles
div
儿ba
dataset.active
到"false"
,设置当前元素ba[bi]
.dataset.active
为"true"
function _(x) { return document.getElementById(x); } var ba, bi = 0, intrvl; var bca = [ '<h2>Header 1</h2>', '<h2>Header 2</h2>', '<h2>Header 3</h2>', ]; function bubbles(bi) { if (ba[bi].dataset.active !== "true") { _("bubblecontent").style.opacity = 0; for (var i = 0; i < ba.length; i++) { ba[i].style.background = "#ccc"; ba[i].dataset.active = "false"; } ba[bi].dataset.active = true; ba[bi].style.background = "#da2225"; setTimeout(function() { _("bubblecontent").innerHTML = bca[bi]; _("bubblecontent").style.opacity = 1; }, 300); } } window.addEventListener("load", function() { ba = _("bubbles").children; });
#bubbles { text-align: center; } #bubbles > div { display: inline-block; width: 15px; height: 15px; margin: 24px 10px 0px 10px; background-color: #ccc; border-radius: 100%; transition: background 0.3s linear 0s; cursor: pointer; } #bubblecontent { transition: opacity 0.3s linear 0s; } #bubblecontent h2 { text-align: center; color: #333; }
<div id="bubbles"> <div data-active="true" onclick="bubbles(0); clearInterval(intrvl);" style="background:#da2225;"></div> <div onclick="bubbles(1); clearInterval(intrvl);"></div> <div onclick="bubbles(2); clearInterval(intrvl);"></div> </div> <div id="bubblecontent"> <h2>Header 1</h2> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.