Jquery ,获取元素的位置()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19728340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:30:05  来源:igfitidea点击:

Jquery , get position().right of an element?

jqueryposition

提问by gregdevs

So I am working on having a draggable image.

所以我正在努力制作一个可拖动的图像。

very simply, When you drag the image to the left or right side at a specific position, I want the image to snap back to the left or right side.

很简单,当您将图像在特定位置的左侧或右侧拖动时,我希望图像重新回到左侧或右侧。

In the fiddle below, I have the functionality for the left side..but Im not quite sure what the best approach is for determining the right side since Jquery only allows top and left properties.

在下面的小提琴中,我有左侧的功能......但我不太确定确定右侧的最佳方法是什么,因为 Jquery 只允许 top 和 left 属性。

consider this code:

考虑这个代码:

$(function() {
$(".headerimage").css('cursor', 's-resize');
var y1 = $('.picturecontainer').height();
var y2 = $('.headerimage').height();
$(".headerimage").draggable({
    scroll: false,
    drag: function(event, ui) {
        if (ui.position.left >= 200) {
            ui.position.left = 0;
        }
    },
    stop: function(event, ui) {
        //####
    }
});

FIDDLE

小提琴

采纳答案by Jonathan Marzullo

what about this

那这个呢

drag: function(event, ui) {
       if (ui.position.left >= 200 || ui.position.left <= -300) {
               ui.position.left = 0;
       }
},

http://jsfiddle.net/nk7wc/10/

http://jsfiddle.net/nk7wc/10/

$(function() {
     $(".headerimage").css('cursor', 's-resize');
     var y1 = $('.picturecontainer').height();
     var y2 = $('.headerimage').height();
     $(".headerimage").draggable({
          scroll: false,
          drag: function(event, ui) {
                if (ui.position.left >= 200 || ui.position.left <= -300) {
                      ui.position.left = 0;
                }
          },
          stop: function(event, ui) {
              //####
          }
     });
});

checking against negative value of position.left

检查 position.left 的负值

回答by Chad Kuehn

To get the position to the right of an element I use this jQuery:

要获得元素右侧的位置,我使用了这个 jQuery:

var target = $("selector");
var result = target.position().left + target.width();

Make sure the element is fully rendered when you are getting this value. Otherwise the results could be off.

确保在获取此值时完全呈现元素。否则结果可能会关闭。

回答by Nikola Mitic

What about this one:

这个如何:

var rightOffset = $('.parent').width() - ( $(target).position().left +  $(target).width() )

note: I believe that this shoudn't work if you have margins and border around target element, not sure if that could be fixed with box-sizing: border-box

注意:我相信如果目标元素周围有边距和边框,这应该不起作用,不确定是否可以使用 box-sizing: border-box

回答by Huy Nguy?n

to get position right=0. Set left = leftpositionwithout margin css is set

获得位置right=0。Set left = leftposition没有边距 css 被设置

var leftposition = $(parent).width()-$(child).width();

回答by Mohamad Hamouday

This work for me:

这对我有用:

(function($) {
    $.fn.Right = function() 
    {
        return this.parent().outerWidth() - this.position().left + this.outerWidth();
    };
 })(jQuery);

To use it:

要使用它:

$("selector").Right();

回答by Matt

Do position left + width.

做左+宽的位置。

$(ui).position().left + $(ui).width();