为什么我的菜单按钮在整个屏幕上都会触发?

huangapple go评论49阅读模式
英文:

Why is my menu button activating across the screen?

问题

我最近创建了一个带有左侧下拉菜单的网站。然而,每当我位于屏幕顶部,即下拉菜单应该出现在左侧的位置时,即使我位于屏幕的另一侧,下拉菜单也会激活。我原本期望它只在我悬停在按钮本身时才激活。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
   .dropbtn {
      background-color: rgb(191, 255, 181);
      padding: 0px;
      font-size: 45px;
      border: none;
      width: 100px;
      height: 100px;
    }

    .dropdown-content {
      display: none;
      position: absolute;
      background-color: #98CC90;
      min-width: 16px;
      border-radius: 5px;
      z-index: 0;
    }

    .dropdown {
    text-align: left;
    }

    .dropdown-content a {
      color: #182016;
      padding: 12px 12px;
      text-decoration: none;
      font-size: 35px;
       font-family: Arial, sans-serif;
      display: block;
    }

    .dropdown-content a:hover {background-color: #79A373;}

    .dropdown-content a:hover {border-radius: 5px;}

    .dropdown:hover .dropdown-content {display: block;}

    .dropdown:hover .dropbtn {background-color: rgb(191, 255, 181);}

<!-- language: lang-html -->

    <div class="dropdown">
      <button class="dropbtn"><div class="menu"><img src="https://www.freeiconspng.com/thumbs/menu-icon/menu-icon-6.png"/></div></button>
      
      <div class="dropdown-content">
        <a onclick="window.location.href='https://ieatlargefrogs.com/about-me.html';">About me</a>
        <a onclick="window.location.href='https://ieatlargefrogs.com/dnilist.html';">DNI List</a>
        <a onclick="window.location.href='https://ieatlargefrogs.com/website.html';">Website Stuff</a>
      </div>
    </div>`
<!-- end snippet -->
英文:

I recently created a website with a dropdown menu on the left side. However, whenever I'm at the top of the screen, where the dropdown would be on the left, the dropdown activates even when I'm on the other side of the screen. I was expecting it to only activate when I hover over the button itself.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

   .dropbtn {
  background-color: rgb(191, 255, 181);
  padding: 0px;
  font-size: 45px;
  border: none;
  width: 100px;
  height: 100px;
}


.dropdown-content {
  display: none;
  position: absolute;
  background-color: #98CC90;
  min-width: 16px;
  border-radius: 5px;
  z-index: 0;
}

.dropdown {
text-align: left;
}

.dropdown-content a {
  color: #182016;
  padding: 12px 12px;
  text-decoration: none;
  font-size: 35px;
   font-family: Arial, sans-serif;
  display: block;
}

.dropdown-content a:hover {background-color: #79A373;}

.dropdown-content a:hover {border-radius: 5px;}

.dropdown:hover .dropdown-content {display: block;}

.dropdown:hover .dropbtn {background-color: rgb(191, 255, 181);}

<!-- language: lang-html -->

&lt;div class=&quot;dropdown&quot;&gt;
  &lt;button class=&quot;dropbtn&quot;&gt;&lt;div class=&quot;menu&quot;&gt;&lt;img src=&quot;https://www.freeiconspng.com/thumbs/menu-icon/menu-icon-6.png&quot;/&gt;&lt;/div&gt;&lt;/button&gt;
  
  &lt;div class=&quot;dropdown-content&quot;&gt;
    &lt;a onclick=&quot;window.location.href=&#39;https://ieatlargefrogs.com/about-me.html&#39;;&quot;&gt;About me&lt;/a&gt;
    &lt;a onclick=&quot;window.location.href=&#39;https://ieatlargefrogs.com/dnilist.html&#39;;&quot;&gt;DNI List&lt;/a&gt;
    &lt;a onclick=&quot;window.location.href=&#39;https://ieatlargefrogs.com/website.html&#39;;&quot;&gt;Website Stuff&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;`

<!-- end snippet -->

答案1

得分: 0

Sure, here is the translated code portion:

.dropbtn {
  background-color: rgb(191, 255, 181);
  padding: 0px;
  font-size: 45px;
  border: none;
  width: 100px;
  height: 100px;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #98CC90;
  min-width: 16px;
  border-radius: 5px;
  z-index: 0;
}

.dropdown {
  text-align: left;
}

.dropdown-content a {
  color: #182016;
  padding: 12px 12px;
  text-decoration: none;
  font-size: 35px;
  font-family: Arial, sans-serif;
  display: inline-block;
}

.dropdown-content a:hover {
  background-color: #79A373;
}

.dropdown-content a:hover {
  border-radius: 5px;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.dropdown:hover .dropbtn {
  background-color: rgb(191, 255, 181);
}

Please note that this is the translated CSS code.

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.dropbtn {
  background-color: rgb(191, 255, 181);
  padding: 0px;
  font-size: 45px;
  border: none;
  width: 100px;
  height: 100px;
}


.dropdown-content {
  display: none;
  position: absolute;
  background-color: #98CC90;
  min-width: 16px;
  border-radius: 5px;
  z-index: 0;
}

.dropdown {
text-align: left;
}

.dropdown-content a {
  color: #182016;
  padding: 12px 12px;
  text-decoration: none;
  font-size: 35px;
   font-family: Arial, sans-serif;
  display: inline-block;
}

.dropdown-content a:hover {background-color: #79A373;}

.dropdown-content a:hover {border-radius: 5px;}

.dropdown:hover .dropdown-content {display: block;}

.dropdown:hover .dropbtn {background-color: rgb(191, 255, 181);}

<!-- end snippet -->

try setting the display of the a tags to inline block. The problem is that block display will stretch over the entire width of the container on default.

alternatively you can set a width to the dropdown-content

答案2

得分: 0

根据您在问题中提到的,您希望只有在鼠标悬停在按钮上时才显示dropdown-content。但是为了在悬停dropdown-content时仍然保持显示,必须修改您的代码。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
.dropbtn {
  background-color: rgb(191, 255, 181);
  padding: 0px;
  font-size: 45px;
  border: none;
  width: 100px;
  height: 100px;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #98CC90;
  min-width: 16px;
  border-radius: 5px;
  z-index: 0;
}

.dropdown {
text-align: left;
}

.dropdown-content a {
  color: #182016;
  padding: 12px 12px;
  text-decoration: none;
  font-size: 35px;
   font-family: Arial, sans-serif;
  display: block;
}

.dropdown-content a:hover {background-color: #79A373;}

.dropdown-content a:hover {border-radius: 5px;}

.dropbtn:hover + .dropdown-content, .dropdown-content:hover {display: block;}

.dropdown:hover .dropbtn {background-color: rgb(191, 255, 181);}

<!-- language: lang-html -->
<div class="dropdown">
  <button class="dropbtn"><div class="menu"><img src="https://www.freeiconspng.com/thumbs/menu-icon/menu-icon-6.png"/></div></button>
  
  <div class="dropdown-content">
    <a onclick="window.location.href='https://ieatlargefrogs.com/about-me.html';">关于我</a>
    <a onclick="window.location.href='https://ieatlargefrogs.com/dnilist.html';">DNI 列表</a>
    <a onclick="window.location.href='https://ieatlargefrogs.com/website.html';">网站内容</a>
  </div>
</div>

请确保dropbtndropdown-content之间始终存在位置上的连续性。否则,将无法悬停在dropdown-content上。

英文:

As you have mentioned in the question, you want the dropdown-content to be displayed only when the button is hovered. But for hovering the dropdown-content, it has to remain displayed even when dropdown-content is hovered.
So I have modified your code.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.dropbtn {
  background-color: rgb(191, 255, 181);
  padding: 0px;
  font-size: 45px;
  border: none;
  width: 100px;
  height: 100px;
}


.dropdown-content {
  display: none;
  position: absolute;
  background-color: #98CC90;
  min-width: 16px;
  border-radius: 5px;
  z-index: 0;
}

.dropdown {
text-align: left;
}

.dropdown-content a {
  color: #182016;
  padding: 12px 12px;
  text-decoration: none;
  font-size: 35px;
   font-family: Arial, sans-serif;
  display: block;
}

.dropdown-content a:hover {background-color: #79A373;}

.dropdown-content a:hover {border-radius: 5px;}

.dropbtn:hover + .dropdown-content, .dropdown-content:hover {display: block;}

.dropdown:hover .dropbtn {background-color: rgb(191, 255, 181);}

<!-- language: lang-html -->

&lt;div class=&quot;dropdown&quot;&gt;
  &lt;button class=&quot;dropbtn&quot;&gt;&lt;div class=&quot;menu&quot;&gt;&lt;img src=&quot;https://www.freeiconspng.com/thumbs/menu-icon/menu-icon-6.png&quot;/&gt;&lt;/div&gt;&lt;/button&gt;
  
  &lt;div class=&quot;dropdown-content&quot;&gt;
    &lt;a onclick=&quot;window.location.href=&#39;https://ieatlargefrogs.com/about-me.html&#39;;&quot;&gt;About me&lt;/a&gt;
    &lt;a onclick=&quot;window.location.href=&#39;https://ieatlargefrogs.com/dnilist.html&#39;;&quot;&gt;DNI List&lt;/a&gt;
    &lt;a onclick=&quot;window.location.href=&#39;https://ieatlargefrogs.com/website.html&#39;;&quot;&gt;Website Stuff&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;`

<!-- end snippet -->

Please ensure that there is always positional continuity between dropbtn and dropdown-content. Otherwise, it will not be possible to hover the dropdown-content.

huangapple
  • 本文由 发表于 2023年5月17日 21:09:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定