使用javascript根据运动方向旋转元素

时间:2018-08-07 21:42:20

标签: javascript css animation transform mousemove

我创建了一个客户请求的“放风筝”页面的模型,这使风筝跟随屏幕上的光标。我想让它旋转,使它看起来像风筝朝着运动的方向移动。

我想做类似的事情:

transform: rotate3d(0, 1, 1, 25deg); //TURN KITE TO THE RIGHT
transform: rotate3d(0, -1, -1, 25deg); //TURN KITE TO THE LEFT

但是我对JS的了解还不足以设置该功能,因此如果X轴上的移动增加,则向右旋转,如果减小,则向左旋转。

有问题的演示:http://stoysnet.com/clients/wingsofthewind/flyme/

请原谅我凌乱的代码...我是Java和CSS动画的新手。

额外:如果有人对如何制作“弦”有任何建议,以使它A)如当前所示向左和向右移动,还向B)提示以使其指向/连接风筝,我是开放的提出建议。


编辑:此处显示页面的完整代码(稍微整理一下),以便其他人在删除模型后仍可以查看它:

// SCRIPT FOR CURSOR FOLLOW
             $(document).on('mousemove', (event) => {
                 $('.follower').css({
                     left: event.clientX,
                     top: event.clientY/1.5 -100,
                 });
                 $('.string').css({
                     left: event.clientX,
                 });
             });
// END SCRIPT FOR CURSOR FOLLOW
        
// SCRIPT FOR SWAP IMAGE
            function pickKite(kite) {
                document.getElementById("flyme").style.backgroundImage = "url(" + kite + ")";
            }
// END SCRIPT FOR SWAP IMAGE
    @keyframes animatedBackground {
	    from { background-position: 0 0; }
	    to { background-position: 100% 0; }
    }
    @keyframes windy {
        0% {
            transform: translateX(-30%) translateY(-30%);
        }
        25% {
            transform: translateX(-50%) translateY(-50%);
        }
        50% {
            transform: translateX(-80%) translateY(-30%);
        }
        75% {
            transform: translateX(-50%) translateY(-10%);
        }
        100% {
            transform: translateX(-30%) translateY(-30%);
        }
    }
    body {
        padding:0px;
        margin:0px;
    }
    .container {
        width:100%;
        height:100vh;
        background-image: url(https://i.imgur.com/ak3aIyl.png);
        background-size: cover;
        background-position: 0px 0px;
        background-repeat: repeat-x;
        animation: animatedBackground 180s linear infinite;
        -ms-animation: animatedBackground 180s linear infinite;
        -moz-animation: animatedBackground 180s linear infinite;
        -webkit-animation: animatedBackground 180s linear infinite;
        cursor:crosshair;
    }
    .follower {
        width: 200px;
        height: 200px;
        background-image: url(https://i.imgur.com/i4xmPPt.png);
        background-position: center center;
        background-size: contain;
        transition-duration: 1800ms;
        transition-timing-function: ease-out;
        position: fixed;
        left:50%;
        top:50%;
        transform: translateX(-50%) translateY(-50%);
        transform-origin:50% 50%;
        animation: windy 20s ease-in-out infinite;
        -ms-animation: windy 20s ease-in-out infinite;
        -moz-animation: windy 20s ease-in-out infinite;
        -webkit-animation: windy 20s ease-in-out infinite;
    }
    .string {
        width: 2px;
        height: 400px;
        background:#FFFFFF;
        background-position: center center;
        background-size: cover;
        transition-duration: 1000ms;
        transition-timing-function: ease-out;
        position: fixed;
        left:50%;
        top:100%;
        transform: translateX(-50%) translateY(-50%);
        transform-origin:100% 50%;
    }
    .kite-selector {
        position: absolute;
        bottom:10px;
        right:10px;
        width:300px;
        height:100px;
        background-color:rgba(0,0,0,0.31);
    }
    .kite-selector div {
        float:left;
        margin-left:15px;
    }
    label > input{ /* HIDE RADIO */
        visibility: hidden; /* Makes input not-clickable */
        position: absolute; /* Remove input from document flow */
    }
    label > input + img{ /* IMAGE STYLES */
        cursor:pointer;
        background-color:rgba(255,255,255,0.00);
    }
    label > input:checked + img{ /* (RADIO CHECKED) IMAGE STYLES */
        background-color:rgba(255,255,255,0.25);
    }
<div class="container">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        
        <div class="follower" id="flyme"></div>

        <div class="string">String Position Marker</div>
        
<!-- KITE IMAGE SELECTOR -->
        <div class="kite-selector">
            <h3 style="margin:10px;">Select your kite:</h3>
            <form>
                <div>
                    <label><input type="radio" name="kite" value="https://i.imgur.com/i4xmPPt.png" checked onClick="pickKite(this.value);"><img src="https://i.imgur.com/i4xmPPt.png" style="max-height:50px;" alt="kite1"></label>
                </div>
                <div>
                    <label><input type="radio" name="kite" value="https://i.imgur.com/iXRQF77.png" onClick="pickKite(this.value);"><img src="https://i.imgur.com/iXRQF77.png" style="max-height:50px;" alt="kite2"></label>
                </div>
                <div>
                    <label><input type="radio" name="kite" value="https://i.imgur.com/L3IA1hX.png" onClick="pickKite(this.value);"><img src="https://i.imgur.com/L3IA1hX.png" style="max-height:50px;" alt="kite3"></label>
                </div>
            </form>
        </div>
<!-- END KITE IMAGE SELECTOR -->
</div>

0 个答案:

没有答案