将可拖动对象移到屏幕顶部或Div? [英] Move draggable objects to top of the screen or Div?

查看:55
本文介绍了将可拖动对象移到屏幕顶部或Div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在div内有可拖动的对象.我试图弄清楚如何允许用户单击按钮并将对象移动到屏幕上的确切位置(即使他们已将对象拖动到新位置)

I have draggable objects inside of a div. I'm trying to figure out how to allow a user to click a button and move the objects to an exact point on the screen (Even if they've dragged the object to a new location)

有什么方法可以将对象设置为屏幕顶部的动画,而不管对象从何处开始? (请参阅工作中的JSFiddle)谢谢!

Any way to animate an object to the top of the screen regardless of where the object starts? (See working JSFiddle) Thanks!

我一直在尝试下面的代码,但无法使其正常工作.

I've been trying the code below but I cannot get it to work correctly.

var json = [

           {'x' : '200' , 'y' : '200'},

            ];


    function initPage() {
        $.each(json, function() {
            $("#point").animate({
                left: this.x,
                top: this.y
            },
            "linear");
        });
    }

https://jsfiddle.net/unppstma/15/

推荐答案

将其他函数添加到按钮上的onclick事件中,如下所示:

Add another function to the onclick event on your button, with something like this:

function moveToTop(){
  $('#draggable1, #draggable2').animate({
    "position": "absolute",
    "top" : "0"
  })
}

这篇关于将可拖动对象移到屏幕顶部或Div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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