开发者

In jQuery, how to set the position of a element in percentage?

开发者 https://www.devze.com 2023-04-11 12:27 出处:网络
How to get the click event position in percentage and how to pass this value to element via jQuery? When I pass via the .css() method, it pass always as pixels.

How to get the click event position in percentage and how to pass this value to element via jQuery? When I pass via the .css() method, it pass always as pixels.

The events e.pageX and e.PageY return the values in pixels. I can calculate the percentage, but if there is some way to get in percentage, I will use it.

The main problem is passing the values as percentag开发者_运维问答e to the element.


There's no pre-calculated percentage property.

$('element').click(function(e){
    var percent = e.pageX / $(window).width() * 100;
    $(this).css('left',percent + '%');
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号