Get css top value as number not as string?

后端 未结 3 1526
长情又很酷
长情又很酷 2021-01-30 02:15

In jQuery you can get the top position relative to the parent as a number, but you can not get the css top value as a number if it was set in px.
Say I have th

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 02:29

    A jQuery plugin based on M4N's answer

    jQuery.fn.cssNumber = function(prop){
        var v = parseInt(this.css(prop),10);
        return isNaN(v) ? 0 : v;
    };
    

    So then you just use this method to get number values

    $("#logo").cssNumber("top")
    

提交回复
热议问题