你为什么用这个javascript?可以用纯CSS来实现:
#gradient #wrapper #camboxs .cambox:nth-child(5n):not(#gradient #wrapper #footer .box #camboxs .combox)
{
margin-right: 0;
}
然后使用媒体查询:
@media screen and (max-width: 980px), projection and (max-width: 980px)
{
/* first undo the general styles */
#gradient #wrapper #camboxs .cambox:nth-child(5n):not(#gradient #wrapper #footer .box #camboxs .combox)
{
margin-right: 10px; /* replace with the original margin */
}
#gradient #wrapper #camboxs .cambox:nth-child(2n):not(#gradient #wrapper #footer .box #camboxs .combox)
{
margin-right: 0;
}
}
像安迪的建议,它可以用较少的方式完成的代码。
和回退:要在窗口大小调整反应,使用与jQuery(未经测试)以下:
$(window).resize(function() {
if ($(window).width() <= 980) {
$("#gradient #wrapper #camboxs .cambox:nth-child(5n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","");
$("#gradient #wrapper #camboxs .cambox:nth-child(2n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","0");
} else {
$("#gradient #wrapper #camboxs .cambox:nth-child(2n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","");
$("#gradient #wrapper #camboxs .cambox:nth-child(5n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","0");
}
});
示例代码:http://twitter.github.com/bootstrap/assets/css/bootstrap-responsive.css – JasonWoof 2013-03-18 20:21:31
您的选择器真的很复杂且过于冗长。你为什么不使用$('#camboxs .cambox:n-child(5n)')。 ID是独一无二的,除了使其更快以外,它没有什么不同。 – Andy 2013-03-18 20:21:57