javascript - Why when I use transform scale it moves the animated div's position - Stack Overflow

EDIT : I don't think this question should be marked as a possible duplicate because It deals with

EDIT : I don't think this question should be marked as a possible duplicate because It deals with distracting SVG info and I think unnecessary math.

In the keyframes section below when I leave out the transform: scale the div shows up in the middle of the page. That's what I want. Now I wanted to make it so that the div start out bigger and the opacity fadesIn while the div gets smaller and the center of the div should be at the center of the page. When I add transform scale the div doesn't animate in the center anymore. why? and how can I fix it?

   .popup{
        position: absolute;
        width:400px;
        height: 300px;
        left: 50%;
        top: 50%;
        background: limegreen;
        border: 5px solid silver;
        transform: translate(-50%, -50%);
        transition: all 2s;
        opacity: 0;

    }
    .anim{
        animation: popAnim 2s forwards;
    }
    @keyframes popAnim{
        0%{
            transform : scale(1.5);
            opacity: 0;
        }
        100%{
            transform: scale(1);
            opacity: 1;
        }
    }

html:

<div class="popup">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error inventore molestiae, dignissimos ducimus adipisci sint suscipit dolor blanditiis fugit, quia aspernatur quos, facere nostrum! Distinctio praesentium saepe, quaerat modi fuga.</div>
<button class="click">click me</button>

jquery:

$(document).ready( function(){
        $(".click").on("click", function(){
            $(".popup").toggleClass("anim")
        })
});

EDIT : I don't think this question should be marked as a possible duplicate because It deals with distracting SVG info and I think unnecessary math.

In the keyframes section below when I leave out the transform: scale the div shows up in the middle of the page. That's what I want. Now I wanted to make it so that the div start out bigger and the opacity fadesIn while the div gets smaller and the center of the div should be at the center of the page. When I add transform scale the div doesn't animate in the center anymore. why? and how can I fix it?

   .popup{
        position: absolute;
        width:400px;
        height: 300px;
        left: 50%;
        top: 50%;
        background: limegreen;
        border: 5px solid silver;
        transform: translate(-50%, -50%);
        transition: all 2s;
        opacity: 0;

    }
    .anim{
        animation: popAnim 2s forwards;
    }
    @keyframes popAnim{
        0%{
            transform : scale(1.5);
            opacity: 0;
        }
        100%{
            transform: scale(1);
            opacity: 1;
        }
    }

html:

<div class="popup">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error inventore molestiae, dignissimos ducimus adipisci sint suscipit dolor blanditiis fugit, quia aspernatur quos, facere nostrum! Distinctio praesentium saepe, quaerat modi fuga.</div>
<button class="click">click me</button>

jquery:

$(document).ready( function(){
        $(".click").on("click", function(){
            $(".popup").toggleClass("anim")
        })
});
Share Improve this question edited Aug 30, 2015 at 8:18 jack blank asked Aug 30, 2015 at 7:59 jack blankjack blank 5,2257 gold badges45 silver badges75 bronze badges 3
  • 1 possible duplicate of Scale path from center – Michael Seltenreich Commented Aug 30, 2015 at 8:03
  • its not moving, its just zooming out based on center.. jsfiddle/josangel555/6rkLc7jc – Jos Commented Aug 30, 2015 at 8:05
  • EDIT : I don't think this question should be marked as a possible duplicate because It deals with distracting SVG info and I think unnecessary math. – jack blank Commented Aug 30, 2015 at 8:18
Add a ment  | 

1 Answer 1

Reset to default 7

In declaraing transform : scale(1.5); you're overriding transform: translate(-50%, -50%);, so transform: scale(1.5) is the equivalent of transform: translate(0, 0) scale(1.5).

Instead, you need to stack your transform values so the translate is maintained. The keyframe animation bees:


  @keyframes popAnim{
    0%{
      transform : translate(-50%, -50%) scale(1.5);
      opacity: 0;
    }
    100%{
      transform: translate(-50%, -50%) scale(1);
      opacity: 1;
    }
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744791025a4593916.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信