本文介绍了使用jQuery获取百分比CSS位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用jQuery获取元素的CSS(顶部和左侧):
I'm trying to get an element's CSS (top and left) with jQuery:
$(element).css('top');
但是我得到像素,而不是12%。
如何获得百分比?
but instead of "12%" like it should be, I get the pixels.
How do I get the percent?
HTML:
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<style type="text/css">
.parWrapper {
position:absolute;
top: 40%
}
</style>
</head>
<body>
<div>
<div id="crap" class="parWrapper" style="left:50%">
Wrap1
</div>
<div class="parWrapper">
Wrap2
</div>
<div class="parWrapper">
Wrap3
</div>
<div class="parWrapper">
Wrap4
</div>
<div class="parWrapper">
Wrap5
</div>
</div>
</body>
推荐答案
我刚刚遇到这个问题,
I just encountered this myself and thought it weird, too.
而不是:
$(element).css('top');
我刚刚:
element.style.top
没有jQuery,您所创建的类型(百分比,ems等)
No jQuery and gives you the actual value in the type you made it (percent, ems, etc.)
这篇关于使用jQuery获取百分比CSS位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!