我希望这个jQuery以%而不是以像素为单位给我排名 [英] I want this jquery to give me position in % not in pixels
本文介绍了我希望这个jQuery以%而不是以像素为单位给我排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我现在拥有的东西.这给了我绝对的像素位置
$(this).css({
"left" : ($(this).width() + 5) * (index % 4),
"top" : ($(this).height() + 5) * Math.floor(index / 4)
});
我希望它以相对于屏幕尺寸的百分比形式给出位置.
I want this to give the positions in percentage relative to the screen size.
请帮助.
推荐答案
jQuery不会返回以%表示的值.
Jquery will not return the value in %.
它将始终以px为单位返回.
It will always return in px.
因此,要获取以%为单位的值,您可以将值除以最高节点[dom中的第一个父节点]
So, to get the value in %, you can divide the value with either the top most node [1st parent in dom]
parseFloat($(this).width() / $('#idOfFirstParent').width()) * 100;
,如果您想获取有关DOM中第一个父级的值或按文档的宽度[此值将从屏幕的0,0开始].
, if you want to get the value in reference to the 1st parent in the DOM or by width of the document [this will start with 0,0 of your screen].
(parseFloat($(this).width() / $('window').width()) * 100).toFixed(2);
or
(parseFloat($(this).width() / $('document').width()) * 100).toFixed(2);
这篇关于我希望这个jQuery以%而不是以像素为单位给我排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文