I am trying to get a horizontal bar to appear below a link when a user hovers over the link.
let galleryAnchor = document.querySelector('#gallery-anchor')
let galleryAnchorBar = document.querySelector('#gallery-anchor-bar')
console.log('galleryAnchorBar.style.left = ' + galleryAnchorBar.style.left)
console.log('galleryAnchorBar.style.top = ' + galleryAnchorBar.style.top)
galleryAnchorBar.style.left = galleryAnchor.style.left
galleryAnchorBar.style.top = parseInt(galleryAnchor.style.bottom, 10) + 2 + 'px'
console.log('galleryAnchorBar.style.left = ' + galleryAnchorBar.style.left)
console.log('galleryAnchorBar.style.top = ' + galleryAnchorBar.style.top)
galleryAnchor.addEventListener('mouseenter', () => {
galleryAnchorBar.style.width = galleryAnchor.style.width
})
galleryAnchor.addEventListener('mouseleave', () => {
galleryAnchorBar.style.width = 0
})
#gallery-anchor-bar {
position: fixed;
width: 0px;
height: 2px;
background: linear-gradient(to right, #FF9966, #FF6565);
border-radius: 1px;
transition: all 500ms;
}
<div id="nav-anchor-grid">
<a id="gallery-anchor" href="gallery.html">Gallery</a>
<a id="organizations-anchor" href="organizations.html">Organizations</a>
<a id="about-anchor" href="about-me.html">About Me</a>
</div>
<div id="gallery-anchor-bar"></div>
With this code, nothing happens when I hover over the link. If instead I add left: 500px;
(or any other value), top: 30px
(or any other value) and set width: 500px
(or any other value) to my CSS, the bar appears on page load. When I hover over the gallery link and then move the cursor elsewhere, it disappears. And when I hover over the link again, it expands to the width set in the CSS (not the width of the anchor as I have specified in the JavaScript). I cannot figure out what is happening. The console.log()
statements only print the hardcoded string without the values I have specified. What surprises me most is that the bar expands to the width specified in the CSS when code for the eventListener on mouseenter runs, not the width of the anchor.
The snippet with the modified CSS:
let galleryAnchor = document.querySelector('#gallery-anchor')
let galleryAnchorBar = document.querySelector('#gallery-anchor-bar')
console.log('galleryAnchorBar.style.left = ' + galleryAnchorBar.style.left)
console.log('galleryAnchorBar.style.top = ' + galleryAnchorBar.style.top)
galleryAnchorBar.style.left = galleryAnchor.style.left
galleryAnchorBar.style.top = parseInt(galleryAnchor.style.bottom, 10) + 2 + 'px'
console.log('galleryAnchorBar.style.left = ' + galleryAnchorBar.style.left)
console.log('galleryAnchorBar.style.top = ' + galleryAnchorBar.style.top)
galleryAnchor.addEventListener('mouseenter', () => {
galleryAnchorBar.style.width = galleryAnchor.style.width
})
galleryAnchor.addEventListener('mouseleave', () => {
galleryAnchorBar.style.width = 0
})
#gallery-anchor-bar {
position: fixed;
left: 500px;
top: 30px;
width: 500px;
height: 2px;
background: linear-gradient(to right, #FF9966, #FF6565);
border-radius: 1px;
transition: all 500ms;
}
<div id="nav-anchor-grid">
<a id="gallery-anchor" href="gallery.html">Gallery</a>
<a id="organizations-anchor" href="organizations.html">Organizations</a>
<a id="about-anchor" href="about-me.html">About Me</a>
</div>
<div id="gallery-anchor-bar"></div>