Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOS animation-play-state 解决方案 #9

Open
linweiwei123 opened this issue Apr 27, 2018 · 0 comments
Open

IOS animation-play-state 解决方案 #9

linweiwei123 opened this issue Apr 27, 2018 · 0 comments

Comments

@linweiwei123
Copy link
Owner

linweiwei123 commented Apr 27, 2018

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=375, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .spin {
            position: absolute;
            top:50%;
            left: 50%;
            transform: translate(-50%, -50%);
            animation: rotate-music 10s infinite linear;
        }
        .no-animation {
            -webkit-animation: none !important;
            -moz-animation: none !important;
            animation: none !important;
        }
        button {
            font-size: 32px;
        }
        @-webkit-keyframes rotate-music{
            0% {
                transform: rotate(0deg);
            }
            100% {transform: rotate(360deg);}
        }
    </style>
</head>
<body>
<h1 id="text" class="spin">text...</h1>
<button onclick="stop()">stop</button>
<button onclick="restart()">restart</button>

<script>
    function stop(){
        var textEl = document.querySelector('#text');
        document.querySelector('#text').style.transform = 'rotate('+ getRotate(document.getElementById('text')) +'deg)';
        change();
        textEl.classList.add('no-animation');
    }

    function restart(){
        document.querySelector('#text').classList.remove('no-animation');
    }

    function getRotate(el){
        var st = window.getComputedStyle(el, null);
        var tr = st.getPropertyValue("-webkit-transform") ||
            st.getPropertyValue("-moz-transform") ||
            st.getPropertyValue("-ms-transform") ||
            st.getPropertyValue("-o-transform") ||
            st.getPropertyValue("transform") ||
            "FAIL";
        var values = tr.split('(')[1].split(')')[0].split(',');
        var a = values[0];
        var b = values[1];
        var angle = Math.round(Math.atan2(b, a) * (180/Math.PI));
        return angle;
    }

    function change(){
        var rule = document.styleSheets[0].cssRules[3];
        var deg = getRotate(document.getElementById('text'));
        var end = deg + 360;
        rule.deleteRule("0%");
        rule.deleteRule("100%");
        rule.appendRule("0% {  transform: rotate("+ deg + "deg);}");
        rule.appendRule("100% { transform: rotate("+ end + "deg);}");
    }
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant