多个jQuery UI元素的X和Y位置 [英] X and Y Positions of multiple jQuery UI Elements

查看:107
本文介绍了多个jQuery UI元素的X和Y位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im试图使用jQuery UI收集多个可拖动元素的位置.

im trying to collect the positions of multiple draggable elements using jQuery UI.

目前,我可以获得一个元素的位置,但是当我在另一个元素上移动时,两个位置都会改变.

At the moment I can get the position of one element, but when I move around another, both positions change.

请有人可以帮助我获得多个可拖动项目的单独位置.

Please can someone assist me in getting the separate positions of multiple draggable items.

http://codepen.io/anon/pen/nLGIl

HTML

<div class="dragThis" id="box-1"style="top: 100px; left: 50px;" >
  <h2>Test 1</h2>
  <p>This is a test</p>
  <ul>
    <li class="posX"></li>
    <li class="posY"></li>
  </ul>
</div>

<div class="dragThis" id="box-1" style="top: 50px; left: 100px;" >
  <h2>Test 2</h2>
  <p>This is a test</p>
  <ul>
    <li class="posX"></li>
    <li class="posY"></li>
  </ul>
</div>

CSS

.dragThis {
    min-width: 50px;
    max-width: 300px;
    min-height: 50px;
    max-height: 400px;
    overflow: auto;
    padding: 10px;

    background: #efefef;
    border: 3px solid #ccc;
    border-radius: 10px;

    display: inline-block;

    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 0;
}

.dragThis h2 {
    font-size: 20px;
    margin: 0;
    padding: 0;
}

.dragThis ul {
    list-style: none;
    margin: 0;
    padding: 0;
}

.top {z-index: 2; position: relative}
.bottom {z-index: 1; position: relative}

JS

$(document).ready(function() {
    var a = 3;

    $('.dragThis').draggable(
    {
        start: function(){
            $(this).css("z-index", a++);
        },
        drag: function(){
            var offset = $(this).offset();
            var xPos = offset.left;
            var yPos = offset.top;
            $('.posX').text('x: ' + xPos);
            $('.posY').text('y: ' + yPos);
        }
    });

    $('.dragThis').click(function(){
      $(this).addClass('top').removeClass('bottom');

        $(this).siblings().removeClass('top').addClass('bottom');
        $(this).css("z-index", a++);
    });

});

推荐答案

首先,Double ID不好..box-1和box-2更好:

First of all, Double ID's are not good.. box-1 and box-2 are better:

您正在将所有元素与类'.posX'匹配. 试试这个:

You are matching all elements with class '.posX' etc. Try this:

$(document).ready(function() {
    var a = 3;

    $('.dragThis').draggable(
    {
        start: function(){
            $(this).css("z-index", a++);
        },
        drag: function(){
            var offset = $(this).offset();
            var xPos = offset.left;
            var yPos = offset.top;
            $(this).find('.posX').text('x: ' + xPos);
            $(this).find('.posY').text('y: ' + yPos);
        }
    });

    $('.dragThis').click(function(){
      $(this).addClass('top').removeClass('bottom');

        $(this).siblings().removeClass('top').addClass('bottom');
        $(this).css("z-index", a++);
    });

});

这篇关于多个jQuery UI元素的X和Y位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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