简体   繁体   English

使用CSS将鼠标悬停在下拉菜单上

[英]Dropdown menu on mouse hover with CSS

I have a very annoying problem, and it is bugging me for some time now, and I've tried everything to fix it, but I simply fail at doing so. 我有一个非常烦人的问题,现在已经困扰了我一段时间,并且我已尽一切努力对其进行了修复,但我只是失败了。

The problem is: I have a navigation which consists of many <div> elements inside <div> , which is inside another <div> and so on. 问题是:我有由许多的导航<div>内的元素<div>这是在另一个内部<div>等。

Here is the code for my navigation: 这是我的导航代码:

    <div class="nav-laptop"><a href="proizvodi.php?upit=SELECT Slika, Naziv, Opis, Cijena FROM Proizvodi WHERE Kategorija='Laptop' ORDER BY Proizvodac Asc;">Laptop</a><br />

  <div class="nav-bot"><a href="proizvodi.php?upit=SELECT Slika, Naziv, Opis, Cijena FROM Proizvodi WHERE Kategorija='Laptop' AND Proizvodac='Acer' ORDER BY Proizvodac ASC;">Acer</a></div>
  <br />
  <div class="nav-bot"><a href="proizvodi.php?upit=SELECT Slika, Naziv, Opis, Cijena FROM Proizvodi WHERE Kategorija='Laptop' AND Proizvodac='Apple' ORDER BY Proizvodac ASC;">Apple</a></div>
  <br />
  <div class="nav-bot"><a href="proizvodi.php?upit=SELECT Slika, Naziv, Opis, Cijena FROM Proizvodi WHERE Kategorija='Laptop' AND Proizvodac='Asus' ORDER BY Proizvodac ASC;">Asus</a></div>
  <br />
  <div class="nav-bot"><a href="proizvodi.php?upit=SELECT Slika, Naziv, Opis, Cijena FROM Proizvodi WHERE Kategorija='Laptop' AND Proizvodac='Dell' ORDER BY Proizvodac ASC;">Dell</a></div>
  <br />
  <div class="nav-bot"><a href="proizvodi.php?upit=SELECT Slika, Naziv, Opis, Cijena FROM Proizvodi WHERE Kategorija='Laptop' AND Proizvodac='HP' ORDER BY Proizvodac ASC;">HP</a></div>
  <br />
  <div class="nav-bot"><a href="proizvodi.php?upit=SELECT Slika, Naziv, Opis, Cijena FROM Proizvodi WHERE Kategorija='Laptop' AND Proizvodac='Lenovo' ORDER BY Proizvodac ASC;">Lenovo</a></div>
  <br />
  <div class="nav-bot"><a href="proizvodi.php?upit=SELECT Slika, Naziv, Opis, Cijena FROM Proizvodi WHERE Kategorija='Laptop' AND Proizvodac='Toshiba' ORDER BY Proizvodac ASC;">Toshiba</a></div>
</div>

I will attach a picture to give you a better view on what I have: 我将附上一张图片,以使您更好地了解自己所拥有的: 在这里,您看到的第一个下拉导航是上面的代码,所有其他代码完全相同,只包含更多数据。下拉列表使用CSS3进行动画处理。

Now here is my CSS for those navigation <div> s: 现在这是这些导航<div>的CSS:

    .nav-laptop, .nav-pc, .nav-tablet, .nav-periferije, .nav-dijelovi, .nav-audio, .nav-mobitel, .nav-monitor, .nav-televizor, .nav-usluge {
    transition: height 0.5s;
    -moz-transition: height 0.5s;
    -ms-transition: height 0.5s;
    -o-transition: height 0.5s;
    -webkit-transition: height 0.5s;
    height: 20px;
    width: 72px;
    border-radius: 8px 8px 0px 0px;
    -moz-border-radius: 8px 8px 0px 0px;
    -ms-border-radius: 8px 8px 0px 0px;
    -o-border-radius: 8px 8px 0px 0px;
    -webkit-border-radius: 8px 8px 0px 0px;
    float: left;
    text-align: center;
    vertical-align: middle;
    border-top-width: 2px;
    border-top-style: solid;
    border-top-color: #F00;
    font-size: 1.05em;
    margin-top: 6px;
    overflow: hidden;
}

And here is :hover version for laptop nav only: 这是:hover版本,仅适用于笔记本电脑导航:

    .nav-laptop:hover {
    height: 186px;
    width: 72px;
    border-radius: 8px 8px 0px 0px;
    -moz-border-radius: 8px 8px 0px 0px;
    -ms-border-radius: 8px 8px 0px 0px;
    -o-border-radius: 8px 8px 0px 0px;
    -webkit-border-radius: 8px 8px 0px 0px;
    float: left;
    text-align: center;
    vertical-align: middle;
    font-size: 1.05em;
    margin-top: 6px;
}

Now this all navigation works awesome and beautiful and everything, but the problem is, whenever I hover with my mouse over any category, when the division drops down, it distorts all the content below, and makes a huge mess, and once I move my mouse, it all gets back to normal. 现在,所有的导航都很棒,很漂亮,而且一切正常,但是问题是,每当我将鼠标悬停在任何类别上时,该分区下降时,它就会扭曲下面的所有内容,并造成巨大的混乱,并且一旦我移动了鼠标,一切恢复正常。 How should I go about it? 我应该怎么做? This may be very inefficient way to do it, and I'm open to all suggestions, and looking forward to learning more from you people. 这可能是一种非常低效的方式,我愿意接受所有建议,并希望向大家学习更多。

Here's a normal content: 这是正常的内容: 在此处输入图片说明

And here's distorted content due to hovering onto navigation: 由于悬停在导航上,因此内容失真: 在此处输入图片说明

You are floating the "dropdown" parts of the menu. 您正在浮动菜单的“下拉”部分。 And if all the rest of the content are also floating, all the things are in the same flow. 并且,如果所有其他内容也都在浮动,则所有内容都将在同一流中。 Thus they get all "weird", even though this is not at all weird but rather exactly the expected behavior. 这样,他们就变得“怪异”,即使这根本不是怪异的,而恰恰是预期的行为。

Instead, you should be using absolute positioning for the parts that you want to overlay on the other content. 相反,您应该对要覆盖在其他内容上的零件使用绝对定位。 Absolute positioning removes the element from the document flow, and thus is does not affect the layout of anything else other than itself. 绝对定位会将元素从文档流中删除,因此不会影响除自身以外的其他任何元素的布局。

So, make the "dropdown" parts position: absolute and the main menu items position: relative , for example. 因此,例如,将“下拉”部分的position: absolute ,将主菜单项的position: relative This way the "dropdown" parts will get positioned relative to the main menu items. 这样,“下拉”部分将相对于主菜单项定位。

Take a look at eg http://www.w3schools.com/css/css_positioning.asp or http://learnlayout.com for basic CSS layout tutorials. 看一下http://www.w3schools.com/css/css_positioning.asphttp://learnlayout.com以获得基本的CSS布局教程。

Simple example of a hover menu: 悬停菜单的简单示例:

 ul, li { list-style: none; padding: 0; margin: 0; } li { display: inline-block; width: 100px; background: #ddd; } ul li { position: relative; } li ul { position: absolute; display: none; } li:hover ul { display: block; } li ul li { background: #eee } 
 <ul> <li> <span>First</span> <ul> <li>First</li> <li>Second</li> </ul> </li> <li> <span>Second</span> <ul> <li>First</li> <li>Second</li> </ul> </li> </ul> <div class="content"> <p>Content content content content content content content<br>content content content content content content...</p> <p>Content content content content content content content<br>content content content content content content...</p> </div> 

I would play around with position:absolute; 我会position:absolute; and z-index . z-index

A normal way to do this is to use <ul> lists. 通常的方法是使用<ul>列表。

<ul>
    <li>
        <a href="#">Hover</a>
        <ul>
            <li>asdasd</li>
        </ul>
    </li>
</ul>

css: CSS:

ul li:hover ul{display:block;}
/* or ul li a + ul{display:block} */

ul li > a{position:relative;}
ul ul{display:absolute;display:none;}

Offtopic: 无关:

proizvodi.php?upit=SELECT Slika, Naziv, Opis, Cijena FROM Proizvodi WHERE Kategorija='Laptop' AND Proizvodac='Toshiba' ORDER BY Proizvodac ASC;

Are you allowing a script to execute sql through a GET parameter? 您是否允许脚本通过GET参数执行sql? :O :O

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

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