简体   繁体   English

当元素包含某个 class 时尝试使用 JS 旋转图像

[英]Trying to rotate an image with JS when an element contains a certain class

Basically im trying to make it so that when i click on a question the arrow i have next to the question rotates 180 degrees.基本上我试图做到这一点,以便当我点击一个问题时,我在问题旁边的箭头会旋转 180 度。 Right now my code doesnt rotate the arrows at all.现在我的代码根本不旋转箭头。 Any help would be appreciated.任何帮助,将不胜感激。

Heres my javascript code这是我的 javascript 代码

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function () {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.display === "block") {
      panel.style.display = "none";
    } else {
      panel.style.display = "block";
    }
  });
}

var img = document.getElementsByClassName("arrow");

if (button.classList.contains(active)) {
  img.style.transform = "rotate(180deg)";
}
    

Current result当前结果

One big reason this isn't working is that this line:这不起作用的一个重要原因是这条线:

if (button.classList.contains(active)) { img.style.transform = "rotate(180deg)"; if (button.classList.contains(active)) { img.style.transform = "rotate(180deg)"; } }

isn't inside of some function that triggers the arrow to rotate.不在某些触发箭头旋转的 function 内部。 It is evaluated when the page loads and then not used again.它在页面加载时进行评估,然后不再使用。 However, there are a few ways to improve your code.但是,有几种方法可以改进您的代码。

Using classes to affect your style changes allows you better readability, flexibility and reusability.使用类来影响您的样式更改可以让您获得更好的可读性、灵活性和可重用性。 I also opted for querySelectorAll instead of getElementsByClassname and used a closest / querySelector combo to find adjacent elements rather than nextElementSibling我还选择了querySelectorAll而不是getElementsByClassname并使用了closest / querySelector组合来查找相邻元素而不是 nextElementSibling

 document.querySelectorAll(".accordian").forEach(acc => { acc.addEventListener("click", e => { e.target.classList.toggle("active"); e.target.closest('.container').querySelector('.arrow').classList.toggle('active') e.target.closest('.container').querySelector('.content').classList.toggle('show') }); });
 .arrow { transition: all.5s; display: inline-block; }.arrow.active { transform: rotate(90deg); }.content { display: none; }.show { display: block; }
 <div class='container'> <a class='accordian' href='#'>click me</a> <span class='arrow'>></span> <div class='content'>Show//Hide content</div> </div> <div class='container'> <a class='accordian' href='#'>click me</a> <span class='arrow'>></span> <div class='content'>Show//Hide content</div> </div>

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

相关问题 包含某些类字符串的jQuery显示元素 - jQuery display element that contains certain class strings 如果一个类包含某个名称,请更改该元素的样式 - If a class contains a certain name, change the style of that element JS - 将 Class 添加到包含某个单词的链接 - JS - Add Class to link that contains a certain word 检查元素是否具有某个 class 并且属于包含某个值的表单 - Check if element has a certain class and belongs to a form that contains a certain value 错误:尝试使用 Next JS next-optimize-image 时,输入缓冲区包含不受支持的图像格式 - Error: Input buffer contains unsupported image format when trying to use Next JS next-optimize-image 如果span元素包含具有特定类的div,则隐藏一个元素 - hide an element if span element contains div with a certain class 如何检查单击的元素是否包含具有特定类的元素 - How to check if clicked element contains element with certain class Magento 1.9.3.8中的Javascript。 如果body包含某些类并且元素id包含类,则向元素添加id - Javascript in Magento 1.9.3.8. Add id to element if body contains certain class and an element id contains a class JS 更改具有特定 id 和 class 名称的元素 - JS alter element with certain id and class name 如果元素包含某些文本 - If element contains certain text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM