2016-05-16 48 views
-1

我需要在用户关注它时移动我的搜索框。我正在使用CSS动画,这是工作,但不能正常工作。我想让当前的搜索框向上移动,但现在看起来新的搜索框正在取代当前的搜索框。使用CSS动画在顶部移动搜索框

$('input').focus(function(){ 
 
$('.search').addClass('fixed'); 
 
}) 
 

 
$('input').blur(function(){ 
 
$('.search').removeClass('fixed'); 
 
})
body{ 
 
    margin:0 
 
} 
 
.banner{ 
 
    height:200px; 
 
    background:#ff0000 
 
} 
 
.search{} 
 
.search-bar{ height:50px; background:#666; padding-top:10px} 
 
.search-bar input{width:100%; height:35px} 
 
.animated{background:#fff; opacity:0; position:fixed; bottom:0; height:0px; transition:height 0.5s; width:100%} 
 
.fixed .animated{ height:100%; opacity:1}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<div> 
 
    <div class="banner"> 
 
    
 
    </div> 
 
    <div class="search"> 
 
    <div class="search-bar"> 
 
     <input type="text"> 
 
    </div> 
 
    <div class="animated"> 
 
     <div class="search-bar"> 
 
     <input type="text"> 
 
    </div> 
 
     
 
    </div> 
 
    </div> 
 
    <div class="body"> 
 
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum</p> 
 
    <p> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
 
    </p> 
 
    <p> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
 
    </p> 
 
    <p> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
 
    </p> 
 
    <p> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
 
    </p> 
 
    <p> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
 
    </p> 
 
    </div> 
 
</div>

+0

为什么两个输入?使用一个并移动它。 – matpol

+0

我没有通过使用一个输入来获得动画效果 – Carlos

回答

1

在这里,使用具有相同的动画的样本仅1搜索框(输入)

$('input').focus(function(){ 
 
    $('.search').addClass('fixed'); 
 
}) 
 

 
$('input').blur(function(){ 
 
    $('.search').removeClass('fixed'); 
 
})
html, body { 
 
    margin: 0 
 
} 
 
.banner { 
 
    height: 200px; 
 
    background: #ff0000 
 
} 
 
.search-bar { 
 
    height: 50px; 
 
    background: #666; 
 
    padding-top: 10px 
 
} 
 
.search-bar input { 
 
    width: 100%; 
 
    height: 35px 
 
} 
 
.search { 
 
    transition: bottom 0.5s, top 0.5s; 
 
    background: #ddd; 
 
    width: 100%; 
 
    min-height: 50px; 
 
    top: 200px; 
 
    bottom: 40%; 
 
} 
 
.search.fixed { 
 
    transition: bottom 0.5s, top 0.5s; 
 
    top: 0; 
 
    bottom: 0; 
 
    position: fixed; 
 
    width: 100%; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<div> 
 
    <div class="banner"> 
 

 
    </div> 
 
    <div class="search"> 
 
    <div class="search-bar"> 
 
     <input type="text"> 
 
    </div>  
 
    </div> 
 
    <div class="body"> 
 
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum</p> 
 
    <p> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
 
    </p> 
 
    <p> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
 
    </p> 
 
    <p> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
 
    </p> 
 
    <p> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
 
    </p> 
 
    <p> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
 
    </p> 
 
    </div> 
 
</div>

+0

现在它首先进入底部然后回到顶部。它应该从目前的版图上移到顶端 – Carlos

+0

@Carlos以灰色背景更新,所以你会看到会发生什么,所以当你说“先走到最后”时,它仍然会这么做吗? – LGSon

+0

已通过验证,但好像是从底部开始 – Carlos