[英]When hovering over an element with a certain index, change the styles of the element in another parent with the same index
I have a menu on the site in two places.我在网站上有两个地方的菜单。 One is made by text, and the other by pictures.一种是文字,一种是图片。 You can click on both.您可以单击两者。
I want that when you hover over a specific item in a text menu (for example, under number 2), the picture with the same number changes (for example, under 2).我希望当您 hover 在文本菜单中的特定项目上(例如,在数字 2 下)时,具有相同数字的图片会发生变化(例如,在 2 下)。
Code for text menu:文本菜单代码:
<ul>
<li class="page_item">
<a href="#">Test 1</a>
</li>
<li class="page_item">
<a href="#">Test 2</a>
</li>
</ul>
Code for Pictures menu:图片菜单代码:
<div class="project__card project__card-design">
<div class="project__card-design-bigelem">
<a href="#" class="project__img-link project__img-fromdesign" style="background-image: url(https://images.pexels.com/photos/927022/pexels-photo-927022.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500);"></a>
</div>
<div class="project__card-design-bigelem">
<a href="#" class="project__img-link project__img-fromdesign" style="background-image: url(https://images.pexels.com/photos/927022/pexels-photo-927022.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500);"></a>
</div>
<div class="project__card-design-bigelem">
<a href="#" class="project__img-link project__img-fromdesign" style="background-image: url(https://images.pexels.com/photos/927022/pexels-photo-927022.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500);"></a>
</div>
</div>
Screen shot with Picture and text menu: Screen shot with Picture and text menu带有图片和文本菜单的屏幕截图:带有图片和文本菜单的屏幕截图
I will be grateful for any help!我将不胜感激任何帮助!
Since I was looking for solutions that could identify the element with which number was highlighted.因为我正在寻找可以识别突出显示数字的元素的解决方案。 But so far I don't even have ideas on how to do this.但到目前为止,我什至没有关于如何做到这一点的想法。
All thanks in advance for any help!在此先感谢您的帮助!
Here's a solution with pure js, works for elements that are not parents using ids.这是一个纯 js 的解决方案,适用于不是使用 id 的父元素的元素。
html html
<li id="child1" onmouseenter="customHover(event)" onmouseleave="handlemouseleave(event)"></li>
<div id="parent1"> </div>
<li id="child2" onmouseenter="customHover(event)" onmouseleave="handlemouseleave(event)"></li>
<div id="parent2"> </div>
<li id="child3" onmouseenter="customHover(event)" onmouseleave="handlemouseleave(event)"></li>
<div id="parent3"> </div>
and heres the js继承人的js
function customHover(e){
let id = e.target.id;
let idNumber = id.slice(id.length - 1);
document.getElementById(`parent${idNumber}`).style.border = '1px solid red';
}
function handleMouseLeave(e){
let id = e.target.id;
let idNumber = id.slice(id.length - 1);
document.getElementById(`parent${idNumber}`).style.border = 'unset';//or whatever you need to change the styles back to the original
}
If you like for this behaviour you can do this如果你喜欢这种行为,你可以这样做
hover: nav1 > imageNav1 ect... hover:导航1 > imageNav1 等...
You can get the index of the hover item and match that to the image nav item.您可以获取 hover 项目的索引并将其与图像导航项目匹配。 Sorry for the markup, it's just to show you how you can implement it.抱歉标记,这只是向您展示如何实现它。 You can also choose to do whatever after the matching is made in the mouseenter
您也可以选择在mouseenter
中进行匹配后执行任何操作
$(".js-hover").on("mouseenter", function () { const hoverIndex = $(this).index(); const $imageListItems = $(".image-list > li"); $imageListItems.removeClass("image-list__item--selected"); $imageListItems.eq(hoverIndex).addClass("image-list__item--selected"); });
.image-list__item--selected { background-color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="list"> <li class="js-hover">text</li> <li class="js-hover">text</li> <li class="js-hover">text</li> <li class="js-hover">text</li> </ul> <ul class="image-list"> <li>image1</li> <li>image2</li> <li>image3</li> <li>image4</li> </ul>
there are many solutions, with an without using libraries.有很多解决方案,没有使用库。 I think you may use some jquery if possible, and if not you should search for addeventlistener (the advanced way)我想你可能会使用一些 jquery 如果可能的话,你应该搜索 addeventlistener (高级方式)
https://api.jquery.com/hover/ https://api.jquery.com/hover/
is a good example of doing what you are trying todo.是做你想做的事情的一个很好的例子。
var pageitemcount=0;
$( ".page_item" ).hover(function() {
pageitemcount++;
$.post("/mypageitemcounter.php",{pageitemcount:pageitemcount});
$(this).parent().append( $( "<span>"+pageitemcount+"</span>" ) );
});
The above part is for php, still can be used in a plugin.以上部分是针对 php 的,仍然可以在插件中使用。 If you are in wordpress environment, you have to dig into how to write wp plugins also.如果您在 wordpress 环境中,您还必须深入研究如何编写 wp 插件。 Trying to achieve this in an environment, and then applying the same to your custom wp plugin is the way to go.尝试在环境中实现这一点,然后将其应用于您的自定义 wp 插件是 go 的方法。 Do not change the existing plugins, or themes if possible.如果可能,请勿更改现有插件或主题。 This may cause headaches after an update.. In wp environment, writing a custom plugin is the way to go.这可能会在更新后引起头疼。在 wp 环境中,编写自定义插件是 go 的方式。 You should tag your question as wp-plugin if possible.如果可能,您应该将您的问题标记为 wp-plugin。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.