获取px-JQuery中的最高值

Getting top value in px - JQuery

本文关键字:最高值 px-JQuery 获取      更新时间:2023-09-26

由于某些原因,当使用JQuery将div附加到网页中时,默认使用;顶部div上50%的百分比。当使用JQuery获取top值时,我得到157.6px?实际上,它应该在400px附近。

我不知道为什么会这样?任何见解都将不胜感激!

JQuery:

...code
//get last x and y cordinates
    if(fontActive == 1)
    {
        var x = $("#draggable").css('left');
        var y =$("#draggable").css('top');
    }
    // default remove old
    $(".customize-Container #draggable").remove();
    //get values
    var text = $("#fontEnter").val();
    var current = $(".activeText a div").attr('id');
    //create a canvas for image converting
    $(".customize-Container").append("<div id='draggable'><canvas id='"+current+"'></canvas></div>");
    //get x and y cordinates if fontActive not 1
    if(fontActive != 1)
    {
        var x = $("#draggable").css('left');
        var y =$("#draggable").css('top');
    }
...code

请注意,在上面,在第一次启动fontActive = 0时,一旦已经运行了fontActive = 1. 的值

也像我说的#draggable有css样式,top的值默认为50%;

我很确定这是因为.css('top')returning the top value as relative to the closest parent element with a position defined

要获得您想要的价值,很可能是

$('#draggable').position().top +'px' 

其中您必须附加px值,因为它将返回一个整数。