简体   繁体   English

在JavaScript或JQuery中将百分比值转换为像素

[英]Converting Percent value to pixel in JavaScript or JQuery

I have a string value in percent unit that will be assign to height but before assigning I need to deduct another height which got by top() the output result is NaN , My code is as below: 我有一个以百分比为单位的字符串值,该字符串值将分配给高度,但是在分配之前,我需要减去top()得到的另一个高度,输出结果为NaN ,我的代码如下:

var valHeight = "50%";
var result = valHeight - $("#item").css("top");
$("#AnotherItem").height(result);

as valHeight is string and percent and height is pixel the result will be NaN . 由于valHeight是字符串,%和height是像素,结果将为NaN How may I solve that issue? 我该如何解决这个问题? the valHeight is percent but top value is pixel. valHeight是百分比,但最高值是像素。 I need to have my result as percent Let's clarify more: I want to use calc function of CSS and I guess the below code is correct: 我需要以百分比表示结果。让我们进一步阐明:我想使用CSS的calc函数,并且我猜下面的代码是正确的:

$('#AnotherItem').css('height', valHeight).css('height', '-='+itemOffsetTop);

the only problem is I want to use subtracted value in animate function. 唯一的问题是我想在动画函数中使用减值。

I see two issues with your code: 我发现您的代码有两个问题:

  1. valHeight is a string, not a number. valHeight是字符串,而不是数字。 It needs to be a number before using it in a math operation. 在数学运算中使用它之前,它必须是一个数字。
  2. It's also not clear where you're getting .top() from. 还不清楚您从哪里获得.top() Perhaps you meant to use .offset().top ? 也许您打算使用.offset().top

Example: 例:

var valHeight = 50;
var result = valHeight - $("#item").offset().top;
$("#AnotherItem").height(result + "px");

Now, you modified your question to use 50% and it doesn't really make sense. 现在,您将问题修改为使用50% ,这实际上没有任何意义。 If you want the result to be 50% of $("#item").offset().top , then you could use something like this: 如果您希望结果为$("#item").offset().top 50%,则可以使用以下方法:

var valHeight = 0.5;
var result = $("#item").offset().top * valHeight;
$("#AnotherItem").height(result + "px");

First 第一

valHeight is a string you need to convert that to number. valHeight是一个字符串,您需要将其转换为数字。

var varlHeight = parseInt("50px");

Second 第二

Your $("#item").top() is invalid, use this instead. 您的$("#item").top()无效,请改用它。

$("#item").offset().top

Putting them together 放在一起

var valHeight=parseInt("50px");
var result= valHeight - $("#item").offset().top;
$("#AnotherItem").height(result);

Update 更新资料

Since you've updated your post with a '50%' value. 由于您的帖子已更新为“ 50%”的值。 How about doing this kind of approach instead. 怎么做这种方法呢?

var valHeight="50%";
var itemOffsetTop = $("#item").offset().top;
$('#AnotherItem').css('height', valHeight).css('height', '-='+itemOffsetTop);
  1. You need to use .offset() as there is no method as .top() in jQuery 您需要使用.offset()因为没有方法.top() jQuery中

Get the current coordinates of the first element, or set the coordinates of every element, in the set of matched elements, relative to the document. 获取第一个元素的当前坐标,或设置匹配元素集中相对于文档的每个元素的坐标。

  1. valHeight should be a number valHeight应该是一个数字

Code

var valHeight = 50; //Or, parseInt("50px")
var result = valHeight - $("#item").offset().top;
$("#AnotherItem").height(result + "px");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM