网站搜索

javascript - 类似 HTML5 Bing 的整洁内容过渡


我最近在 Bing 上看到一个很不错的效果预览,适用于 IE9 和 Safari 5。当你在搜索框按下回车时,搜索框会很漂亮地移动到页面顶部,搜索结果从底部折叠出来。你可以在这里看到它的实际效果... http://www.youtube.com/watch?v=NYuLALX6aeI#at=69

我的问题是,这个是如何完成的,我如何才能做到?我希望你能理解我的问题。

答案#1

基本想法:

JQuery:

$('#go').click(function() {
    $('#form').animate({
        'height': '80px',
        'text-indent': '50px',
        'padding-top':'20px'
    }, {
        queue: false,
        duration: 1500,
        complete:function(){
            $('html,body').css('overflow-y','visible');
        }
    });
    $('#results').show({
        type: 'slide',
        direction: 'up'
    }, {
        queue: false,
        duration: 1500
    });
});

CSS:

#form {
    background-color:blue;
    text-indent:300px;
    width:100%;
    height:100%;
    padding-top:200px;
}

#results {
    background-color:yellow;
    display:none;
    height:700px;
}
body, html {
    width:100%;
    height:100%;
    overflow:hidden;
}

HTML:

<div id="form">
    <input type="text" /> 
    <input type="button" id="go" value="go" />
</div>
<div id="results">Search results</div>

演示: http://jsfiddle.net/AlienWebguy/hwAtU/

答案#2

它们都在使用 CSS3 动画。我在这里弄了一个带动画的非常简单的搜索框。请使用 Chrome 或 Safari。只需输入内容并按回车。

CSS

body{text-align:center; padding: 200px 0;-webkit-transition: all 0.4s linear;}
#search{-webkit-transition: all 0.2s linear;}
#search:focus{-webkit-transform:scale(1.4);}

HTML

 <input type="search" placeholder="Search..." id="search"/>

JS :

document.getElementById('search').addEventListener('keydown', function(e){
    if(e.keyCode == 13){document.body.style.padding = "40px";}
}, false);

使用 Javascript 来进行定位和动画不是语义化的代码。CSS 动画速度更快,效果更流畅。