发布于2024-11-03 21:58 阅读(27) 评论(0) 点赞(24) 收藏(1)
我有一个由类“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>
注册事件侦听器时,您实际上是在注册一个元素。只要该元素附加了单击事件侦听器,每次单击该元素时都会触发事件侦听器的回调。可以添加和/或删除该元素的类,事件侦听器不会受到影响。请记住,您只是使用该类来查找要附加事件侦听器的元素。找到它后,删除用于查找它的类就无关紧要了。
要删除元素侦听器,请使用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>
作者:黑洞官方问答小能手
链接:http://www.qianduanheidong.com/blog/article/534986/a84a37757a0680ba5f5c/
来源:前端黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 前端黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-3
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!