简体   繁体   English

如何从中心动画div

[英]how to animate div from its center

I am trying to animate a div tag from center. 我试图从中心动画div标签。 Initially I want the div tag to be none visible. 最初我希望div标签不可见。 when user clicks on link then this div tag should animate from its center. 当用户点击链接时,此div标签应从其中心动画。 How can I achieve this using jquery. 我怎样才能使用jquery实现这一点。 Here is my current code. 这是我目前的代码。

<a href="#" class="linkone">link one</a><br><br><br><br>
<section class="one">one</section>

here is css 这是css

.one {
        margin: 0 auto;
        width: 200px;
        height: 200px;
        border: 1px solid red;
        background: lightblue;
    }

and here is my jquery 这是我的jquery

$('a').on('click', function (e) {
    e.preventDefault();
    var w = 200; //$('.one').outerWidth();
    var h = 200; //$('.one').outerHeight();
    var x = $('.one').width() / 2;
    var y = $('.one').height() / 2;
    var startW = h - y/2;
    var startH = w - x/2;
    var endTop = y - h/2;
    var endLeft = x - w/2;

    $('.one').animate({
        opacity: 1,
        width: (w+200) + 'px',
        height: (h+200) + 'px',
        top: endTop+'px',
        left: endLeft+'px'
    }, 1000);

    console.log(endLeft);
});

Something like this: http://jsfiddle.net/TJNTq/ 像这样: http//jsfiddle.net/TJNTq/

$('.linkone').on('click', function (e) {
    e.preventDefault();
    var w = 200; //$('.one').outerWidth();
    var h = 200; //$('.one').outerHeight();
    var x = $('.one').width() / 2;
    var y = $('.one').height() / 2;
    var startW = h - y/2;
    var startH = w - x/2;
    var endTop = y - h/2;
    var endLeft = x - w/2;

    $('.one').show().animate({
        opacity: 1,
        width: (w+200) + 'px',
        height: (h+200) + 'px',
        marginTop: 0 + 'px'
    }, 1000);

    console.log(endLeft);
});

note that your animations of top and left weren't doing anything because the .one div isn't positioned absolutely or relatively. 请注意,您的顶部和左侧动画没有做任何事情,因为.one div没有绝对或相对定位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM