悬停后div项目消失

Posted

技术标签:

【中文标题】悬停后div项目消失【英文标题】:div Items disappears after hovering 【发布时间】:2021-06-25 04:32:53 【问题描述】:

我正在尝试使用 css 在侧栏导航栏上创建嵌套链接。当我将鼠标悬停在主链接上时,会显示子链接。但是当我将鼠标悬停在子链接上时,它的子链接变得可见,但我无法单击/悬停在它们上,因为它们正在消失。

注意:我对 SCSS/CSS 及其 react 应用非常陌生,我正在尝试添加一些样式。

代码沙盒:https://g0mbs.csb.app/

HTML 页面

import "./styles/style.css";

function App() 
  return (
    <>

      <header>
        <nav>
          <div className="logo">
            <h1>Logo</h1>
          </div>
          <div className="nav__links" >
            <ul >
              <li><a>Link1</a></li>
              <li><a>Link2</a></li>
              <li><a>Link3</a></li>
            </ul>
          </div>

          <div className="burger">
            <button>
              <div className="line1"></div>
              <div className="line2"></div>
              <div className="line3"></div>
            </button>
          </div>

        </nav>
      </header>
      <div className="main-content-with-side-bar">
        <div className="side-bar">
          <ul>
            <li>
              <div>
                <a>Hello1</a>
                <div className="subMenu">
                  <ul>
                    <li><a>link1</a></li>
                    <li><a>link2</a></li>
                    <li><a>link3</a></li>
                    <li><a>link4</a></li>
                  </ul>
                </div>
              </div>
            </li>
            <li>
              <div>
                <a>Hello1</a>
                <div className="subMenu">
                  <ul>
                    <li><a>link1</a></li>
                    <li><a>link2</a></li>
                    <li><a>link3</a></li>
                    <li><a>link4</a></li>
                  </ul>
                </div>
              </div>
            </li>
            <li>
              <div>
                <a>Hello1</a>
                <div className="subMenu">
                  <ul>
                    <li><a>link5</a></li>
                    <li><a>link6</a></li>
                    <li><a>link7</a></li>
                    <li><a>link8</a></li>
                  </ul>
                </div>
              </div>
            </li>
            <li>
              <div>
                <a>Hello1</a>
                <div className="subMenu">
                  <ul>
                    <li><a>link9</a></li>
                    <li><a>link10</a></li>
                    <li><a>link11</a></li>
                    <li><a>link12</a></li>
                  </ul>
                </div>
              </div>
            </li>
            <li>
              <div>
                <a>Hello1</a>
                <div className="subMenu">
                  <ul>
                    <li>
                    <a>link13</a>
                      <div className="sub-subMenu">
                        <ul>
                          <li><a>link13.1</a></li>
                          <li><a>link13.2</a></li>
                          <li><a>link13.3</a></li>
                          <li><a>link13.4</a></li>
                        </ul>
                      </div>
                    </li>
                    <li><a>link14</a>
                    <div className="sub-subMenu">
                        <ul>
                          <li><a>link14.1</a></li>
                          <li><a>link14.2</a></li>
                          <li><a>link14.3</a></li>
                          <li><a>link14.4</a></li>
                        </ul>
                      </div>
                    </li>
                    <li><a>link15</a>
                    <div className="sub-subMenu">
                        <ul>
                          <li><a>link15.1</a></li>
                          <li><a>link15.2</a></li>
                          <li><a>link15.3</a></li>
                          <li><a>link15.4</a></li>
                        </ul>
                      </div></li>
                    <li><a>link16</a>
                    <div className="sub-subMenu">
                        <ul>
                          <li><a>link16.1</a></li>
                          <li><a>link16.2</a></li>
                          <li><a>link16.3</a></li>
                          <li><a>link16.4</a></li>
                        </ul>
                      </div></li>
                  </ul>
                </div>
              </div>
            </li>
          </ul>


        </div>
        <div>

          <p>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

            Why do we use it?
            It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).


            Where does it come from?
            Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

            The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
</p>

        </div>
      </div>


      <footer>
        <div>

        </div>
      </footer>
    </>
  );


export default App;

SCSS

@import "variable";

* 
  margin: 0;
  padding: 0;
  box-sizing: border-box;

header 
  position: sticky;
  & nav 
    position: sticky;
    background-color: $header-back-color;
    display: flex;
    align-items: center;
    justify-content: space-around;
    min-height: 8vh;

    & button 
      background-color: $header-back-color;
    

    & .nav__links 
      display: block;
      text-decoration: none;
      width: auto;
      height: auto;
      overflow: initial;
      & ul 
        display: flex;
        justify-content: space-around;
        width: 100%;
        float: left;
        overflow: hidden;
      
      & li 
        list-style: none;
      

      li a 
        display: flex;
        justify-content: space-around;
        // align-items: center;
        color: black;
        letter-spacing: 2px;
        font-weight: bold;
        font-size: 14px;
      
    

    & .logo 
      letter-spacing: 2px;
      text-decoration: none;
      text-transform: uppercase;
      font-size: 20px;
      float: left;
    

    & .burger 
      & div 
        width: 25px;
        height: 3px;
        background-color: rgb(3, 3, 3);
        margin: 3px;
      
      & button 
        display: none;
      
    
  


.sideNav 
  & ul 
    min-width: 40px;
  
  & ul li 
    display: flex;
    flex-direction: column;
  


.main-content-with-side-bar 
  display: flex;
  width: fit-content;

  & .side-bar 
    width: fit-content;
    min-width: 180px;
    margin: 0 20px;
    background-color: white;
    border-style: solid;

    & li:hover .subMenu
       
        display: inline-block;
        position: absolute;
    
    & .subMenu li:hover .sub-subMenu
        display: inline-block;
        position: absolute;
    
  
  & .subMenu 
    display: none;
    visibility: hidden;
    margin-left: 20px;
    z-index: 10;
    // opacity: 0.5;
    
  

  & .sub-subMenu
    display: none;
    visibility: hidden;
    margin-left: 20px;
    z-index: 10;
  

  & .side-bar ul li 
    display: flex;
    flex-direction: column;
  



@media screen and (max-width: 768px) 
  nav 
    & .nav__links 
      display: none;
      justify-content: space-around;
      width: 30%;
    
    & .burger 
      & button 
        display: block;
      
    
  

【问题讨论】:

嘿,请尝试添加一个工作示例,可以使用 codesandbox 相同 @Codenewbie 我现在已经添加了。你可以找到上面的链接。 【参考方案1】:

在不运行代码的情况下,仅根据我的经验,尝试缩短两个元素之间的边距。当您将鼠标悬停在焦点元素上时,在“停用”悬停的位置之间可能会有空间。确保您也有悬停并关注其他元素。或者在悬停的元素上使用 setTimeOut() 让自己有时间从一个移动到下一个。前一种建议是最简单的。

【讨论】:

【参考方案2】:

消失的原因是鼠标超出了悬停区域。所以只需增加悬停区域即可。

在你的styles.css中添加这段代码

ul li :hover::after 
  content: " ";
  width: 100px;
  height: 20px;
  position: absolute;

代码沙盒:https://codesandbox.io/s/naughty-gates-qtgyb?file=/src/styles.css

【讨论】:

【参考方案3】:

当您尝试单击子链接时,子链接的子链接就会消失,因为您为 .sub-submenu 提供了左边距。尝试减少它,并增加子链接的宽度,就可以了。

【讨论】:

以上是关于悬停后div项目消失的主要内容,如果未能解决你的问题,请参考以下文章

jQuery 悬停效果:一个 div 消失,另一个 div 出现

如何让 div 在悬停时消失而不会在鼠标移动时闪烁?

下拉菜单在悬停时消失(Firefox)

为啥当鼠标悬停后出现边框时 <div> 的大小会增加? CSS [重复]

JavaScript 通过悬停另一个项目来切换显示/隐藏div

点击后jquery删除悬停?