简体   繁体   English

更改CSS动画锚点

[英]Changing CSS Animation anchor point

I have the following page (see image) 我有以下页面(见图) 在此处输入图片说明

When I click the red button, I want a div to animate when it shows to the user. 当我单击红色按钮时,我希望div在显示给用户时进行动画处理。 However, I want the animation to come from the button. 但是,我希望动画来自按钮。 Right now the animation comes from the center of the page. 现在,动画来自页面中心。

Here's the code I have 这是我的代码

@keyframes fadeInScale {
    0%{
        opacity:0;
        transform: scale(0.5);
    }
    100%{
        opacity:1;
        transform: scale(1);
    }
}

@keyframes fadeOutScale {
    0%{
        opacity:1;
        transform: scale(1);
    }
    100%{
        opacity:0;
        transform: scale(0.5);
    }
}

.overlay {
    position: absolute;
    top: 0;
    z-index: 500;
    width: 100%;
    height: 100%;
    left: 0;
    right: 0;
    bottom: 0;
}

.fade-scale-in {
    display: block;
    animation: fadeInScale 0.3s 1 ease-out;
}

.fade-scale-out {
    display: block;
    animation: fadeOutScale 0.3s 1 ease-out;
}

When I click on the red circle, the user sees an overlay page (which is actually a div with class overlay that is absolutely positioned). 当我单击红色圆圈时,用户会看到一个覆盖页面(实际上是具有绝对位置的类覆盖的div)。 I add fade-scale-in class to the div (with class overlay). 我在div中添加了淡入淡出类(带有类覆盖)。 The div then transitions from the center of the screen and grows to fit the user's screen. 然后,div从屏幕的中心过渡,并增长以适合用户的屏幕。 I want it so that the div transitions from the red circle. 我想要它,以便div从红色圆圈过渡。 Can I use transforms to make this happen? 我可以使用转换来实现这一目标吗?

Here's the code. 这是代码。 As you notice, when you click on the yellow circle, the div animates from the center and not from the yellow circle: https://jsfiddle.net/e4g71y4m/3/ 如您所见,当您单击黄色圆圈时,div从中心而不是从黄色圆圈进行动画: https : //jsfiddle.net/e4g71y4m/3/

You can do this by adding transform-origin: top left; 您可以通过添加transform-origin: top left;来做到这一点transform-origin: top left; to the .overlay element. .overlay元素。 (Don't forget the prefixes ( -webkit , -moz etc.) (不要忘记前缀( -webkit-moz等)。

 var overlay$ = $('.overlay'); overlay$.bind('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd', function(e) { console.debug('test'); if (e.animationName !== undefined && e.animationName == "fadeOutScale") { overlay$.addClass('hide'); } else if (e.originalEvent.animationName !== undefined && e.originalEvent.animationName == "fadeOutScale") { overlay$.addClass('hide'); } }); $('.click').on('click', function() { overlay$.removeClass('hide').addClass('fade-scale-in'); }); $('.click-again').on('click', function() { overlay$.removeClass('hide').addClass('fade-scale-out'); }); 
 @keyframes fadeInScale { 0% { opacity: 0; transform: scale(0.5); } 100% { opacity: 1; transform: scale(1); } } @keyframes fadeOutScale { 0% { opacity: 1; transform: scale(1); } 100% { opacity: 0; transform: scale(0.5); } } .hide { display: none; } .overlay { position: absolute; top: 0; z-index: 500; width: 100%; height: 100%; left: 0; right: 0; bottom: 0; background: red; transform-origin: top left; } .click { background: yellow; border-radius: 50%; width: 100px; height: 100px; line-height: 100px; text-align: center; cursor: pointer; } .fade-scale-in { display: block; animation: fadeInScale 0.3s 1 ease-out; } .fade-scale-out { display: block; animation: fadeOutScale 0.3s 1 ease-out; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="click">Click me</div> <div class="overlay hide"> Test <div class="click-again">Click me again</div> </div> 

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

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