如何获取div的坐标?

发布于 2024-10-19 12:08:17 字数 3589 浏览 2 评论 0原文

我使用下面的代码来获取删除元素的位置。我将位置保存到数据库中,以便当用户下次打开页面时,元素位于相同的位置。到目前为止它工作正常,但是当屏幕尺寸改变时,位置也会改变。例如,如果我使用大桌面屏幕,捕获图像位置,将其保存到数据库并在笔记本电脑[小屏幕硬本]中打开同一页面,则立场各有不同。

我想这是因为 JQuery 获取元素相对于页面的位置,而不是相对于我的容器“div”。那么如何获取div中元素的位置呢?

 $(document).ready(function() {
      //Counter
      counter = 0;
      //Make element draggable
      $("#fd img").draggable({
          helper: 'clone',
          containment: '#frame',
          //When first dragged
          stop: function(ev, ui) {

              var pos = $(ui.helper).offset();
              objName = "#clonediv" + counter
              $(objName).css({ "left": pos.left, "top": pos.top });
              $(objName).removeClass("drag");
              //When an existiung object is dragged

              var container = $('#frame');
              $(objName).draggable({
                  containment: container,
                  stop: function(ev, ui) {

                      var pos = $(ui.helper).offset();
                      //console.log($(this).attr("id"));
                      //console.log(pos.left)
                      //console.log(pos.top)
                  }
              });
          }
      });
      //Make element droppable
      $("#frame").droppable({

          drop: function(ev, ui) {

          var angle; 
              if (document.getElementById('angle').value != "") {
                  angle = document.getElementById('angle').value;
                  document.getElementById('angle').value = 0;
              }
              else {
                  angle = 0;
              }

调试器 if (ui.helper.attr('id').search(/drag[0-9]/) != -1) {

                  if (ui.helper.attr('id').indexOf("clonediv") != -1) {
                      var deleteimage = 0;
                      var id = ui.helper.attr('id').substring(8);
                      saveCoords(ui.absolutePosition.left, ui.absolutePosition.top, angle, id, document.getElementById("trafficID").value, deleteimage);

                  }
                  else {
                      var pos = $(ui.helper).offset();
                      counter++;
                      var element = $(ui.helper).clone();
                      //var element = element1.resizable();

                      element.addClass("tempclass");
                      $(this).append(element);
                      var deleteimage = 0;
                      element.attr("id", ui.helper.attr('id') + counter);

                      **saveCoords(ui.absolutePosition.left, ui.absolutePosition.top, angle, ui.helper.attr('id') + counter, document.getElementById("trafficID").value, deleteimage);**

                      $(".tempclass").attr("id", "clonediv" + ui.helper.attr('id') + counter);
                      $("#clonediv" + ui.helper.attr('id') + counter).removeClass("tempclass");

                      //$(".tempclass").attr("onclick",function(){ $(this).remove(););
                      //$("#clonediv" + counter).removeClass("tempclass");
                      //Get the dynamically item id

                      draggedNumber = ui.helper.attr('id').search(/drag([0-9])/);
                      itemDragged = "dragged" + RegExp.$1;

                      //console.log(itemDragged)
                      //alert('left' + pos.left + ',top' + pos.top + 'of item' + itemDragged);

                      $("#clonediv" + ui.helper.attr('id') + counter).addClass(itemDragged);
                  }



              }
          }
      });
      //Make the element resizable


  });

谢谢,

I am using the below code to get the position of dropped elements. I am saving the position to database,so that when the user opens the page next time, the elements are in the same position. It works fine so far, but when the screen size changes the position are changed.for example if i am using big desktop screen ,capture the image position ,save it to database and open the same page in laptop[small screen toughbook] ,the position are varied.

I guess this is bacause the JQuery gets the position of elements relative to page ,not to my container "div ". So how to get position of elements with in a div.

 $(document).ready(function() {
      //Counter
      counter = 0;
      //Make element draggable
      $("#fd img").draggable({
          helper: 'clone',
          containment: '#frame',
          //When first dragged
          stop: function(ev, ui) {

              var pos = $(ui.helper).offset();
              objName = "#clonediv" + counter
              $(objName).css({ "left": pos.left, "top": pos.top });
              $(objName).removeClass("drag");
              //When an existiung object is dragged

              var container = $('#frame');
              $(objName).draggable({
                  containment: container,
                  stop: function(ev, ui) {

                      var pos = $(ui.helper).offset();
                      //console.log($(this).attr("id"));
                      //console.log(pos.left)
                      //console.log(pos.top)
                  }
              });
          }
      });
      //Make element droppable
      $("#frame").droppable({

          drop: function(ev, ui) {

          var angle; 
              if (document.getElementById('angle').value != "") {
                  angle = document.getElementById('angle').value;
                  document.getElementById('angle').value = 0;
              }
              else {
                  angle = 0;
              }

debugger
if (ui.helper.attr('id').search(/drag[0-9]/) != -1) {

                  if (ui.helper.attr('id').indexOf("clonediv") != -1) {
                      var deleteimage = 0;
                      var id = ui.helper.attr('id').substring(8);
                      saveCoords(ui.absolutePosition.left, ui.absolutePosition.top, angle, id, document.getElementById("trafficID").value, deleteimage);

                  }
                  else {
                      var pos = $(ui.helper).offset();
                      counter++;
                      var element = $(ui.helper).clone();
                      //var element = element1.resizable();

                      element.addClass("tempclass");
                      $(this).append(element);
                      var deleteimage = 0;
                      element.attr("id", ui.helper.attr('id') + counter);

                      **saveCoords(ui.absolutePosition.left, ui.absolutePosition.top, angle, ui.helper.attr('id') + counter, document.getElementById("trafficID").value, deleteimage);**

                      $(".tempclass").attr("id", "clonediv" + ui.helper.attr('id') + counter);
                      $("#clonediv" + ui.helper.attr('id') + counter).removeClass("tempclass");

                      //$(".tempclass").attr("onclick",function(){ $(this).remove(););
                      //$("#clonediv" + counter).removeClass("tempclass");
                      //Get the dynamically item id

                      draggedNumber = ui.helper.attr('id').search(/drag([0-9])/);
                      itemDragged = "dragged" + RegExp.$1;

                      //console.log(itemDragged)
                      //alert('left' + pos.left + ',top' + pos.top + 'of item' + itemDragged);

                      $("#clonediv" + ui.helper.attr('id') + counter).addClass(itemDragged);
                  }



              }
          }
      });
      //Make the element resizable


  });

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-10-26 12:08:17

offset 方法为您提供相对于文档的位置,因此您可能需要 position 方法来代替,它为您提供相对于偏移量父级的位置:

var pos = $(ui.helper).position();

在您的代码中,您根本没有使用 pos 变量,这也是它不起作用的原因。

saveCoords(pos.left, pos.top, angle, ui.helper.attr('id') + counter);

The offset method gets you the position relative to the document, so you probably want the position method instead, that gives you the position relative to the offset parent:

var pos = $(ui.helper).position();

In your code you are not using the pos variable at all, which also is a reason that it doesn't work.

saveCoords(pos.left, pos.top, angle, ui.helper.attr('id') + counter);
只是在用心讲痛 2024-10-26 12:08:17

不知道你的代码到底是如何工作的,但我认为这可以解决问题:

saveCoords($(ui.helper).offsetLeft, $(ui.helper).offsetTop, angle, ui.helper.attr('id') + counter);

Don't know exactly how your code works, but I think this will do the trick:

saveCoords($(ui.helper).offsetLeft, $(ui.helper).offsetTop, angle, ui.helper.attr('id') + counter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文