[英]how to make every button have its respective text appear next to it, when hovered and still apply the type animation
const navList = document.getElementById("navigation"); const hoveredHome = document.querySelector(".nav_home"); const hoveredAbout = document.querySelector(".nav_about"); const hoveredSkills = document.querySelector(".nav_skills"); const hoveredContact = document.querySelector(".nav_contact"); const texts = [' // Home']; const text2 = [' // About'] let count = 0; // counts the individual characters in array numbers let characters = 0; // counts the individual letters/characters let currentWord = ''; let letter = ''; //specifies individual letter, one by one function typing() { // currentWord needs to change depending on what button is hovered currentWord = texts[count]; letter = currentWord.slice(0, ++characters); //adds one character at a time document.querySelector(".nav_home").textContent = letter; //adds letter setTimeout(typing, 50); }; document.querySelector(".hovered").addEventListener('mouseover', typing);
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.3/css/all.css" integrity="sha384-SZXxX4whJ79/gErwcOYf+zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk" crossorigin="anonymous"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <nav id="navigation"> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-home"></div></i><p class="nav_home"></p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-question-circle"></div></i><p class="nav_about"></p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-code"></i></div><p class="nav_skills"></p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-envelope"></div></i><p class="nav_contact"></p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-blog"></i></div><p class="nav_blog"></p></a></div> </nav> </div> </div> <script src="./javaScript/navAnimate.js"></script> </body>
Edited question, im trying to make it so when the icons are hovered text appears next to them, and eventually when they are not hovered the text dissapears.编辑过的问题,我试图让它在图标悬停时出现在它们旁边,最终当它们没有悬停时,文本消失。
You don't need javascript at all.您根本不需要 javascript。 You can do this with pure CSS - in only one line (once you do the initial styling) .
你可以用纯 CSS 做到这一点 - 只需一行(一旦你做了初始样式) 。 This is the one line that does the work:
这是完成工作的一行:
a:hover .nav_text{display:block;}
a{display:flex;min-height:50px;}.nav_text{display:none;}.hovered{display:flex;align-items:center;padding-right:5px;} #navigation a:hover.nav_text{display:block;}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.3/css/all.css" integrity="sha384-SZXxX4whJ79/gErwcOYf+zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk" crossorigin="anonymous"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <nav id="navigation"> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-home"></i></div><p class="nav_text">Home</p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-question-circle"></i></div><p class="nav_text">Aboot, eh?</p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-code"></i></div><p class="nav_text">Skills</p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-envelope"></i></div><p class="nav_text">Contact</p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-blog"></i></div><p class="nav_text">Blog</p></a></div> </nav> </div> </div> </body>
However, if you wanted to do it with javascript, here's the code that would make this work for you:然而,如果你想用 javascript 来做,这里的代码会让你工作:
//Look closely - this is NOT jQuery //const $ = document.querySelector.bind(document); //<== not needed in _this_ example const $$ = document.querySelectorAll.bind(document); const links = $$('#navigation.hovered'); $$('#navigation a').forEach(link => { link.addEventListener('mouseover', evt => { const atag = evt.target.closest('a'); const ntxt = atag.getElementsByClassName('nav_text'); ntxt[0].style.display = 'block'; }); link.addEventListener('mouseout', evt => { const atag = evt.target.closest('a'); const ntxt = atag.getElementsByClassName('nav_text'); ntxt[0].style.display = 'none'; }); });
#navigation a{display:flex;min-height:50px;}.nav_text{display:none;}.hovered{display:flex;align-items:center;padding-right:5px;}.nav_show{display:block;}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.3/css/all.css" integrity="sha384-SZXxX4whJ79/gErwcOYf+zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk" crossorigin="anonymous"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <nav id="navigation"> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-home"></i></div><p class="nav_text nav_home">Home</p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-question-circle"></i></div><p class="nav_text nav_about">Aboot, eh?</p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-code"></i></div><p class="nav_text nav_skills">Skills</p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-envelope"></i></div><p class="nav_text nav_contact">Contact</p></a></div> <div class="length_size"><a href="#"><div class="hovered"><i class="fas fa-blog"></i></div><p class="nav_text nav_blog">Blog</p></a></div> </nav> </div> </div> </body>
You might already know this, but please note that I prefixed several element tagNames/classNames with #navigation
just to ensure that the correct a
tags or .hovered
elements were selected (in case you had others elsewhere on your page - quite likely with the a tag).您可能已经知道这一点,但请注意,我在几个元素 tagNames/classNames 前加上
#navigation
只是为了确保选择了正确a
标签或.hovered
元素(以防您页面上的其他地方有其他元素 - 很可能使用 a标签)。 If you added class names to the a
tags (and used the className instead of a
, and ensured that the .hovered
className was truly unique, you would not need the #navigation
selector.如果您将 class 名称添加到
a
标签(并使用 className 而不是a
,并确保.hovered
className 真正唯一,则不需要#navigation
选择器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.