你可以使用一些CSS媒体查询来替代JavaScript:
@media (max-width:768px) { /*Extra small devices - Phones (<768px)*/
/*css here to show only the horizontal line for this size*/
}
@media (min-width:768px) { /*Small devices - Tablets (≥768px)*/
/*css here to show only the horizontal line for this size*/
}
@media (min-width:992px) { /*Medium devices - Desktops (≥992px)*/
/*css here to show only the horizontal line for this size*/
}
@media (min-width:1200px) { /*Large devices - Desktops (≥1200px)*/
/*css here to show only the horizontal line for this size*/
}
<!DOCTYPE html>
<html>
<head>
<title>Quick and dirty border demo</title>
<style>
div {width: 47%; float:left; border: 1px solid #333; margin:1em 5px}
div:nth-child(2n+1):before {
content:'';
border-bottom:1px solid green;
position:absolute;
display:block;
width: 100%;
margin-top: -1em;
}
</style>
</head>
<body>
<div>a</div>
<div>a</div>
<div>a</div>
<div>a</div>
<div>a</div>
<div>a</div>
<div>a</div>
<div>a</div>
<div>a</div>
</body>
</html>
对于每个媒体查询,使用不同的模式(例如3n+1)。
如果您不想在第一行上方有边框,请使用(2n+3)、(3n+4)...(xn+(x+1))。