我知道以像素为单位你可以做这种事情,但如果使用百分比,则不会返回任何内容。如果我的 CSS 使用百分比,我该如何处理?我基本上需要查看具有特定值的 div,然后触发脚本的变体。
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');
}
以这种方式获取宽度将始终以像素为单位返回它,无论您如何设置它,因为它是计算值 https://developer.mozilla.org/en-US/docs/Web/CSS/computed_value。同样适用于.width()
。您需要根据其父级的宽度计算出宽度。
像下面这样的事情会做:
Math.floor(($el.width()/$el.parent().width())*100)+"%"
您甚至可以将其放入您自己的插件中:
$.fn.widthPerc = function(){
var parent = this.parent();
return ~~((this.width()/parent.width())*100)+"%";
}
被称为:
if ($(".modPopUp").widthPerc() == "32%" ){
JSFiddle http://jsfiddle.net/LR7Bg/1/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)