简体   繁体   English

CSS-将鼠标悬停在元素上会影响多个元素

[英]CSS - hovering over element affects multiple elements

I have a menu that is floating right on the screen and when hovering over one of the elements it does what it has to do, the opacity is changing of the rest and only the hovered element is emphasized. 我有一个在屏幕上浮动的菜单,当将鼠标悬停在某个元素上时,它会做它必须做的事情,其余部分的不透明度正在改变,仅强调了悬停的元素。 But when hovering between the elements on the empty space, a small portion of the space in the middle changes opacity to all elements. 但是,当将鼠标悬停在空白空间上的元素之间时,中间的一小部分空间会更改所有元素的不透明度。 If I hover over an element near the bottom or top of it and the spaces between all elements, it does what it should. 如果将鼠标悬停在其底部或顶部附近的元素以及所有元素之间的空格上,它会执行应有的操作。 I know it has something to do with the <ul> tag but don't know how to solve it. 我知道它与<ul>标记有关,但不知道如何解决。 Here is the code: 这是代码:

HTML 的HTML

<div class="menu">
  <ul>
    <li><a>Home</a></li>
    <li><a>About</a></li>
    <li><a>Projects</a></li>
    <li><a>Services</a></li>
    <li><a>Contact</a></li>
  </ul>
</div>


CSS 的CSS

.menu {
    float:right;
    margin-right: 50px;
    }
ul li {
    display: inline;
    text-decoration: none; 
    color: #003560;
    padding: 10px 50px;
    transition: all ease .3s;
    }
ul:hover li {
    color: #0089F9;
    opacity: .1;
    transition: all ease .3s;
    }
ul li:hover {
    opacity: 1;
    padding: 15px 55px;
    }
li {
    border: 2px solid #0089F9;
    border-radius: 5px;
    margin-left: 20px;
    }


https://jsfiddle.net/Vc2ug/49/ https://jsfiddle.net/Vc2ug/49/

You can use float: left instead of display: inline 您可以使用float: left而不是display: inline

ul li {
    display: block;
    float: left;
    text-decoration: none; 
    color: #003560;
    padding: 10px 50px;
    transition: all ease .3s;
}

https://jsfiddle.net/7w8w86my/1/ https://jsfiddle.net/7w8w86my/1/

ul:hover li {
  transition: all ease .3s;
}

Remove opacity and color properties for and make ul:hover li as above. 删除的不透明性和颜色属性,并如上所述使ul:hover li Actually it means when moves is on ul tag, apply following properties lo li. 实际上,这意味着当移动位于ul标签上时,请应用以下属性lo li. So, when you are placing between two list items, it is on ul and so it i applying opacity to all list items. 因此,当您放置在两个列表项之间时,它在ul ,因此我将opacity应用于所有列表项。

Just add to your <ul> some padding at bottom and top. 只需添加到您的<ul>在底部和顶部一些填充。 Like this: 像这样:

ul {
    padding: 50px 0 50px 0;
}

Here is a demo. 是一个演示。

只需删除<li>之间的HTML中的空格即可

Your problem that when you direct the cursor at space between li all ul becomes hovered. 当您将光标指向所有ul之间的空间时,您的问题变得悬而未决。 You need to change structure of html and to refuse use of ul or to add a small script. 您需要更改html的结构并拒绝使用ul或添加一个小的脚本。 For example so: https://jsfiddle.net/trean/sxoxpz2e/2/ , https://jsfiddle.net/trean/sxoxpz2e/2/embedded/result/ 例如: https : //jsfiddle.net/trean/sxoxpz2e/2/,https : //jsfiddle.net/trean/sxoxpz2e/2/embedded/result/

$('.menu > ul > li').hover(function() {
        $(this).parent().find('li').addClass('unhover');
    }, function() {
        $(this).parent().find('li').removeClass('unhover');
 });

CSS: CSS:

.unhover {
    color: #0089F9;
    opacity: .1;
    transition: all ease .3s;
    }

I don't exclude that there are ways to do without jquery, but so far anything to mind doesn't come, except refusal of ul in favor of hierarchy div. 我不排除有没有jquery的方法,但是到目前为止,除了拒绝ul赞成分层div之外,什么都没想到。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM