所以,我目前正在一个网站上练习网页开发,但我遇到了一些障碍。我希望能够点击图像"搜索图标"然后让我的文本字段滑出,但它没有聚焦并准备好供用户输入。当我添加焦点事件时,它会起作用,但它会失去转换并将我的其他内容推送到我的导航架中,暂时搁置一秒钟。我很困惑为什么会这样,我希望得到一些帮助,解决我的问题并更好地理解它。
这是我的HTML:
<header>
<div class="nav-holder">
<nav>
<ul>
<li><a class="active" href="index.html">HOME</a></li>
<li><a href="#">WORK EXPERIENCE <span class="one"></span><span class="two"></span></a>
<ul>
<li><a href="#">WEB DEVELOPEMENT</a></li>
<li><a href="#">IT SUPPORT</a></li>
</ul>
</li>
<li><a href="#">INTRODUCING ME</a></li>
<li><a href="#">CONTACT INFORMATION</a></li>
</ul>
</nav>
<form method="post" action="">
<input id="search-box" type="text" name="search" placeholder="Search...">
</form>
<img id="search-icon" src="img/searchicon.png"/>
<div class="clear"></div>
</div>
</header>
这是我的CSS:
* {
margin: 0;
padding: 0;
}
.nav-holder {
position: fixed;
top: 0;
background-color: black;
opacity: .8;
max-width: 100vw;
width: 100%;
max-height: 40px;
height: 100%;
overflow: hidden;
z-index: 1;
}
nav {
width: 72vw;
line-height: 28px;
font-size: 12px;
}
.active {
color: lightgrey;
}
nav li {
list-style: none;
float: left;
}
nav a {
text-decoration: none;
padding: 6px 12px;
display: block;
color: dimgrey;
font-family: "Verdana";
}
nav a:hover {
color: lightgrey;
transition: all ease-in-out .3s;
}
nav a:hover .one, nav a:hover .two {
background-color: lightgrey;
transition: all ease-in-out .3s;
}
nav ul ul {
position: fixed;
opacity: 0;
z-index: -1;
visibility: hidden;
background-color: black;
transform: translateY(-2em);
transition: all 0.3s ease-in-out 0s, visibility 0s linear 0.3s, z-index 0s linear 0.01s;
}
nav ul li:hover > a {
color: lightgrey;
}
nav ul li:hover .one, nav ul li:hover .two {
background-color: lightgrey;
}
nav ul li:hover ul {
visibility: visible;
opacity: 1;
z-index: 2;
transform: translateY(0%);
transition-delay: 0s, 0s, 0.3s;
}
nav ul li ul li {
float: none;
}
.one {
display: inline-block;
background-color: dimgrey;
height: 1px;
width: 7px;
transform: rotate(55deg);
margin-left: -3px;
margin-bottom: 4px;
}
.two {
display: inline-block;
background-color: dimgrey;
height: 1px;
width: 7px;
transform: rotate(125deg);
margin-left: -3.2px;
margin-bottom: 4px;
}
#search-icon {
float: right;
max-height: 18px;
height: 100%;
max-width: 18px;
width: 100%;
cursor: pointer;
margin-top: 11px;
margin-right: 7px;
}
#search-box {
width: 200px;
box-sizing: border-box;
transition: all ease-in-out .3s;
position: relative;
float: right;
margin-top: 9px;
margin-right: -200px;
line-height: 22px;
border: none;
outline: none;
background-color: black;
opacity: .8;
}
#search-box:focus {
caret-color: lightgrey;
color: lightgrey;
}
#search-box.active {
margin-right: 0;
}
@-webkit-keyframes autofill {
to {
color: lightgrey;
background: transparent;
}
}
input:-webkit-autofill {
-webkit-animation-name: autofill;
-webkit-animation-fill-mode: both;
}
.clear {
clear: both;
}
这是我的JQuery:
$(document).ready(function () {
$('html').on('click', function (e) {
if (e.target.id == 'search-icon') {
$('#search-box').toggleClass('active');
$('#search-box').focus();
}
else if (e.target.id == 'search-box') {
$('#search-box').focusin();
}
else {
$('#search-box').removeClass('active');
$('#search-box').val("");
}
});
});
感谢您提前获得帮助!
答案 0 :(得分:0)
删除溢出:隐藏.nav-holder
。您需要处理导航栏的布局以获得响应。
$(document).ready(function() {
$('html').on('click', function(e) {
if (e.target.id == 'search-icon') {
$('#search-box').toggleClass('active');
$('#search-box').focus();
} else if (e.target.id == 'search-box') {
$('#search-box').focusin();
} else {
$('#search-box').removeClass('active');
$('#search-box').val("");
}
});
});
&#13;
* {
margin: 0;
padding: 0;
}
.nav-holder {
position: fixed;
top: 0;
background-color: black;
opacity: .8;
max-width: 100vw;
width: 100%;
max-height: 40px;
height: 100%;
z-index: 1;
}
nav {
width: 72vw;
line-height: 28px;
font-size: 12px;
}
.active {
color: lightgrey;
}
nav li {
list-style: none;
float: left;
}
nav a {
text-decoration: none;
padding: 6px 12px;
display: block;
color: dimgrey;
font-family: "Verdana";
}
nav a:hover {
color: lightgrey;
transition: all ease-in-out .3s;
}
nav a:hover .one,
nav a:hover .two {
background-color: lightgrey;
transition: all ease-in-out .3s;
}
nav ul ul {
position: fixed;
opacity: 0;
z-index: -1;
visibility: hidden;
background-color: black;
transform: translateY(-2em);
transition: all 0.3s ease-in-out 0s, visibility 0s linear 0.3s, z-index 0s linear 0.01s;
}
nav ul li:hover>a {
color: lightgrey;
}
nav ul li:hover .one,
nav ul li:hover .two {
background-color: lightgrey;
}
nav ul li:hover ul {
visibility: visible;
opacity: 1;
z-index: 2;
transform: translateY(0%);
transition-delay: 0s, 0s, 0.3s;
}
nav ul li ul li {
float: none;
}
.one {
display: inline-block;
background-color: dimgrey;
height: 1px;
width: 7px;
transform: rotate(55deg);
margin-left: -3px;
margin-bottom: 4px;
}
.two {
display: inline-block;
background-color: dimgrey;
height: 1px;
width: 7px;
transform: rotate(125deg);
margin-left: -3.2px;
margin-bottom: 4px;
}
#search-icon {
float: right;
max-height: 18px;
height: 100%;
max-width: 18px;
width: 100%;
cursor: pointer;
margin-top: 11px;
margin-right: 7px;
}
#search-box {
width: 200px;
box-sizing: border-box;
transition: all ease-in-out .3s;
position: relative;
float: right;
margin-top: 9px;
margin-right: -200px;
line-height: 22px;
border: none;
outline: none;
background-color: black;
opacity: .8;
}
#search-box:focus {
caret-color: lightgrey;
color: lightgrey;
}
#search-box.active {
margin-right: 0;
}
@-webkit-keyframes autofill {
to {
color: lightgrey;
background: transparent;
}
}
input:-webkit-autofill {
-webkit-animation-name: autofill;
-webkit-animation-fill-mode: both;
}
.clear {
clear: both;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<div class="nav-holder">
<nav>
<ul>
<li><a class="active" href="index.html">HOME</a></li>
<li><a href="#">WORK EXPERIENCE <span class="one"></span><span class="two"></span></a>
<ul>
<li><a href="#">WEB DEVELOPEMENT</a></li>
<li><a href="#">IT SUPPORT</a></li>
</ul>
</li>
<li><a href="#">INTRODUCING ME</a></li>
<li><a href="#">CONTACT INFORMATION</a></li>
</ul>
</nav>
<form method="post" action="">
<input id="search-box" type="text" name="search" placeholder="Search...">
</form>
<img id="search-icon" src="http://placehold.it/50/00ff00" />
<div class="clear"></div>
</div>
</header>
&#13;