[英]change CSS first child element when hover second or third element
I want to change the CSS of first child element when hovering second or third element 我想在悬停第二个或第三个元素时更改第一个子元素的CSS
My HTML, I want to show "no-thumb" and hide "has-thumb" when hovering second or third element 我的HTML,我想在悬停第二个或第三个元素时显示“ no-thumb”并隐藏“ has-thumb”
.TopList .lists:hover .has-thumb, .no-thumb { display: none; } .TopList .lists:hover .has-thumb { display: block; }
<div class="TopList"> <div class="lists"> <div class="no-thumb" style="display:none"></div> <div class="has-thumb" style="display: block"></div> </div> <div class="lists"> <div class="no-thumb"></div> <div class="has-thumb"></div> </div> <div class="lists"> <div class="no-thumb"></div> <div class="has-thumb"></div> </div> </div>
A JavaScript solution would be to simply add or remove a pre-made CSS class that hides an element as the second and third items are moused over and moused out. JavaScript解决方案将是简单地添加或删除一个预制的CSS类,该类在将第二项和第三项鼠标悬停并移出时会隐藏元素。
// Get references to the elements that will be shown/hidden let noThumb = document.querySelector(".no-thumb"); let hasThumb = document.querySelector(".has-thumb"); // Set up an event handler for when the entire list is hovered document.querySelector(".TopList").addEventListener("mouseover", function(event){ // Check to see if the element being hovered is one we care about if(event.target.parentNode.classList.contains("hover")) { // Hide and show the elements. noThumb.classList.remove("hidden"); hasThumb.classList.add("hidden"); } }); document.querySelector(".TopList").addEventListener("mouseout", function(event){ if(event.target.parentNode.classList.contains("hover")) { noThumb.classList.add("hidden"); hasThumb.classList.remove("hidden"); } });
.hidden { display:none; } .lists { cursor:pointer; }
<div class="TopList"> <div class="lists"> <div class="no-thumb hidden">no thumb</div> <div class="has-thumb">has thumb</div> </div> <div class="lists hover"> <div class="no-thumb">x</div> <div class="has-thumb">x</div> </div> <div class="lists hover"> <div class="no-thumb">x</div> <div class="has-thumb">x</div> </div> </div>
There is no "previous element" selector in css but you can use the :hover selector of the parent element together with :first-child CSS中没有“上一个元素”选择器,但是您可以将父元素的:hover选择器与:first-child一起使用
See snippet: Show no-thumb of first element when hovering over 2nd or 3rd element. 请参见摘要:将鼠标悬停在第二个或第三个元素上时,不显示第一个元素的缩略图。
.no-thumb { display: none; } .TopList:hover .lists:first-child .has-thumb { display: none; } .TopList:hover .lists:first-child .no-thumb { display: block; } .TopList:hover .lists:first-child:hover .has-thumb { display: block; } .TopList:hover .lists:first-child:hover .no-thumb { display: none; } .lists { border: 1px solid black; }
<div class="TopList"> <div class="lists"> <div class="no-thumb">no-thumb</div> <div class="has-thumb">has-thumb</div> </div> <div class="lists"> <div class="no-thumb">no-thumb</div> <div class="has-thumb">has-thumb</div> </div> <div class="lists"> <div class="no-thumb">no-thumb</div> <div class="has-thumb">has-thumb</div> </div> </div>
you should use javascript or jQuery 您应该使用javascript或jQuery
jQuery('.lists').mouseover(function () {
if (!jQuery(this).is(':first-child')) {
jQuery('.lists:first-child .no-thumb').show();
jQuery('.lists:first-child .has-thumb').hide();
}
}).mouseleave(function () {
jQuery('.lists:first-child .no-thumb').hide();
jQuery('.lists:first-child .has-thumb').show();
})
for do that in vanilla javascrpt your HTML changes in this 在香草javascrpt中做到这一点,您的HTML更改在此
<div class="TopList" id="TopList">
<div class="lists">
<div class="no-thumb" style="display:none"></div>
<div class="has-thumb" style="display: block"></div>
</div>
<div class="lists" onmouseenter="javascript:overfunction()" onmouseout="javascript:blurfunction()">
<div class="no-thumb"></div>
<div class="has-thumb"></div>
</div>
<div class="lists" onmouseenter="javascript:overfunction()" onmouseout="javascript:blurfunction()">
<div class="no-thumb"></div>
<div class="has-thumb"></div>
</div>
</div>
and the scripts in this 和这个脚本
<script>
function overfunction(){
var firstEl = document.getElementById('TopList').children[0]
firstEl.children[0].style.display = 'block'
firstEl.children[1].style.display = 'none'
}
function blurfunction(){
var firstEl = document.getElementById('TopList').children[0]
firstEl.children[0].style.display = 'none'
firstEl.children[1].style.display = 'block'
}
</script>
Please try with this, It is working as expected only CSS is used.
请尝试使用此工具,它可以正常工作,仅使用CSS。
.TopList:hover .lists:first-child .no-thumb{ display:block !important; } .TopList:hover .lists:first-child .has-thumb{ display:none !important; }
<div class="TopList"> <div class="lists"> <div class="no-thumb" style="display:none">no-thumb</div> <div class="has-thumb" style="display: block">has-thumb</div> </div> <div class="lists"> <div class="no-thumb">second-no</div> <div class="has-thumb">second-has</div> </div> <div class="lists"> <div class="no-thumb">third-no</div> <div class="has-thumb">third-has</div> </div> </div>
See This solution and use accordingly-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.