最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In jQuery, how to set the position of a element in percentage? - Stack Overflow

programmeradmin2浏览0评论

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 percentage to the element.

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 percentage to the element.

Share Improve this question edited Oct 8, 2011 at 21:36 Renato Dinhani asked Oct 8, 2011 at 21:31 Renato DinhaniRenato Dinhani 36.8k58 gold badges140 silver badges202 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

There's no pre-calculated percentage property.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论