gpt4 book ai didi

javascript - css 动画运行时不是 :focus but also when page loads

转载 作者:太空宇宙 更新时间:2023-11-03 21:16:10 24 4
gpt4 key购买 nike

我正在为搜索输入字段制作动画。当它处于 :focus 时我让它变大,当它不处于 :focus 时它变小并且它工作正常我遇到了一个问题,当页面加载时它不在焦点上时发生动画。我知道我可以通过在输入字段中进行自动对焦来解决它。这是一个JsFiddle :
html:

<body>
<!--menu start here-->
<div class="menu">
<div class="logo">
Logo goes here
</div>
<div class="search">
<input type="search" class="bar">
<input type="submit" class="submit">
</div>
<ul class="ul">
<li class="menuitem"><a href="#">Link 1</a></li>
<li class="menuitem"><a href="#">Link 2</a></li>
<li class="menuitem"><a href="#">Link 3</a></li>
</ul>
</div>
<!--menu end here-->
</body>

这是CSS:

html,body{
padding:0;
margin:0;
height:100%;
width:100%;
}
.menu{
width:100%;
height:50px;
position:fixed;
top:0;
margin:0;
background:#e6e6e6;
}
.logo{
float:left;
height:50px;
width:250px;
border-right:2px solid white;
}
.search{
display:inline;
float:left;
padding:10px;
}
.bar{
display:inline;
width:150px;
padding:2px;
border-radius:5px;
background:#e6e6e6;
border:1px solid #ccc;
}
.bar:focus{
outline:none;
width:250px;
animation-name:wider;
animation-duration:0.5s;
padding:7px;
background:white;
}
.bar:not(:focus){
animation-name:tighter;
animation-duration:0.5s;
display:inline;
width:150px;
padding:2px;
border-radius:5px;
background:#e6e6e6;
}
.submit{
padding:7px;
display:inline;
background-color:green;
border-radius:5px;
border:1px solid green;
color:lightgreen;
cursor:pointer;
}
.submit:focus{
outline:none;
}
.ul{
float:right;
display:inline;
}
.menuitem{
display:inline;
}
.menuitem>a{
padding:15px;
text-decoration:none;
color:#7a7a7a;
}
.menuitem>a:hover{
background:#5c5c5c;
color:#e6e6e6;
}
@keyframes wider{
from{
width:150px;
padding:2px;
background:#e6e6e6;
}
to{
background:white;
width:250px;
padding:7px;
}
}
@keyframes tighter{
from{
background:white;
width:250px;
padding:7px;
}
to{
width:150px;
padding:2px;
background:#e6e6e6;
}
}


或者如果您更喜欢代码片段:

html,body{
padding:0;
margin:0;
height:100%;
width:100%;
}
.menu{
width:100%;
height:50px;
position:fixed;
top:0;
margin:0;
background:#e6e6e6;
}
.logo{
float:left;
height:50px;
width:250px;
border-right:2px solid white;
}
.search{
display:inline;
float:left;
padding:10px;
}
.bar{
display:inline;
width:150px;
padding:2px;
border-radius:5px;
background:#e6e6e6;
border:1px solid #ccc;
}
.bar:focus{
outline:none;
width:250px;
animation-name:wider;
animation-duration:0.5s;
padding:7px;
background:white;
}
.bar:not(:focus){
animation-name:tighter;
animation-duration:0.5s;
display:inline;
width:150px;
padding:2px;
border-radius:5px;
background:#e6e6e6;
}
.submit{
padding:7px;
display:inline;
background-color:green;
border-radius:5px;
border:1px solid green;
color:lightgreen;
cursor:pointer;
}
.submit:focus{
outline:none;
}
.ul{
float:right;
display:inline;
}
.menuitem{
display:inline;
}
.menuitem>a{
padding:15px;
text-decoration:none;
color:#7a7a7a;
}
.menuitem>a:hover{
background:#5c5c5c;
color:#e6e6e6;
}
@keyframes wider{
from{
width:150px;
padding:2px;
background:#e6e6e6;
}
to{
background:white;
width:250px;
padding:7px;
}
}
@keyframes tighter{
from{
background:white;
width:250px;
padding:7px;
}
to{
width:150px;
padding:2px;
background:#e6e6e6;
}
}
<body>
<!--menu start here-->
<div class="menu">
<div class="logo">
Logo goes here
</div>
<div class="search">
<input type="search" class="bar">
<input type="submit" class="submit">
</div>
<ul class="ul">
<li class="menuitem"><a href="#">Link 1</a></li>
<li class="menuitem"><a href="#">Link 2</a></li>
<li class="menuitem"><a href="#">Link 3</a></li>
</ul>
</div>
<!--menu end here-->
</body>

当页面首次加载时,您可以看到正在发生的动画。无论如何只能使用 css 来做到这一点?如果不是,我不介意使用 javascript 或 jQuery(我两者都不错)

最佳答案

现在导致您看到的问题的原因是更紧密的动画将立即触发,因为 .bar:not(:focus) 将立即“激活”。

我还想说,使用关键帧动画来完成您想要实现的目标有点矫枉过正。一个简单的 css3 转换就足够了,也不会导致您看到的问题:

.bar{
display:inline;
width:150px;
padding:2px;
border-radius:5px;
background:#e6e6e6;
border:1px solid #ccc;
transition: all .5s; /* Transition animates everything that's specified in .bar:focus */
}
.bar:focus{
outline:none;
width:250px;
padding:7px;
background:white;
}

https://jsfiddle.net/3pk1ydvz/

关于javascript - css 动画运行时不是 :focus but also when page loads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42047012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com