I cannot figure out how to make my navigation hover effect work. If you visit this site: https://sw6.boxolutions.de/ you will see the issue. I am trying to change my navigation icons on hover. Therefore i tried to add html properties onmouseover()
and onmouseout()
to my a
tags. These functions are made to change the background url attribute of my wrapper-naviagtion-icons
based on mouseevent. Somehow it is not working.
<a class="nav-link main-navigation-link"
href="{{ category_url(category) }}"
itemprop="url"
onmouseenter="hover()"
onmouseleave="unhover()"
{% if treeItem.children|length > 0 %}data-flyout-menu-trigger="{{ category.id }}"{% endif %}
{% if category_linknewtab(category) %}target="_blank"{% endif %}
title="{{ name }}">
<div class="main-navigation-link-text">
{# ThemeWare: Add navigation Icons - no hover #}
<div class="container">
<div class="wrapper-navigation-container">
<div class="wrapper-naviagtion-icons"
style="background: url({{category.customFields.twt_modern_pro_custom_field_set__category_navigation_icon_desktop_url}}) no-repeat;">
</div>
</div>
{% set navigation_split_title = name|split('&') %}
<div class="wrapper-naviagtion-text-links">
<span itemprop="name"><b>{{ navigation_split_title[0]|upper }}</b></span></br>
<span itemprop="name">{{ navigation_split_title[1]|upper }}</span>
</div>
</div>
</div>
<script>
function hover() {
document.querySelector(".wrapper-naviagtion-icons").style.backgroundImage = "url{{category.customFields.twt_modern_pro_custom_field_set__category_navigation_icon_desktop_url_hover}}";
}
function unhover() {
document.querySelector(".wrapper-naviagtion-icons").style.backgroundImage = "url({{category.customFields.twt_modern_pro_custom_field_set__category_navigation_icon_desktop_url}})";
}
</script>
</a>
I also tried to a different way to display the hover state icon as a separate div and manipulate the display
state on :hover
by changing it to display:none;
. I cannot solve this with pure css because i do safe the icon urls in a twig template variable.
2
Answers
Pure css:
Using JQuery:
The reason why only the first one is changing is because when you use
querySelector
, it basically get the first element that match the criteria. Example, if you haveTo achieve what you wanted to do using javascript, here is a simplified example, basically is to show the concept:
You can pass in
event
when you set the attributeonmouseenter
andonmouseleave
. Then insidehover
function, you can know which element is user hovering by referring toe.target
. Refer to this page.