简体   繁体   English

再次单击后如何将事件恢复为原始状态?

[英]How to revert event to its original state after clicking on it again?

I'm relatively new to web-development and I am building a portfolio website.我对网络开发比较陌生,我正在建立一个投资组合网站。 I have these triangle bullet points I made using borders.我有这些使用边框制作的三角形要点。 Originally, they are pointing to the right and the idea is to be able to click on them and then have them point down (like an arrow) to then show some image (haven't gotten to that part yet) and then point back to the right when clicked again.最初,它们指向右侧,想法是能够单击它们,然后让它们指向下方(如箭头),然后显示一些图像(尚未到达该部分),然后再指向再次单击时的右侧。 I can get them to point down, but I tried implementing both point down and point left functions and now nothing happens when I click on it.我可以让它们指向下方,但我尝试同时实现指向下方和指向左侧功能,现在单击它时什么也没有发生。 I tried using an if statement but that didn't work either.我尝试使用 if 语句,但这也不起作用。 I did a lot of research but nothing really covered my exact problem, maybe I'm not googling the right terms.我做了很多研究,但没有真正涵盖我的确切问题,也许我没有在谷歌上搜索正确的术语。

const projectList = document.getElementById('projects').querySelector('ul');
const projectPsuedo = projectList.querySelectorAll('span');

const switchBulletDown = event => {
    event.target.style.borderLeftColor = 'transparent';
    event.target.style.borderTopColor = '#111';
    event.target.style.left = '-1.2em';
    event.target.style.top = '1.6em';
}

const switchBulletRight = event => {
    event.target.style.borderLeftColor = '#111';
    event.target.style.borderTopColor = 'transparent';
    event.target.style.left = '-1em';
    event.target.style.top = '1.4em';
}

const eventHandler = psuedoClass => {
    if (psuedoClass.style.borderLeftColor === '#111') {
        psuedoClass.addEventListener('click', switchBulletDown);
    } else {
        psuedoClass.addEventListener('click', switchBulletRight);
    }    
}

projectPsuedo.forEach(eventHandler);

Ok I got it to work and I used the .toggle method to switch between classes.好的,我让它工作了,我使用 .toggle 方法在类之间切换。

const projectList = document.getElementById('projects').querySelector('ul');
const projectPsuedo = projectList.querySelectorAll('span');

const eventHandler = psuedoClass => {
    psuedoClass.addEventListener('click', () => {
        psuedoClass.classList.toggle('point-down')
    })
}

projectPsuedo.forEach(eventHandler);

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

相关问题 如何在单击时更改按钮的 state 并在 React 中再次单击时恢复为原始 state? - How to change state of button on click and revert back to the original state when clicked again in React? 为什么这段代码改变了字符串但又恢复到原始状态 - why does this code change the string but again revert back to original state JavaScript - 单击另一个元素后将元素恢复为原始样式 - JavaScript - Revert element to original style after clicking another element 如何在被覆盖后将节点上的 Object.defineProperty 恢复为原始 state? - How do I revert Object.defineProperty on Node to the original state after it was overridden? 项目没有保存,重新点击编辑后恢复到原来的状态 - Item is not saving, changing back to its original state after re-clicking edit 点击事件 - 如何让点击事件逐渐恢复到原来的状态 - click event - how to make click event revert back to original gradually 如何在拖动时使项目恢复到原始位置? - How to make an item revert back to its original position on drag? 茉莉花:使用callFake之后,如何恢复到原始功能? - Jasmine: after using callFake, how can you revert to the original function? 如何在.css()方法之后恢复到原始的css值 - how to revert to original css values after .css() method 如果更改,如何将值重置为其原始状态 - How to reset a value to its original state if changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM