我有一个由“ tile1”类触发的onclick事件。在该函数中,我使用“ classList.remove(“ tile1”)“,因此,如果我单击具有类” tile1“的元素,怎么会再次触发它?
这里有很多CSS信息,但我不认为它们与引起问题的原因有关,但我已经将其包括在内,以便您可以轻松地自己复制和粘贴代码。
非常感谢您的帮助。
var galleryContainer = document.getElementsByClassName("galleryContainer");
var category = document.getElementsByClassName("category");
var subCat = document.getElementsByClassName("subCat");
var tile = document.getElementsByClassName("tile");
var tile1 = document.getElementsByClassName("tile1");
var tileImg = document.getElementsByClassName("tileImg");
var galleryWindow = document.getElementsByClassName("galleryWindow");
var albumEntry = document.getElementsByClassName("albumEntry");
var albumEntryImg = document.getElementsByClassName("albumEntryImg");
// POSSIBLE ISSUE (next two lines)
for(var index = 0; index < tile1.length; index++) {
tile1[index].onclick = function() {
//SUBCAT1a
setTimeout(function(){
for (var a = 0; a < subCat.length; a++) {
subCat[a].classList.remove("subCat1");
subCat[a].classList.remove("subCat2");
subCat[a].classList.remove("subCat3");
subCat[a].classList.add("subCat1a");
subCat[a].style.transition = "linear 200ms";
}
}, 0);
//SUBCAT
setTimeout(function(){
for (var b = 0; b < subCat.length; b++) {
subCat[b].classList.remove("subCat1");
subCat[b].classList.remove("subCat1a");
subCat[b].classList.remove("subCat3");
subCat[b].classList.add("subCat2");
subCat[b].style.transition = "linear 1000ms";
}
}, 1500);
//GALLERY CONTAINER
setTimeout(function(){
for (var c = 0; c < galleryContainer.length; c++) {
galleryContainer[c].classList.remove("galleryContainer1");
galleryContainer[c].classList.remove("galleryContainer3");
galleryContainer[c].classList.add("galleryContainer2");
galleryContainer[c].style.transition = "linear 500ms";
}
}, 1000);
//CATEGORY
setTimeout(function(){
for (var d = 0; d < category.length; d++) {
category[d].classList.remove("category1");
category[d].classList.remove("category2");
category[d].classList.remove("category3");
category[d].classList.add("category1a");
category[d].style.transition = "linear 500ms";
}
}, 500);
//CATEGORY
setTimeout(function(){
for (var e = 0; e < category.length; e++) {
category[e].classList.remove("category1");
category[e].classList.remove("category1a");
category[e].classList.remove("category3");
category[e].classList.add("category2");
//category[e].style.transition = "linear 0s";
}
}, 500);
// POSSIBLE ISSUE (next nine lines)
//TILE
setTimeout(function(){
for (var f = 0; f < tile.length; f++) {
tile[f].classList.remove("tile1");
tile[f].classList.remove("tile3");
tile[f].classList.add("tile2");
//tile[e].style.transition = "linear 0s";
}
}, 500);
//TILEIMG
setTimeout(function(){
for (var g = 0; g < tileImg.length; g++) {
tileImg[g].classList.remove("tileImg1");
tileImg[g].classList.remove("tileImg3");
tileImg[g].classList.add("tileImg2");
tileImg[g].style.transition = "linear 500ms";
}
}, 500);
//GALLERY WINDOW
setTimeout(function(){
for (var h = 0; h < galleryWindow.length; h++) {
galleryWindow[h].classList.remove("galleryWindow1");
galleryWindow[h].classList.remove("galleryWindow3");
galleryWindow[h].classList.add("galleryWindow2");
//galleryWindow[h].style.transition = "opacity linear 5000ms";
}
}, 1800);
//ALBUM ENTRY CONTAINER
setTimeout(function(){
for(var i = 0; albumEntry.length; i++) {
albumEntry[i].classList.remove("albumEntry1");
albumEntry[i].classList.remove("albumEntry3");
albumEntry[i].classList.add("albumEntry2");
}
}, 1800);
//ALBUM ENTRY IMG
setTimeout(function(){
for(var j = 0; albumEntryImg.length; j++) {
albumEntryImg[j].classList.remove("albumEntryImg1");
albumEntryImg[j].classList.remove("albumEntryImg3");
albumEntryImg[j].classList.add("albumEntryImg2");
}
}, 1800);
}
}
* {
box-sizing: border-box;
}
body {
margin: 0px;
padding: 0px;
}
/* ////////// STEP 1 ////////// */
.galleryContainer1 {
display: flex;
width: 100vw;
flex-direction: column;
justify-content: center;
padding-top: 1vw;
}
.category1 {
border: 0.4vw solid #00f;
position: relative;
margin: 1vw;
}
.category1 h3 {
position: absolute;
top: -2.6vw;
left: 1vw;
font-size: 1.5vw;
background-color: #fff;
padding: 0px 1vw;
}
.subCat1 {
display: flex;
margin: 0.5vw;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
}
.tile1 {
border: 1px solid #000;
position: relative;
display: flex;
margin: 1vw;
cursor: pointer;
}
.tile1:hover {
opacity: 0.5;
}
.tile1 h4 {
position: absolute;
top: 0.1vw;
user-select: none;
cursor: inherit;
font-size: 2vw;
font-weight: bolder;
color: #fff;
text-shadow:
0px 0px 5px #000;
}
.tileImg1 {
position: relative;
top: 0px;
border: 1px solid #000;
display: flex;
flex: 1;
width: 6vw;
}
#natureText {left: 3vw;}
.galleryWindow1 {
display: flex;
width: 0px;
opacity: 0;
}
.galleryWindow1 h1 {
display: none;
color: #f0f;
}
.albumEntry1 {
display: none;
}
.albumEntryImg1 {
display: none;
}
/* ///// STEP 1a (transitional step) ///// */
.category1a:first-of-type {
border: 0.4vw solid #00f;
position: relative;
margin: 1vw;
height: 15vw;
}
.category1a {
border: 0.4vw solid #00f;
position: relative;
margin: 1vw;
height: 28vw;
}
.subCat1a {
display: flex;
margin: 0.5vw;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
opacity: 0;
}
/* ////////// STEP 2 ////////// */
.galleryContainer2 {
display: flex;
width: 20vw;
flex-direction: column;
justify-content: center;
padding-top: 1vw;
}
.category2:first-of-type {
/*min-height: 190px;*/
height: 14vw;
}
.category2 {
border: 0.4vw solid #00f;
position: relative;
margin: 1vw;
/*min-height: 360px;*/
height: 27vw;
}
.category2 h3 {
position: absolute;
top: -2.6vw;
left: 1vw;
font-size: 1.5vw;
background-color: #fff;
padding: 0px 1vw;
}
.subCat2 {
display: flex;
margin: 0.5vw;
justify-content: flex-start;
flex-direction: column;
}
.tile2 {
position: relative;
display: flex;
margin: 0vw;
user-select: none;
}
.tile2 h4 {
display: flex;
margin: 0.2vw;
font-size: 1.3vw;
color: #888;
cursor: pointer;
}
.tile2 h4:hover {
color: #000;
}
.tileImg2 {
position: relative;
top: 0.8vw;
/*top: 11px;*/
border: 1px solid transparent;
display: flex;
flex: 1;
width: 0vw;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test 5 - transitions</title>
<link href="css/T5.css" rel="stylesheet" type="text/css">
</head>
<body>
<section class="galleryContainer galleryContainer1">
<div class="category category1">
<h3>TYPE:</h3>
<div class="subCat subCat1">
<div class="tile tile1">
<div>
<img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
<img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
</div>
<div>
<img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
<img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
</div>
<h4 class="tileText tileText1" id="natureText">Nature</h4>
</div>
</div>
</div>
</section>
<section class="galleryWindow galleryWindow1">
<h1>TEST:</h1>
</section>
<script src="js/T5.js"></script>
</body>
</html>
答案 0 :(得分:1)
注册事件侦听器时,您正在注册元素。例如,只要该元素附加了click事件监听器,则每次单击该元素时都会触发事件监听器的回调。该元素可以添加和/或删除类,并且事件监听器不会受到影响。请记住,您仅使用该类来查找要添加事件侦听器的元素。找到它后,删除用于查找它的类是不相关的。
要删除元素侦听器,请使用removeEventListener
并将相同的引用传递给用于注册事件侦听器的回调。
自从您使用过onclick
,就可以将函数引用设置为null
。
...
tile[f].onclick = null;
...
var galleryContainer = document.getElementsByClassName("galleryContainer");
var category = document.getElementsByClassName("category");
var subCat = document.getElementsByClassName("subCat");
var tile = document.getElementsByClassName("tile");
var tile1 = document.getElementsByClassName("tile1");
var tileImg = document.getElementsByClassName("tileImg");
var galleryWindow = document.getElementsByClassName("galleryWindow");
var albumEntry = document.getElementsByClassName("albumEntry");
var albumEntryImg = document.getElementsByClassName("albumEntryImg");
// POSSIBLE ISSUE (next two lines)
for(var index = 0; index < tile1.length; index++) {
tile1[index].onclick = function() {
//SUBCAT1a
setTimeout(function(){
for (var a = 0; a < subCat.length; a++) {
subCat[a].classList.remove("subCat1");
subCat[a].classList.remove("subCat2");
subCat[a].classList.remove("subCat3");
subCat[a].classList.add("subCat1a");
subCat[a].style.transition = "linear 200ms";
}
}, 0);
//SUBCAT
setTimeout(function(){
for (var b = 0; b < subCat.length; b++) {
subCat[b].classList.remove("subCat1");
subCat[b].classList.remove("subCat1a");
subCat[b].classList.remove("subCat3");
subCat[b].classList.add("subCat2");
subCat[b].style.transition = "linear 1000ms";
}
}, 1500);
//GALLERY CONTAINER
setTimeout(function(){
for (var c = 0; c < galleryContainer.length; c++) {
galleryContainer[c].classList.remove("galleryContainer1");
galleryContainer[c].classList.remove("galleryContainer3");
galleryContainer[c].classList.add("galleryContainer2");
galleryContainer[c].style.transition = "linear 500ms";
}
}, 1000);
//CATEGORY
setTimeout(function(){
for (var d = 0; d < category.length; d++) {
category[d].classList.remove("category1");
category[d].classList.remove("category2");
category[d].classList.remove("category3");
category[d].classList.add("category1a");
category[d].style.transition = "linear 500ms";
}
}, 500);
//CATEGORY
setTimeout(function(){
for (var e = 0; e < category.length; e++) {
category[e].classList.remove("category1");
category[e].classList.remove("category1a");
category[e].classList.remove("category3");
category[e].classList.add("category2");
//category[e].style.transition = "linear 0s";
}
}, 500);
// POSSIBLE ISSUE (next nine lines)
//TILE
setTimeout(function(){
for (var f = 0; f < tile.length; f++) {
tile[f].classList.remove("tile1");
tile[f].onclick = null;
tile[f].classList.remove("tile3");
tile[f].classList.add("tile2");
//tile[e].style.transition = "linear 0s";
}
}, 500);
//TILEIMG
setTimeout(function(){
for (var g = 0; g < tileImg.length; g++) {
tileImg[g].classList.remove("tileImg1");
tileImg[g].classList.remove("tileImg3");
tileImg[g].classList.add("tileImg2");
tileImg[g].style.transition = "linear 500ms";
}
}, 500);
//GALLERY WINDOW
setTimeout(function(){
for (var h = 0; h < galleryWindow.length; h++) {
galleryWindow[h].classList.remove("galleryWindow1");
galleryWindow[h].classList.remove("galleryWindow3");
galleryWindow[h].classList.add("galleryWindow2");
//galleryWindow[h].style.transition = "opacity linear 5000ms";
}
}, 1800);
//ALBUM ENTRY CONTAINER
setTimeout(function(){
for(var i = 0; albumEntry.length; i++) {
albumEntry[i].classList.remove("albumEntry1");
albumEntry[i].classList.remove("albumEntry3");
albumEntry[i].classList.add("albumEntry2");
}
}, 1800);
//ALBUM ENTRY IMG
setTimeout(function(){
for(var j = 0; albumEntryImg.length; j++) {
albumEntryImg[j].classList.remove("albumEntryImg1");
albumEntryImg[j].classList.remove("albumEntryImg3");
albumEntryImg[j].classList.add("albumEntryImg2");
}
}, 1800);
}
}
* {
box-sizing: border-box;
}
body {
margin: 0px;
padding: 0px;
}
/* ////////// STEP 1 ////////// */
.galleryContainer1 {
display: flex;
width: 100vw;
flex-direction: column;
justify-content: center;
padding-top: 1vw;
}
.category1 {
border: 0.4vw solid #00f;
position: relative;
margin: 1vw;
}
.category1 h3 {
position: absolute;
top: -2.6vw;
left: 1vw;
font-size: 1.5vw;
background-color: #fff;
padding: 0px 1vw;
}
.subCat1 {
display: flex;
margin: 0.5vw;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
}
.tile1 {
border: 1px solid #000;
position: relative;
display: flex;
margin: 1vw;
cursor: pointer;
}
.tile1:hover {
opacity: 0.5;
}
.tile1 h4 {
position: absolute;
top: 0.1vw;
user-select: none;
cursor: inherit;
font-size: 2vw;
font-weight: bolder;
color: #fff;
text-shadow:
0px 0px 5px #000;
}
.tileImg1 {
position: relative;
top: 0px;
border: 1px solid #000;
display: flex;
flex: 1;
width: 6vw;
}
#natureText {left: 3vw;}
.galleryWindow1 {
display: flex;
width: 0px;
opacity: 0;
}
.galleryWindow1 h1 {
display: none;
color: #f0f;
}
.albumEntry1 {
display: none;
}
.albumEntryImg1 {
display: none;
}
/* ///// STEP 1a (transitional step) ///// */
.category1a:first-of-type {
border: 0.4vw solid #00f;
position: relative;
margin: 1vw;
height: 15vw;
}
.category1a {
border: 0.4vw solid #00f;
position: relative;
margin: 1vw;
height: 28vw;
}
.subCat1a {
display: flex;
margin: 0.5vw;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
opacity: 0;
}
/* ////////// STEP 2 ////////// */
.galleryContainer2 {
display: flex;
width: 20vw;
flex-direction: column;
justify-content: center;
padding-top: 1vw;
}
.category2:first-of-type {
/*min-height: 190px;*/
height: 14vw;
}
.category2 {
border: 0.4vw solid #00f;
position: relative;
margin: 1vw;
/*min-height: 360px;*/
height: 27vw;
}
.category2 h3 {
position: absolute;
top: -2.6vw;
left: 1vw;
font-size: 1.5vw;
background-color: #fff;
padding: 0px 1vw;
}
.subCat2 {
display: flex;
margin: 0.5vw;
justify-content: flex-start;
flex-direction: column;
}
.tile2 {
position: relative;
display: flex;
margin: 0vw;
user-select: none;
}
.tile2 h4 {
display: flex;
margin: 0.2vw;
font-size: 1.3vw;
color: #888;
cursor: pointer;
}
.tile2 h4:hover {
color: #000;
}
.tileImg2 {
position: relative;
top: 0.8vw;
/*top: 11px;*/
border: 1px solid transparent;
display: flex;
flex: 1;
width: 0vw;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test 5 - transitions</title>
<link href="css/T5.css" rel="stylesheet" type="text/css">
</head>
<body>
<section class="galleryContainer galleryContainer1">
<div class="category category1">
<h3>TYPE:</h3>
<div class="subCat subCat1">
<div class="tile tile1">
<div>
<img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
<img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
</div>
<div>
<img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
<img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
</div>
<h4 class="tileText tileText1" id="natureText">Nature</h4>
</div>
</div>
</div>
</section>
<section class="galleryWindow galleryWindow1">
<h1>TEST:</h1>
</section>
<script src="js/T5.js"></script>
</body>
</html>