简体   繁体   English

在没有 JavaScript 的情况下向 DIV 添加悬停效果(纯 CSS)

[英]Adding Hover-Effect to DIV without JavaScript (pure CSS)

Assumed we have a simple, horizontal navigation of a website:假设我们有一个简单的网站水平导航:

| | Home |主页 | About |关于 | Products |产品 | Contact |联系方式 | Impress |... or similar...印象|...或类似...

Above the navigation there is a rectangle placed.在导航上方放置了一个矩形。

Now I want to automatically "slide" this rectangle horizontally (left/right), depending on the navigation entry that is currently hovered.现在我想根据当前悬停的导航条目自动水平(左/右)“滑动”这个矩形。

Example:例子:

  • If entry 2 ("About") is hovered, the rectangle should slide 5em to the right如果条目 2(“关于”)悬停,矩形应向右滑动5em
  • If entry 5 ("Impress") is hovered, the rectangle should slide 20em to the right, etc.如果条目 5(“Impress”)悬停,则矩形应向右滑动20em ,以此类推。

HTML: HTML:

<div id="rectangle"></div>

<ul class="navigation">
    <li><a href="#">Entry 1</a></li>
    <li><a href="#">Entry 2</a></li>
    <li><a href="#">Entry 3</a></li>
    <li><a href="#">Entry 4</a></li>
    <li><a href="#">Entry 5</a></li>
</ul>

CSS: CSS:

I am not yet sure on how to solve this in CSS.我还不确定如何在 CSS 中解决这个问题。 My approach would be something like that:我的方法是这样的:

#rectangle {
    background-color: powderblue;
    width: 10em;
    height: 2em;
    margin: 0 auto;
    position: absolute;
}

ul li:nth-child(1):hover ~ #rectangle {right:0em}
ul li:nth-child(2):hover ~ #rectangle {right:5em}
ul li:nth-child(3):hover ~ #rectangle {right:10em}
ul li:nth-child(4):hover ~ #rectangle {right:15em}
ul li:nth-child(5):hover ~ #rectangle {right:20em}

Unfortunately, this does not work as expected.不幸的是,这并没有按预期工作。 Did I do something wrong?我做错什么了吗?

 #rectangle { background-color: powderblue; width: 10em; height: 2em; margin: 0 auto; position: absolute; } ul li:nth-child(1):hover~#rectangle { right: 0em } ul li:nth-child(2):hover~#rectangle { right: 5em } ul li:nth-child(3):hover~#rectangle { right: 10em } ul li:nth-child(4):hover~#rectangle { right: 15em } ul li:nth-child(5):hover~#rectangle { right: 20em }
 <div id="rectangle"></div> <ul class="navigation"> <li><a href="#">Entry 1</a></li> <li><a href="#">Entry 2</a></li> <li><a href="#">Entry 3</a></li> <li><a href="#">Entry 4</a></li> <li><a href="#">Entry 5</a></li> </ul>

You could do display:none;你可以做 display:none; on the regular div and the use the pseudo class:hover to display the content.在常规 div 上并使用伪 class:hover 显示内容。

#mydiv {
display:none;
}
#mydiv:hover {
display:inline;
}

With the rectangle being a pseudo element, you could solve it this way (without an additional element):由于矩形是一个伪元素,你可以这样解决它(没有额外的元素):

 ul { position: relative; margin: 0; padding: 0; } ul li { display: inline-block; } ul li:last-child:after { content: ''; background-color: powderblue; width: 10em; height: 2em; margin: 0 auto; position: absolute; z-index: -1; left: 0; transition: .4s ease-in-out; } ul li:nth-child(1):hover~li:last-child:after { transform: translateX(0em); } ul li:nth-child(2):hover~li:last-child:after { transform: translateX(5em); } ul li:nth-child(3):hover~li:last-child:after { transform: translateX(10em); } ul li:nth-child(4):hover~li:last-child:after { transform: translateX(15em); } ul li:nth-child(5):hover:after { transform: translateX(20em); }
 <ul class="navigation"> <li><a href="#">Entry 1</a></li> <li><a href="#">Entry 2</a></li> <li><a href="#">Entry 3</a></li> <li><a href="#">Entry 4</a></li> <li><a href="#">Entry 5</a></li> </ul>

The simple way to make this with pure CSS is:使用纯 CSS 进行此操作的简单方法是:

just write class="navigation" and li hover只需写 class="navigation" 和 li hover

navigation li:hover{
opacity: 0.9;
padding-left: 3px;
padding-right: 7px;
background-color:#f2f2f2;
transition: 0.9s;

}

you can change other changes if you want...如果需要,您可以更改其他更改...

You can't using CSS (unless you follow these conditions ) but you can get the same effect by changing your styles with jQuery in the onMouseOver and onMouseOut parameters,您不能使用CSS (除非您遵循这些条件),但您可以通过在onMouseOveronMouseOut参数中使用jQuery参数更改 styles 来获得相同的效果

 $(function() { $('#a').hover(function() { $('#rectangle').css('right', '0em'); }, function() { $('#rectangle').css('right', ''); }); $('#b').hover(function() { $('#rectangle').css('right', '5em'); }, function() { $('#rectangle').css('right', ''); }); $('#c').hover(function() { $('#rectangle').css('right', '10em'); }, function() { $('#rectangle').css('right', ''); }); $('#d').hover(function() { $('#rectangle').css('right', '15em'); }, function() { $('#rectangle').css('right', ''); }); $('#e').hover(function() { $('#rectangle').css('right', '20em'); }, function() { $('#rectangle').css('right', ''); }); });
 #rectangle { background-color: powderblue; width: 10em; height: 2em; margin: 0 auto; position: absolute; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="navigation"> <li><a href="#" id="a">Entry 1</a></li> <li><a href="#" id="b">Entry 2</a></li> <li><a href="#" id="c">Entry 3</a></li> <li><a href="#" id="d">Entry 4</a></li> <li><a href="#" id="e">Entry 5</a></li> </ul> <div id="rectangle"></div>

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

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