jQuery,如果宽度等于百分比 [英] jQuery if width is equal to percentage
本文介绍了jQuery,如果宽度等于百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道以像素为单位,您可以执行这种操作,但是以百分比表示,不会返回任何内容.如果我的CSS使用百分比,该如何处理?我基本上需要查看具有特定值的div,然后启动脚本的变体.
I know in pixels you can do this sort of thing but with percentages, nothing is being returned. How would I go about this if my CSS is using percentages? I basically need to look at the div having a specific value and then firing off a variant of my script.
jQuery:
if ($(".modPopUp").css("width") == "32%" ){
console.log("32%");
$(".modPopUp:nth-child(3n-2)").css('margin-left', '0');
}
else if ($(".modPopUp").css("width") == "49%" ){
console.log("49%");
$(".modPopUp:nth-child(2n)").css('margin-left', '0');
}
推荐答案
以这种方式获取宽度将始终以像素为单位返回该宽度,无论您如何设置它,因为它是
Getting the width that way will always return it in pixels, no matter how you set it, because it is the computed value. The same goes for .width()
. You'd need to work out the width against that of it's parent.
类似下面的事情:
Math.floor(($el.width()/$el.parent().width())*100)+"%"
您甚至可以将其放在您自己的插件中:
You could even put that in your own plugin:
$.fn.widthPerc = function(){
var parent = this.parent();
return ~~((this.width()/parent.width())*100)+"%";
}
要这样称呼:
if ($(".modPopUp").widthPerc() == "32%" ){
这篇关于jQuery,如果宽度等于百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文