我希望这个jQuery以%而不是以像素为单位给我排名

编程入门 行业动态 更新时间:2024-10-28 01:16:26
本文介绍了我希望这个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以%而不是以像素为单位给我排名

本文发布于:2023-07-19 09:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:给我   而不   像素   单位   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!