我想通过单击菜单项之一来创建一个具有js功能的下拉菜单。我试图通过下面的功能来实现这一点,但是它不起作用。你能告诉我我的错误在哪里吗?我想使用纯JavaScript,如果它更改了某些内容,我正在使用SASS。
代码如下:
$string = "{Test1|Test2} {Test3|Test4}";
$string = str_replace('{', '', $string);
$arr = array_filter(explode('}', $string));
foreach($arr as $item){
$item = explode('|', $item);
$result[trim($item[0])] = trim($item[1]);
}
echo '<pre>';print_r($result);echo '</pre>';
let home = document.getElementById('home');
let underhome = document.querySelector('under-home');
home.addEventListener('click', function() {
underhome.classList.toggle('open');
});
.navbar .slider .hammburger-links {
padding: 0 1.250em;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.navbar .slider .hammburger-links a {
padding: 0 1.500em;
text-decoration: none;
font-family: "Helvetica", Arial;
font-size: 11px;
color: #a6adb4;
}
.navbar .slider .hammburger-links .under-home {
position: absolute;
top: 5%;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
background: #F6F8F9;
min-width: 12.5em;
min-height: 12.5em;
z-index: 1;
display: none;
}
.navbar .slider .hammburger-links .under-home a {
margin: 10px 0;
}
.navbar .slider .hammburger-links .open {
display: inline;
答案 0 :(得分:1)
有很多更改-主要说明如下:
首先在代码段中添加了navbar
包装器,而您的.querySelector('under-home')
则缺少了.
(类选择器),
在position: relative
上添加了.hammburger-links
,以便子菜单相对于此元素绝对定位。
在您的 eventListener 中添加了e.preventDefault()
,以便禁用单击超链接时的默认操作(我们希望菜单在此处向右切换),
在.open
上使用display: flex
代替display: inline
,
请参见下面的演示
let home = document.getElementById('home');
let underhome = document.querySelector('.under-home');
home.addEventListener('click', function(e) {
e.preventDefault(); /* prevent default hyperlink action */
underhome.classList.toggle('open');
});
.navbar .slider .hammburger-links {
padding: 0 1.250em;
display: flex;
align-items: center;
position: relative; /* added */
}
.navbar .slider .hammburger-links a {
padding: 0 1.500em;
text-decoration: none;
font-family: "Helvetica", Arial;
font-size: 11px;
color: #a6adb4;
}
.navbar .slider .hammburger-links .under-home {
position: absolute;
top: 1.5em; /* adjusted */
display: flex;
flex-direction: column;
background: #F6F8F9;
min-width: 12.5em;
min-height: 12.5em;
z-index: 1;
display: none;
}
.navbar .slider .hammburger-links .under-home a {
margin: 10px 0;
}
.navbar .slider .hammburger-links .open {
display: flex; /* changed */
}
<div class="navbar">
<div class="slider">
<div class="hammburger-links">
<a href="" id="home">HOME</a>
<div class="under-home">
<a href="">WORLD NEWS</a>
<a href="">TRAVEL</a>
<a href="">TECHNOLOGY</a>
<a href="">CITY</a>
<a href="">CULTURE</a>
<a href="">MORE...</a>
</div>
<a href="">DISCOVERY</a>
<a href="">PHOTOS</a>
<a href="">CONTACT</a>
<img src="https://via.placeholder.it/200" alt="">
</div>
</div>
</div>
答案 1 :(得分:0)
尝试一下
let home = document.getElementById('home');
home.addEventListener('click', function() {
var element = document.getElementById('under_home');
element.classList.toggle("open");
});
.navbar .slider .hammburger-links {
padding: 0 1.250em;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.navbar .slider .hammburger-links a {
padding: 0 1.500em;
text-decoration: none;
font-family: "Helvetica", Arial;
font-size: 11px;
color: #a6adb4;
}
.navbar .slider .hammburger-links .under-home {
position: absolute;
top: 14%;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
background: #F6F8F9;
min-width: 12.5em;
min-height: 12.5em;
z-index: 1;
display: none;
}
.navbar .slider .hammburger-links .under-home a {
margin: 10px 0;
}
.navbar .slider .hammburger-links .open {
display: inline;
}
<div class="navbar">
<div class="slider">
<div class="hammburger-links">
<a href="" id="home">HOME</a>
<div class="under-home" id="under_home">
<a href="">WORLD NEWS</a>
<a href="">TRAVEL</a>
<a href="">TECHNOLOGY</a>
<a href="">CITY</a>
<a href="">CULTURE</a>
<a href="">MORE...</a>
</div>
<a href="">DISCOVERY</a>
<a href="">PHOTOS</a>
<a href="">CONTACT</a>
<img src="images/navbar-img.png" alt="">
</div>
</div>
</div>