简体   繁体   English

如何使用此按钮菜单解决跨浏览器显示的问题?

[英]How do i tackle cross-browser display problems with this button menu?

I set up a menu that uses buttons with links, ul's, and li's inside them. 我设置了一个菜单,在其中使用带有链接,ul和li的按钮。 It works fine in Chrome, Android, Safari, and Opera. 它可以在Chrome,Android,Safari和Opera中正常运行。 In Firefox, when the ul's appear the nav jumps down. 在Firefox中,当ul出现时,导航会跳下来。 In IE, the ul's don't display. 在IE中,不显示ul。 In both, the links don't appear. 在这两种情况下,链接都不会出现。

Edit: I chose to do this with buttons because i thought it gave me flexibility a regular ul menu wouldn't - background images, images inside them, attaching javascript events. 编辑:我选择使用按钮来执行此操作,因为我认为它可以给我提供常规ul菜单所没有的灵活性-背景图像,其中的图像以及附加的javascript事件。 It also of course creates a layout that is a row of buttons without any extra styling. 当然,它还创建了一个布局,该布局是一排按钮,没有任何额外的样式。

http://codepen.io/briligg/pen/emwXaw?editors=110 http://codepen.io/briligg/pen/emwXaw?editors=110

nav {       position: fixed;
            top: 0px;
            right: 0px;
            width: 70%;
            float: right;
            padding: 2%;
            height: 34px;
            max-height: 34px;
            margin: 5px 0;
            }
nav button {
            border: 1px solid #666666;
            border-radius: 10px;
            background-color: #3b4c6d;
            color: white;
            padding: 0 4px;
            height: 32px;
            font: 16px;
}

nav button ul {
            position: relative;
            display: none;
}
nav button:hover ul, nav button:focus ul {
            display: block;
            z-index: 7;
            list-style: none;
            background-color: #3b4c6d;
            border: 1px solid #666666;
            border-radius: 10px;
      margin-top: 9px;
            padding: 6px 2px;
}
nav button:hover li, nav button:focus li {
            padding: 8px 2px;
}
nav a {
        text-decoration: none;
        color: white;
}
nav a:hover, nav a:focus {
            color: #52cbff;
}

Then in the html, the ul's are nested in the buttons, with links, like this: 然后在html中,将ul嵌套在带有链接的按钮中,如下所示:

<button tabindex="4"><a href="beingthere.html">Being There</a>
<ul tabindex="5">
        <li><a href="beingthere.html#domination">World Domination</a></li>
        <li><a href="beingthere.html#chickens">Chickens</a></li>
        <li><a href="beingthere.html#gravity">Down with Gravity</a></li>
        <li><a href="beingthere.html#moonstar">The Moonstar</a></li>
        </ul>
</button>

In even creating this thing i was already at the limits of my knowledge. 即使创建这个东西,我也已经掌握了知识。 I don't know how to go about finding work-arounds, or if that is even possible in this case. 我不知道如何找到解决方法,或者在这种情况下是否可行。 Help with even knowing where to go to figure this out would be appreciated, never mind an actual solution to the problem. 甚至知道在哪里都可以找到解决方法的帮助将不胜感激,不用管解决问题的实际方法。 I've been looking for information and haven't found any. 我一直在寻找信息,但没有找到任何信息。

IE has button {overflow:hidden;} style by default, You can rest that as follows. IE默认具有button {overflow:hidden;}样式,您可以按照以下方式休息。

nav button {
    overflow: visible;
}

Edit: In order to get the links working we'll have to redo the markup, I also adjusted the CSS for the HTML changes. 编辑:为了使链接正常工作,我们必须重做标记,我还针对HTML更改调整了CSS。 see the following code snippet. 请参阅以下代码段。

 nav { position: fixed; top: 0px; right: 0px; width: 70%; float: right; padding: 2%; height: 34px; max-height: 34px; margin: 5px 0; white-space: nowrap; } nav > ul > li { display: inline-block; position: relative; font-size: 16px; height: 32px; line-height: 32px; border: 1px solid #666666; border-radius: 10px; background-color: #3b4c6d; color: white; padding: 0 4px; } nav > ul > li > ul { display: none; list-style: none; background-color: #3b4c6d; border: 1px solid #666666; border-radius: 10px; padding: 6px; position: absolute; z-index: 7; top: 32px; left: 0; } nav > ul > li:hover > ul { display: block; } nav a { text-decoration: none; color: white; } nav a:hover { color: #52cbff; } 
 <nav> <ul> <li tabindex="1"><a href="futuremoon.html#begin">Purpose</a></li> <li tabindex="2"> <a href="moonvsmars.html">Moon vs Mars</a> <ul tabindex="3"> <li><a href="moonvsmars.html#ambiance">Ambiance</a></li> <li><a href="moonvsmars.html#communication">Communication</a></li> <li><a href="thereandback.html">There and Back</a></li> </ul> </li> <li tabindex="4"> <a href="beingthere.html">Being There</a> <ul tabindex="5"> <li><a href="beingthere.html#domination">World Domination</a></li> <li><a href="beingthere.html#chickens">Chickens</a></li> <li><a href="beingthere.html#gravity">Down with Gravity</a></li> <li><a href="beingthere.html#moonstar">The Moonstar</a></li> </ul> </li> </ul> </nav> 

The problem must be caused by this Link inside a button not working in Firefox (and IE). 该问题可能是由于在Firefox (和IE)中无法使用的按钮内的链接引起的。

Full Demo: http://codepen.io/anon/pen/KwOqKv 完整演示: http : //codepen.io/anon/pen/KwOqKv

Instead of putting <a> in <button> , put all <a> inside <li> . 不要将<a>放在<button> ,而是将所有<a>放在<li> Also, as you had, put the secondary links inside another <ul> in the <li> . 同样,如您所愿,将辅助链接放在<li>中的另一个<ul>内。

<ul class='primary-links'>
    <li class='primary'><a href='#'>Primary link</a></li>
    <li class='primary'>
        <a href='#'>Another primary link</a>
        <ul class='secondary-links'>
            <li class='secondary'><a href='#'>Secondary Link</a></li>
            <li class='secondary'><a href='#'>Another secondary link</a></li>
        </ul>
    </li>
</ul>

The primary links are display:inline-block in order for them to display horizontally while the secondary links are display:none to initially hide them. 主要链接为display:inline-block ,以便它们水平显示,而次要链接为display:none即可将其隐藏。 The secondary links become visible when the primary links are hovered over. 当主链接悬停时,辅助链接变为可见。 position:absolute removes the secondary links from the document flow preventing the primary links from jumping down when the secondary links become visible. position:absolute从文档流中删除辅助链接,从而防止在辅助链接可见时主链接跳下。

.primary {
    display: inline-block;
}

.secondary-links {
    display: none;
    position: absolute;
}

.primary:hover > .secondary-links {
    display: block;
}

 body { font: 1em/1.5 sans-serif; } a:link, a:visited { color: #08f; text-decoration: none; } a:hover, a:active, a:focus{ color: #f80; } ul { list-style: none; margin: 0; padding: .25em; border-radius: .25em; background: #fff; border: thin solid #ccc; box-shadow: 0 0 .25em #ccc; } li { margin: .5em; } nav > ul > li { display: inline-block; } li > ul { display: none; position: absolute; } li:hover > ul { display: block; } 
 <nav> <ul> <li><a href='#'>One</a></li> <li> <a href='#'>Two</a> <ul> <li><a href='#'>Two One</a></li> <li><a href='#'>Two Two</a></li> <li><a href='#'>Two Three</a></li> </ul> </li> <li> <a href='#'>Three</a> <ul> <li><a href='#'>Three One</a></li> <li><a href='#'>Three Two</a></li> <li><a href='#'>Three Three</a></li> <li><a href='#'>Three Four</a></li> </ul> </li> </ul> </nav> 

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

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