为什么这适用于Safari和Firefox,而不是Chrome
Why is this working on Safari and Firefox but not on Chrome?
我有一个响应式导航菜单,它的工作原理如下:当您调整窗口大小时,"汉堡包"(三行)会出现一个图标。单击此图标将显示菜单,并且该图标通过变换变为"X"图标。单击"X"使菜单消失,图标再次变为三行。
它在Safari和Firefox中完美运行,但在Chrome中则不然。它将三行转换为"X"和viceversa,但菜单从未出现。为什么?
代码如下:
.HTML:
<nav>
<label for="show-menu" class="show-menu">
<button class="show-menu button-toggle-navigation">
<span>Toggle Navigation</span>
</button>
</label>
<input type="checkbox" id="show-menu" role="button">
<ul>
<li><a href="about.html">Conóceme</a></li>
<li><a href="servicios.html">Servicios</a></li>
<li><a href="port.html">Portfolio</a></li>
<li><a href="contacto.html">Contacto</a></li>
</ul>
</nav>
[...]
<script type="text/javascript">
$('button').on('click', function() {
$(this).toggleClass('isActive');
});
</script>
.CSS:
/*Style 'show menu' label button and hide it by default*/
.show-menu {
float: right;
width: 0;
height: 0;
text-align: right;
display: none;
margin-right: 15%;
}
/*Hide checkbox*/
input[type=checkbox]{
display: none;
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ ul{
border-top-color: black;
float: right;
text-align: center;
display: block;
padding-top: 15%;
}
.button-toggle-navigation {
background-color: transparent;
border: 0;
border-bottom: 0.25em solid black;
border-top: 0.25em solid black;
font-size: 13px;
cursor: pointer;
height: 1.5em;
margin: .75em .375em;
outline: 0;
position: relative;
-webkit-transition: border-color 150ms ease-out, -webkit-transform 150ms ease-out;
transition: border-color 150ms ease-out, transform 150ms ease-out;
width: 2.25em;
}
.button-toggle-navigation::after, .button-toggle-navigation::before {
border-bottom: 0.25em solid black;
bottom: .375em;
content: '';
height: 0;
left: 0;
position: absolute;
right: 0;
-webkit-transition: -webkit-transform 200ms ease-out;
transition: transform 200ms ease-out;
}
.button-toggle-navigation span {
color: transparent;
height: 0;
width: 0;
overflow: hidden;
position: absolute;
}
.isActive {
border-color: transparent;
-webkit-transform: rotateZ(90deg);
transform: rotateZ(90deg);
}
.isActive::after, .isActive::before {
-webkit-transition: -webkit-transform 200ms ease-out;
transition: transform 200ms ease-out;
}
.isActive::after {
-webkit-transform: rotateZ(45deg);
transform: rotateZ(45deg);
}
.isActive::before {
-webkit-transform: rotateZ(-45deg);
transform: rotateZ(-45deg);
}
非常感谢您的帮助!
PS:如果你能告诉我一个更好的方法来做这个响应式菜单,我将不胜感激!谢谢!:)
当然!
这就是我解决问题的方式:
.HTML
<nav>
<label for="show-menu"xs class="show-menu">
<button id="pull" class="show-menu button-toggle-navigation"></button>
</label>
<ul>
<li><a href="about.html">Conóceme</a></li>
<li><a href="servicios.html">Servicios</a></li>
<li><a href="port.html">Portfolio</a></li>
<li><a href="contacto.html">Contacto</a></li>
</ul>
</nav>
.JS
<script type="text/javascript">
var menu = $("nav ul");
$('#pull').on('click', function() {
$(this).toggleClass('isActive');
menu.slideToggle(200);
});
</script>
.CSS
/*Style 'show menu' label button and hide it by default*/
.show-menu {
margin-top: 7%;
float: right;
display: block;
}
.button-toggle-navigation {
background-color: transparent;
border: 0;
border-bottom: 0.16em solid black;
border-top: 0.16em solid black;
font-size: 1em;
cursor: pointer;
height: 1.2em;
margin: .75em .375em;
outline: 0;
position: relative;
-webkit-transition: border-color 150ms ease-out, -webkit-transform 150ms ease-out;
transition: border-color 150ms ease-out, transform 150ms ease-out;
width: 2.25em;
}
.button-toggle-navigation::after, .button-toggle-navigation::before {
border-bottom: 0.16em solid black;
bottom: .375em;
content: '';
height: 0;
left: 0;
position: absolute;
right: 0;
-webkit-transition: -webkit-transform 200ms ease-out;
transition: transform 200ms ease-out;
}
.isActive {
border-color: transparent;
-webkit-transform: rotateZ(90deg);
transform: rotateZ(90deg);
}
.isActive::after, .isActive::before {
-webkit-transition: -webkit-transform 200ms ease-out;
transition: transform 200ms ease-out;
}
.isActive::after {
-webkit-transform: rotateZ(45deg);
transform: rotateZ(45deg);
}
.isActive::before {
-webkit-transform: rotateZ(-45deg);
transform: rotateZ(-45deg);
}
如果你不用 CSS 隐藏你的按钮,代码运行良好:
.show-menu {
float: right;
text-align: right;
margin-right: 15%;
}
http://jsfiddle.net/4towu13r/
如果使用-webkit-
、-moz
、-o-
过渡前缀,则更好。
更新:
说明:不知何故,Chrome无法激活<label>
内的<button>
,如果您仅单击<label>
则选中复选框并完成工作。以下是完成这项工作的jQuery解决方法:
checkbox=$('input[role=button]');
checkbox.prop('checked', !checkbox.prop('checked'));
工作代码:http://jsfiddle.net/eapo/4towu13r/3/
相关文章:
- XMLHttpRequest - difference between Chrome & Firefox
- 长时间运行的脚本IE与Chrome/Firefox
- Javascript语法在Safari/Webkit中无效,在Chrome/Firefox中运行良好
- 代码在 Dreamweaver cs6 中工作,但在 chrome/firefox 中不起作用
- 在调试客户端时使用Chrome / Firefox
- Chrome/Firefox 使事件侦听器断点在停止之前跳过 jQuery 和其他库
- Chrome/Firefox 一次在所有 JavaScript onclick 事件上设置断点
- 未捕获的类型错误:无法读取IE和Chrome Firefox中未定义的属性“文档”很好
- 2D纹理渲染在Safari中闪烁,在Chrome / Firefox中很好
- 砌体JS插件 - Chrome / Firefox 问题
- Chrome/Firefox 中的 Rect 函数有什么用
- 在Chrome/Firefox中全屏按转义时未触发键下达事件
- 在PhantomJS中需要JS错误,但不是Chrome / FireFox
- Ruby Time to JavaScript Date in Chrome/Firefox
- 通过Google Chrome/Firefox Addon中的控制台访问/操作内容脚本
- 如何在Chrome/Firefox中导出ES6中的变量
- 在调用 window.location.asassigned 后,网页渲染在 chrome/firefox 之间的行为不
- 在将静态资源切换到CDN后,谷歌在Chrome+Firefox中映射错误,在Safari中很好:
- 粗体在Chrome/Firefox中太过粗体
- Chrome/Firefox中双美元符号选择器查询功能的来源是什么