我在http://elijahmanor.com/css-animated-hamburger-icon/有一个动画的CSS汉堡图标,我想要使用它,但我想通过悬停来查看set ex(x),当没有悬停时将其设置为默认值风格那3行。但即使我使用悬停功能,我也做不到。
document.querySelector("#nav-toggle")
.addEventListener("click", function() {
this.classList.toggle("active");
});

#nav-toggle {
position: absolute;
left: 50%;
top: 50%;
}
#nav-toggle {
cursor: pointer;
padding: 10px 35px 16px 0px;
}
#nav-toggle span,
#nav-toggle span:before,
#nav-toggle span:after {
cursor: pointer;
border-radius: 1px;
height: 5px;
width: 35px;
background: black;
position: absolute;
display: block;
content: '';
}
#nav-toggle span:before {
top: -10px;
}
#nav-toggle span:after {
bottom: -10px;
}
#nav-toggle span,
#nav-toggle span:before,
#nav-toggle span:after {
transition: all 500ms ease-in-out;
}
#nav-toggle.active span {
background-color: transparent;
}
#nav-toggle.active span:before,
#nav-toggle.active span:after {
top: 0;
}
#nav-toggle.active span:before {
transform: rotate(45deg);
}
#nav-toggle.active span:after {
transform: rotate(-45deg);
}

<a id="nav-toggle" href="#"><span></span></a>
&#13;
答案 0 :(得分:1)
您可以使用鼠标悬停功能来完成。
document.querySelector("#nav-toggle")
.addEventListener("mouseover", function() {
this.classList.toggle("active");
});
&#13;
#nav-toggle {
position: absolute;
left: 50%;
top: 50%;
}
#nav-toggle {
cursor: pointer;
padding: 10px 35px 16px 0px;
}
#nav-toggle span,
#nav-toggle span:before,
#nav-toggle span:after {
cursor: pointer;
border-radius: 1px;
height: 5px;
width: 35px;
background: black;
position: absolute;
display: block;
content: '';
}
#nav-toggle span:before {
top: -10px;
}
#nav-toggle span:after {
bottom: -10px;
}
#nav-toggle span,
#nav-toggle span:before,
#nav-toggle span:after {
transition: all 500ms ease-in-out;
}
#nav-toggle.active span {
background-color: transparent;
}
#nav-toggle.active span:before,
#nav-toggle.active span:after {
top: 0;
}
#nav-toggle.active span:before {
transform: rotate(45deg);
}
#nav-toggle.active span:after {
transform: rotate(-45deg);
}
&#13;
<a id="nav-toggle" href="#"><span></span></a>
&#13;
答案 1 :(得分:0)
悬停事件称为mouseover
。
事件名称是经典的HTML DOM Events,只是没有 on 前缀。例如,使用
mouseover
代替onmouseover
。
document.querySelector( "#nav-toggle" )
.addEventListener( "mouseover", function() {
this.classList.toggle( "active" );
});
&#13;
#nav-toggle { position: absolute; left: 50%; top: 50%; }
#nav-toggle { cursor: pointer; padding: 10px 35px 16px 0px; }
#nav-toggle span, #nav-toggle span:before, #nav-toggle span:after {
cursor: pointer;
border-radius: 1px;
height: 5px;
width: 35px;
background: black;
position: absolute;
display: block;
content: '';
}
#nav-toggle span:before {
top: -10px;
}
#nav-toggle span:after {
bottom: -10px;
}
#nav-toggle span, #nav-toggle span:before, #nav-toggle span:after {
transition: all 500ms ease-in-out;
}
#nav-toggle.active span {
background-color: transparent;
}
#nav-toggle.active span:before, #nav-toggle.active span:after {
top: 0;
}
#nav-toggle.active span:before {
transform: rotate(45deg);
}
#nav-toggle.active span:after {
transform: rotate(-45deg);
}
&#13;
<a id="nav-toggle" href="#"><span></span></a>
&#13;
答案 2 :(得分:0)
您只需在脚本中使用mouseover
代替hover
。
document.querySelector("#nav-toggle")
.addEventListener("mouseover", function() {
this.classList.toggle("active");
});
答案 3 :(得分:0)
您需要使用mouseover
事件。
document.querySelector( "#nav-toggle" )
.addEventListener( "mouseover", function() {
this.classList.toggle( "active" );
});
&#13;
#nav-toggle { position: absolute; left: 50%; top: 50%; }
#nav-toggle { cursor: pointer; padding: 10px 35px 16px 0px; }
#nav-toggle span, #nav-toggle span:before, #nav-toggle span:after {
cursor: pointer;
border-radius: 1px;
height: 5px;
width: 35px;
background: black;
position: absolute;
display: block;
content: '';
}
#nav-toggle span:before {
top: -10px;
}
#nav-toggle span:after {
bottom: -10px;
}
#nav-toggle span, #nav-toggle span:before, #nav-toggle span:after {
transition: all 500ms ease-in-out;
}
#nav-toggle.active span {
background-color: transparent;
}
#nav-toggle.active span:before, #nav-toggle.active span:after {
top: 0;
}
#nav-toggle.active span:before {
transform: rotate(45deg);
}
#nav-toggle.active span:after {
transform: rotate(-45deg);
}
&#13;
<a id="nav-toggle" href="#"><span></span></a>
&#13;