在圆弧中拖动元素的最简单方法是什么? [英] What is the easiest way to drag an element in an arc?

查看:97
本文介绍了在圆弧中拖动元素的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的程序的代码。 jQuery拖动太阳是在底部。太阳是一个形象。什么是最简单的方法拖动太阳在一个弧?

This is my code for my "program". The jQuery to drag the sun is at the very bottom. The sun is an image. What is the easiest way to drag the sun in an arc?

    <style>
        /* Colors */
        body {
             /*clouds*/
             background: url(http://i.imgur.com/aZty7Mq.png);
             animation: mymove 4s linear infinite;
             -webkit-animation: mymove 4s linear infinite;
             -moz-animation: mymove 4s linear infinite;
        }
        /*cloud animation*/
        @keyframes mymove {
            0% { background-position: 0 0; }
            50% { background-position: 40% 0; }
        }
        #sun {
            position: absolute;
            width: 300px;
            height: 300px;
            top: 30%;
            left: 10%;
        }
    </style>

    <html>
    <body>
            <img id="sun" src="http://i.imgur.com/DGkZYZQ.png">
    </body>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>

    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script>
    /*sun dragged in a line*/
        $(document).ready(function() {
            $("#sun").draggable({axis: "x"});
        });
    </script>
    </html>


推荐答案

我想我有一个很好的电弧运动在这里,它是基于一个圆公式。您可以通过更改公式中的变量来更改圆弧。告诉我你的想法。

I think I've got a pretty nice arcing motion going on here, it's based on a circle formula. You can change the arc by changing the variables in the formula. Tell me what you think.

现场演示

$("#sun").draggable({
    axis: "x",
    drag: function() {
        var xpos = parseFloat(this.style.left, 10) - 200;
        var ypos = Math.sqrt(300 * 300 - xpos * xpos) * 0.75;
        this.style["margin-top"] = (-ypos) + "px";
    }
});

/* Colors */
 body {
    /*clouds*/
    background: url(https://i.imgur.com/aZty7Mq.png);
    animation: mymove 4s linear infinite;
    -webkit-animation: mymove 4s linear infinite;
    -moz-animation: mymove 4s linear infinite;
    overflow: hidden;
}
/*cloud animation*/
 @keyframes mymove {
    0% {
        background-position: 0 0;
    }
    50% {
        background-position: 40% 0;
    }
}
#sun {
    position: absolute;
    width: 300px;
    height: 300px;
    top: 30%;
    left: 10%;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>

<img id="sun" src="https://i.imgur.com/DGkZYZQ.png">

JSFiddle版本: https://jsfiddle.net/on1z6q0z/3/

JSFiddle Version: https://jsfiddle.net/on1z6q0z/3/

这篇关于在圆弧中拖动元素的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆