简体   繁体   English

如何使用@keyframes正确地动画每个div在纯JavaScript中滑动?

[英]How to properly animate each div to slide in pure JavaScript using @keyframes?

I am doing a project on animating the divs to slide smoothly into the user's view page. 我正在做一个关于动画div的项目,以便平滑地滑入用户的视图页面。 I notice the div will stick it's right and move with margin-left:-700px , which is what I had set but will not move the entire div from the left to right. 我注意到div会向右移动并margin-left:-700px移动margin-left:-700px ,这是我设置但不会从左到右移动整个div。

function show(elem) 
{
    elem = getElem(elem);
    if (elem) 
    {
        elem.style.display = "block";

        var cssAnimation = document.createElement('style'); //responsible for creating animation with <style> element
        cssAnimation.innerHTML = "@keyframes pulseOnce { 0% {margin-left: -700px;} 50%{margin-left: 10px;} 75%{background-color:black;color:white;} 100%{background-color:white;color:inherit;} }";
        cssAnimation.innerHTML += "@-webkit-keyframes pulseOnce { 0% {margin-left: -700px;} 50%{margin-left: 10px;} 75%{background-color:black;color:white;} 100%{background-color:white;color:inherit;} }";
        document.head.appendChild(cssAnimation);
        elem.style.animation = "pulseOnce 1.5s 1";
        elem.style.webkitAnimation = "pulseOnce 1.5s 1";
    }
    return elem;
}

Also, right upon selecting the budget option budgetForm , it will slide both, the budgetForm and the brandForm simultaneously which I don't not want to happen, the rest after that is compliant and experience no problems. 此外,在选择预算选项budgetForm ,它将同时滑动budgetFormbrandForm ,我不希望发生这种情况,其余的则是合规的并且没有遇到任何问题。 This is happening in the show(elem) function . 这发生在show(elem) function How do I prevent both from animating at the same time? 如何防止两者同时动画?

Then after confirming the selections, I notice the queryBox will slide from right to left shortly coming into view, it appends and go back to normal. 然后在确认选择后,我注意到queryBox将从右向左滑动,很快进入视图,它会追加并恢复正常。 I only set a margin-left:1000px; 我只设置了一个margin-left:1000px; . Maybe there's a better style property I could use? 也许我可以使用更好的风格属性?

And lastly, the Reset All button resets again and upon selecting all the options, it does not animate or bring up anything like it's supposed to like upon first visit. 最后,全部重置按钮再次重置,并且在选择所有选项后,它不会动画或显示第一次访问时应该喜欢的任何内容。

Theses are the only hard inconsistencies I'm trying to solve, but can't seem to get it right. 这些是我试图解决的唯一难以解决的问题,但似乎无法做到正确。 Your help would be greatly appreciated. 非常感谢您的帮助。 Please, no jQuery! 拜托,没有jQuery!

To better show what I mean, I provided a jsfiddle: http://jsfiddle.net/WLAC5/ 为了更好地展示我的意思,我提供了一个jsfiddle: http//jsfiddle.net/WLAC5/

I did part of what you wanted to give you an idea of how you should approach this, at the moment you are creating webkit animations in the head which you dont want to do at all, its better to use a class to indicate a state change and a animation. 我做了你希望如何处理这个问题的一部分,当你在头部创建webkit动画时,根本就不想做,最好用一个类来表示状态变化和一个动画。

Updated fiddle: 更新小提琴:

http://jsfiddle.net/WLAC5/2/ http://jsfiddle.net/WLAC5/2/

function show(elem) 
        {
            elem = getElem(elem);
            if (elem) 
            {
                elem.style.display = "block";

                elem.classList.add('pulseIn');

            }
            return elem;
        }

In the css, needs more vendor prefixes but you get the idea: 在CSS中,需要更多的供应商前缀,但你明白了:

@-webkit-keyframes pulseOnce { 
            0% {-webkit-transform: translateX(-100%);} 
            50%{-webkit-transform: translateX(0%);} 
            75%{background-color:black;color:white;} 
            100%{background-color:white;color:inherit;} 
        }
        .pulseIn {
            -webkit-animation: pulseOnce .7s ease-in-out both;
        }

And finally, to ensure that you dont get two sliding in, add this to the html 最后,为了确保你没有两个滑入,将其添加到html

<form id="budgetForm" class="pulseIn">
                Select your DSLR budget range:
                <select id="budgetSel" onChange="onChange();">
                    <!--<option value="selectOneBudget"  selected="selected">Select one</option>
                    <option value="fiveHundredDollars">&lt; $500</option>
                    <option value="thousandDollars">&lt; $1000</option>-->
                </select>
            </form>

You shouldnt look for margin to that slide stuff in and out, preferably use css3 translateX to move stuff around as it is cheap to animate. 你不应该寻找进出幻灯片的余量,最好使用css3 translateX来移动东西,因为它对动画来说很便宜。 Otherwise you can also try postioning with left property. 否则你也可以尝试使用left属性进行定位。

Goodluck! 祝好运!

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

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