html - 悬停不能按预期工作

标签 html css hover

问题是,当我悬停在 li 上时,悬停不起作用,第一个有效,但悬停区域与 div 的大小不同。

这是一个没有 javascript 的通过复选框工作的侧边栏。

这是HTML

 <input type="checkbox" id="sidebartoggler">

<div id="page-wrap">
  <label for="sidebartoggler" id="toggle">☰</label>
<div id="sidebar">
    <ul class="elements">
      <li>Str. Glowna</li>
      <li>Wyb. przedmiotu</li>
    </ul>
</div>

<div class="page-content">

和CSS

.page-content
{
  position: relative;
  z-index: 0;
  transition: padding-left 0.5s;
}
#chose
{
  font-size: 40px;
  text-align: center;
  margin: 5% 0 5% 0;
}
#sidebar
{
  position: fixed;
  left:-190px;
  right: 0;
  top: 0px;
  bottom:0px;
  width: 170px;
  padding: 10px;
  background: #333;
  transition: left 0.5s;
  display: inline-block;

}
#toggle
{
  position: fixed;
  left:20px;
  top: 15px;
  font-size: 30px;
  transition: left 0.5s;
}
#sidebartoggler
{
  display: none;

}
#sidebartoggler:checked + #page-wrap #sidebar
{
  left:0px;

}
#sidebartoggler:checked + #page-wrap #toggle
{
  left:200px;

}
#sidebartoggler:checked + #page-wrap .page-content
{
  padding-left: 180px;

}
.page-content
{
  padding-left: 0px;
}
#sidebar ul
{
  font-size: 15px;
  list-style: none;
  list-style-position: inside;
  width: 170px;
  margin: 20px 0 0 -40px;
  text-align: center;

}
#sidebar ul > li
{
  padding: 10px 0;
  margin: 5px 0 0 0;
  background-color: #444;
  display: block;
}
#sidebar ul > li:hover
{

  background-color: #555
}

在这里你可以看到完整的代码:https://jsfiddle.net/2hnLyjw4/1/ 对不起我的英语不好。 感谢您的帮助!

最佳答案

问题是您在 .page-content 上使用了 padding-left;这会导致内容根据需要向右移动,但 div 部分位于菜单顶部并干扰悬停事件。

将其更改为使用 margin-left:

.page-content {
  position: relative;
  z-index: 0;
  transition: margin-left 0.5s;
}

...

#sidebartoggler:checked + #page-wrap .page-content {
  margin-left: 180px;
}

.page-content {
  margin-left: 0px;
}

关于html - 悬停不能按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36989185/

相关文章:

html - 如何在 HTML 5 中像这样在 CSS 中制作 2 级滚动选项卡

html - Wordpress Yeloni 退出弹出式订阅按钮出现宽度不正确

css - 从包装元素的最后一行删除边距底部

css - 如何在菜单中创建随文本长度扩展的悬停图像?

javascript - 延迟()或停止()超时?

html - 如何将css应用到div类中的li来显示 block ?

html - JSP 中的单选按钮验证

javascript异步更新一个div

jquery - 简单的 CSS/HTML 菜单链接不起作用

html - Blogger 中流行的帖子小部件的悬停效果