CSS 下拉菜单在 Chrome 按钮单击和悬停时向下滚动页面

CSS Dropdown Menu Scrolls Page Down In Chrome On Button Click and Hover

咆哮

所以我花了一个多小时在纯 css 下拉和关闭菜单上,我认为它非常漂亮,然后只有在完成所有样式并使其在我的网站上看起来不错之后,我才决定在 Chrome 中查看。哎呀

问题

为什么我的纯 css 下拉菜单使 Google 的 Chrome 在单击按钮时向下滚动页面?有没有一种简单的方法可以让它同时兼容 FF 和 Chrome?

提示

这与菜单是 position: fixed header 有关,但我不知道为什么 Firefox 的行为符合我的要求,而 Chrome 拉动 IE。

例子

这是 jsfiddle 上的精简示例

http://jsfiddle.net/Hastig/a77ewh89/

线索

代码

css

.header {
    position: fixed;    top: 20px;
    background-color: rgba(0,0,0,0.8);
}

.button {
    color: white;
    display: inline-block;
}

.menu {
    width: 500px; height: 0px;
    overflow: hidden;
    transition: all 1s;
    color: white;
}

#menu1:target, #menu2:target, #menu3:target {
    height: 300px;
}

.content {
    margin: 80px 0px 0px 0px;
    width: 500px;
    background-color: red;
}

.filler-divs {
    font-size: 40px;
    color: white;
}

html

<div class="header">
    <a class="button" id="button1" href="#menu1">Menu 1</a>
    <a class="button" id="button1" href="#menu2">Menu 2</a>
    <a class="button" id="button1" href="#menu3">Menu 3</a>

    <div class="menu" id="menu1">menu 1</div>
    <div class="menu" id="menu2">menu 2</div>
    <div class="menu" id="menu3">menu 3</div>
</div><!-- end header -->

<div class="content">
    <div class="filler-divs">filler filler filler filler filler filler filler filler filler filler</div>
    <div class="filler-divs">filler filler filler filler filler filler filler filler filler filler</div>
<!-- add lots more of these divs to fill the page with text so you can see it scroll down - scrolling stops at page bottom -->  
</div><!-- end content -->

没有必要放弃纯粹的 CSS 方法。请参阅下面的示例。解决方案是将所有内容放在一个容器元素中,该容器元素复制您的正文元素,但 Chrome 不会滚动浏览以尝试显示菜单元素。

新版本http://jsfiddle.net/scarl3tt/dm89g0m5/

A div.fake-body 必须包裹你的 .content class:

.fake-body {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 10px;
    overflow-y: auto;
}

...并且您的 .header 需要在 DOM 中在此之后移动或标记为 z-index: 1 以确保它显示在假体上方并且能够与之互动。