To get the offset values from jquery

前端 未结 3 1181
孤城傲影
孤城傲影 2021-02-04 19:23

How do i find the offset value of an image, which is enclosed with in a table. The table consist of lot of images. i want to get the offset - left, right, top, bottom for all th

相关标签:
3条回答
  • 2021-02-04 19:41

    Please read the jQuery documentation. These functions are all very clearly spelled out.

    $("#yourImg").bind("mousemove", function(e) {
        var $this = $(this);
        var imgLeft = e.pageX - $this.offset().left;
        var imgTop = e.pageY - $this.offset().top;
        var imgBottom = $this.offset().top + $this.height() - e.pageY;
        var imgRight = $this.offset().left + $this.width() - e.pageX;
    
        // do more stuff here
    }
    
    0 讨论(0)
  • 2021-02-04 19:56
    var elem = $("your_element");
    var offset = elem.offset();
    var leftValue = offset.left;
    var topValue =  offset.top;
    

    To get right and bottom values add width and height values to left and top..

    0 讨论(0)
  • 2021-02-04 19:58

    I wrote a little plugin that does just this.

    It gets the position of the element that you want; left, right, top or bottom; relative to it's parent or to the HTML document.

    You can find it on my github: https://github.com/Ridle/jQuery-getOffsets

    0 讨论(0)
提交回复
热议问题