简体   繁体   English

如何通过将鼠标悬停在不同容器中的元素/链接上来更改一个容器的内容

[英]How to change the contents of one container by hovering over elements/links in a DIFFERENT container

I'm currently developing an e-catalogue.我目前正在开发一个电子目录。 Following the suggestion of a Stackoverflow contributor, I now have a navigation bar with a dropdown that produces a two-container menu.根据 Stackoverflow 贡献者的建议,我现在有了一个带有下拉菜单的导航栏,可以生成一个包含两个容器的菜单。 Next, I am trying to set it in such a way that when one hovers over a specific element in one container (the left container), the contents of the other container (the right one) of the dropdown, change and allows the user, interact with the changed contents of the right container.接下来,我试图以这样一种方式设置它,当一个容器(左侧容器)中的特定元素悬停时,下拉列表的另一个容器(右侧)的内容会更改并允许用户,与正确容器的更改内容进行交互。

I am using 'mouseenter' and 'mouseleave' event handlers, and yes, you've guessed it, the moment I leave the element (in the left container) I hovered over to trigger the change (in the right container), the contents of the right container reverse back to their original contents!我正在使用“mouseenter”和“mouseleave”事件处理程序,是的,你已经猜到了,当我离开元素(在左侧容器中)时,我将鼠标悬停在触发更改(在右侧容器中),内容正确的容器反转回原来的内容!

To get 'round' this problem, within the mouseenter eventlistener function, I wrote in a second mouseenter eventlistener function, this time for the right container itself.为了解决这个问题,在 mouseenter eventlistener 函数中,我编写了第二个 mouseenter eventlistener 函数,这次是针对正确的容器本身。 I hoped that if I left the element (in the left container), that triggered the change in the right container, and went into the right container, I would be able to interact with the altered contents of the right container.我希望如果我离开元素(在左侧容器中),触发右侧容器中的更改,然后进入右侧容器,我将能够与右侧容器中更改的内容进行交互。 Alas, it worked it!唉,它奏效了!

The problem now, however, is that ANYTIME I hover over the right container, the contents change as if I had hovered over the specific element in the left container, regardless of whether or not I had actually hovered over that element in the left container.然而,现在的问题是,每当我将鼠标悬停在右容器上时,内容都会发生变化,就好像我将鼠标悬停在左容器中的特定元素上一样,无论我是否实际上将鼠标悬停在左容器中的那个元素上。

I have tried numerous approaches, including, 'mouseout' (which 'bubbles'), and also tried giving the two mouseenter event functions, names, so that the 'inner' function for the mouseenter event of the right container only executes when the 'outer' function for the mouseenter event of the element in the left container has executed (a very Pythonistic way of thinking!), but nothing has worked.我尝试了许多方法,包括“mouseout”(“冒泡”),还尝试为两个 mouseenter 事件函数命名,以便正确容器的 mouseenter 事件的“内部”函数仅在“左侧容器中元素的 mouseenter 事件的 external' 函数已执行(一种非常 Python 主义的思维方式!),但没有任何效果。

I need to keep a mouseout or mouseleave event of some sort for the element in the left container;我需要为左容器中的元素保留某种 mouseout 或 mouseleave 事件; otherwise, the change in the right container will persist as you move the mouse on to other elements in the left container.否则,当您将鼠标移到左侧容器中的其他元素上时,右侧容器中的更改将持续存在。

Ultimately, I want each element in the left container to trigger different changes in the contents of the right container, much like what you see in the dropdown here .最终,我希望左侧容器中的每个元素触发右侧容器内容的不同变化,就像您在此处的下拉列表中看到的一样。

A minimal working version of my code is shown below:我的代码的最小工作版本如下所示:

 // block-1h selectors const breakingLine = document.querySelector(".breakingline"); const breaking = document.querySelector(".breaking"); // block-2h selector(s) const block2H = document.querySelector(".block-2h"); const drop2HCaptionText = document.querySelector(".drop-2h-captiontext"); // Event listeners breakingLine.addEventListener("mouseenter", function() { drop2HCaptionText.textContent = "Camon C2000 Rotavator"; block2H.addEventListener("mouseenter", function() { drop2HCaptionText.textContent = "Camon C2000 Rotavator"; }) }) breaking.addEventListener("mouseleave", function() { drop2HCaptionText.textContent = "Boss Ladderspan 3T Scaffold Tower (Single Width)"; block2H.addEventListener("mouseleave", function() { drop2HCaptionText.textContent = "Boss Ladderspan 3T Scaffold Tower (Single Width)"; }) })
 .nav-list { list-style: none; border: 1px solid blue; display: flex; flex-flow: row wrap; align-items: left; justify-content: left; color: white; background-color: #429CD9; } #hire-dropdown { position: absolute; cursor: pointer; padding-right: 3em; padding-left: 3em; } .hdrop, .block-1h, .block-2h { display: none; } #hire-dropdown:hover * { display: grid; } #hire-dropdown .hdrop { grid-template-areas: "block-1h block-2h"; grid-template-columns: 1fr 1fr; } .block-1h { grid-area: "block-1h"; height: 30em; } .block-2h { grid-area: "block-2h"; background-color: white; border: 1px solid black; height: 40em; } .drop-1h-list { list-style: none; display: block; border: 1px solid black; background-color: white; height: 40em; } .drop-most-popular-hire, .drop-2h-captiontext { color: #3D3F41; } .drop-most-popular-hire { padding-left: 3em; }
 <nav> <ul class="nav-list"> <li>Nothing</li> <li class="to-hire"> <div id="hire-dropdown">To Hire <div class="hdrop"> <div class="block-1h"> <ul class="drop-1h-list"> <li><a href="#">Access</a></li> <li class="breakingline"><a class="breaking" href="#">Breaking</a></li> <li class="compactionline"><a class="compaction" href="#">Compaction</a></li> </ul> </div> <div class="block-2h"> <h3 class="drop-most-popular-hire">Our most popular product in this category</h3> <p class="drop-2h-captiontext">Boss Ladderspan 3T Scaffold Tower (Single Width)</p> </div> </div> </div> </li> </ul> </nav>

Your (constructive) help will be most appreciated.您的(建设性)帮助将不胜感激。

You could just remove the mouseleave event listener.您可以删除mouseleave事件侦听器。

When you enter a li element in the left container, the mouseenter event listener will be fired and the text in the right container will be changed.当您在左侧容器中输入 li 元素时,将触发mouseenter事件侦听器并更改右侧容器中的文本。

The content will be changed to another text only when you enter another element in the left container.只有当您在左侧容器中输入另一个元素时,内容才会更改为另一个文本。

 const drop2HCaptionText = document.querySelector(".drop-2h-captiontext"); let texts = [ "Boss Ladderspan 3T Scaffold Tower (Single Width)", "Camon C2000 Rotavator", "Boss Ladderspan 3T Scaffold Tower (Single Width)", ] let listItems = document.querySelectorAll('.drop-1h-list li'); listItems.forEach((item, index) => { item.addEventListener('mouseenter', () => { drop2HCaptionText.textContent = texts[index]; }) })
 .nav-list { list-style: none; border: 1px solid blue; display: flex; flex-flow: row wrap; align-items: left; justify-content: left; color: white; background-color: #429CD9; } #hire-dropdown { position: absolute; cursor: pointer; padding-right: 3em; padding-left: 3em; } .hdrop, .block-1h, .block-2h { display: none; } #hire-dropdown:hover * { display: grid; } #hire-dropdown .hdrop { grid-template-areas: "block-1h block-2h"; grid-template-columns: 1fr 1fr; } .block-1h { grid-area: "block-1h"; height: 30em; } .block-2h { grid-area: "block-2h"; background-color: white; border: 1px solid black; height: 40em; } .drop-1h-list { list-style: none; display: block; border: 1px solid black; background-color: white; height: 40em; } .drop-most-popular-hire, .drop-2h-captiontext { color: #3D3F41; } .drop-most-popular-hire { padding-left: 3em; }
 <nav> <ul class="nav-list"> <li>Nothing</li> <li class="to-hire"> <div id="hire-dropdown">To Hire <div class="hdrop"> <div class="block-1h"> <ul class="drop-1h-list"> <li><a href="#">Access</a></li> <li class="breakingline"><a class="breaking" href="#">Breaking</a></li> <li class="compactionline"><a class="compaction" href="#">Compaction</a></li> </ul> </div> <div class="block-2h"> <h3 class="drop-most-popular-hire">Our most popular product in this category</h3> <p class="drop-2h-captiontext">Boss Ladderspan 3T Scaffold Tower (Single Width)</p> </div> </div> </div> </li> </ul> </nav>

暂无
暂无

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

相关问题 将鼠标悬停在一个div容器中的图像上,以使img或背景出现在另一个容器中或上方 - Hovering over an image in one div container to have an img or background appear in or over top of a different container 当悬停除两个元素之外的所有元素时,如何使菜单容器隐藏? - How to make menu container hide when hovering all but two elements? 如何在Shiny中悬停元素时更改绘图? - How to change a plot when hovering over elements in Shiny? 将鼠标悬停在按钮上时如何影响不同的类或HTML元素? - How to impact different class or HTML elements when hovering over a button? 角4,如何根据条件将元素放入一个容器或另一个容器中? - Angular 4, How to put elements in one container or the other container based on condition? Dragula-将元素从一个容器拖到不同的元素 - Dragula - Drag elements from one container to different elements 悬停在不同链接上时创建具有不同内容的对话框 - Creating Dialog Box with Different Contents when Hovering on Different Links 如何根据容器更改元素的宽度? - How to change the width of elements based on container? 将鼠标悬停在另一类上时如何更改它的CSS? - How to change css of one class when hovering over another? 悬停在下拉菜单选项合并在容器后面,如何获得下拉菜单显示在容器上方 - Hovering on the drop down the options is merged behind the container, how do i get the drop down options displayed over the container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM