How to install and run css transition from javascript

I am new to html5, css and javascript, and basically I just played. What I want to do is set and initiate the transition div. After loading the page, I manage to do this by setting the transition. But this is not very dynamic and does not seem to be the right way. I am grateful for any help.

<!DOCTYPE html>
<html>
<head>   
    <style> 
        body{
            text-align: center;
        }

        #dialPointer
        {
            position:relative;
            margin-left: auto;
            margin-right: auto;
            width:23px;
            height:281px;
            background:url(pointer.png);
            background-size:100% 100%;
            background-repeat:no-repeat;

            transform: rotate(-150deg);
            transition-duration: 2s;
            transition-delay: 2s;

            -webkit-transform: rotate(-150deg);
            -webkit-transition-duration:2s;
            -webkit-transition-delay: 2s;
        }


        /* I want to call this once */
        .didLoad
        {
            width:23px;
            height:281px;
            transform:rotate(110deg);
            -moz-transform:rotate(110deg); /* Firefox 4 */
            -webkit-transform:rotate(110deg); /* Safari and Chrome */
            -o-transform:rotate(110deg); /* Opera */
        }

        </style>
</head>

<body>
    <div id="dialPointer"></div>
    <script language="javascript" type="text/javascript">
        window.onload=function () {
            //But instead of using rotate(110deg), I would like to call "didLoad"

            document.getElementById("dialPointer").style.webkitTransform = "rotate(110deg)";


        };
        </script>
</body>
</html>
+5
source share
3 answers

You can add your class to the element when you want to use the following JavaScript:

document.getElementById("dialPointer").className += " didLoad";

Or perhaps better if you want to guarantee cross-browser support using jQuery as follows:

$(function() {
 // Handler for .ready() called.
 $('#dialPointer').addClass('didLoad');
});

Edit:

fiddle, Chrome Safari Windows. dialPointer didLoad. , .

+4

, , - CSS. - , Javascript. , :

document.getElementById('dialPointer').classList.add('didLoad');

. ( Javascript , , , ).

, :

window.addEventListener('load', function () {
    document.getElementById('dialPointer').classList.add('didLoad');
});
+2
source

Maybe something like this:

document.getElementById("dialPointer").className += "didLoad";
0
source

All Articles