在浏览器中居中动画

时间:2013-08-28 08:30:09

标签: jquery animation center

我正在创建一个类似于TypeCode(http://typecode.com/work/)的投资组合部分。我有简单的jQuery动画,可以在点击时扩展和收缩div。有没有办法在每次点击时将浏览器中的div集中在TypeCode上?

    // Select portfolio item
var active = false;
$('.portfolio-item').click(function(e) {

    // Check if alternative tab is open and then close it
     if ( $('#one').css("height") == "220px" ) {
        $('#one').animate({
            height: '110px',
        }, 1000, function() {
            // Animation complete    
        });
        active = false;
     }; // End if

    if ( $('#two').css("height") == "220px" ) {
        $('#two').animate({
            height: '110px',
        }, 1000, function() {
            // Animation complete    
        });
        active = false;
     }; // End if

    if (active == false) {
        $('.portfolio-item').css("height", "110px");

        $(this).animate({
            height: '220px',
        }, 1000, function() {
            // Animation complete    
        });

        active = true;
    }; // End if


}); // End click function


// Close portfolio item
$('.close').click(function(e) {
    e.stopPropagation();

    $(this).parents('.portfolio-item').animate({
        height: '110px',
    }, 1000, function() {

    });

    active = false;
}); 

这是基本的小提琴:http://jsfiddle.net/7Avu9/1/

如您所见,如果您单击另一个选项卡(仅从前两个选项卡),则关闭当前选项卡并打开新选项卡。这适用于少量内容,但内容较大,但效果不佳。

1 个答案:

答案 0 :(得分:0)

jsFiddle

$('article').click(function(){
    $(this).toggleClass('state-collapsed');
    $('html,body').animate({scrollTop: $(this).offset().top}, 500);
});

这应该有希望帮助