gpt4 book ai didi

javascript - 将无序列表扩展到浏览器宽度 (html/css)

转载 作者:行者123 更新时间:2023-11-28 02:12:53 24 4
gpt4 key购买 nike

我想要做的是有一个显示产品的 ul 列表。宽度是窗口的 100%。因此,浏览器窗口越宽,用户每行看到的产品就越多。随着浏览器变小,每行的产品越来越少。我遇到的问题是,如果窗口边缘之间的空间略小于产品容器,我会在浏览器右侧得到这个空白,我想要的是该行向边缘均匀间隔如果它不能容纳该行的其他产品。

这是我的代码:

<!DOCTYPE HTML>


<html>
<head>
<meta charset="UTF-8" />
</head>

<body>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script>
$(window).resize(function() {
var windowWidth = $('#prodGrid').width();
var prodGridWidth = $('#prodGrid li').width();
var numOfUnits = windowWidth/prodGridWidth;
var remainder = windowWidth%prodGridWidth;
var widthWithRemainder = prodGridWidth+remainder + "px";
if(numOfUnits > 5 && numOfUnits < 7)
{
$('#prodGrid li').css('min-width',widthWithRemainder);
}
});
</script>
<style type="text/css">
#prodGrid {
list-style: none;
margin: 0;
padding: 0;
display: inline;
}

#prodGrid li {
float: left;
margin: 0;
padding: 0;
min-width: 165px;

}

.prodGridWidth {
width: 10%;
}

.searchImageContainer {
height: 350px;
width: 140px;
}

.right {
width: 100%;
}
</style>




<table id="searchContainerTable">
<tr>
<td class="right">
<ul id="prodGrid">


<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>



<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>



<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>



<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>



<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>



<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>



<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>



<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>



<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>



<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>



<li class="prodGridWidth">
<div class="searchImageContainer">
<div id="loadarea" class="image">

<img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRm4NX4YZe6OUhOB-0xT0rOOJ-iiYf3fO1x1dW0FW4dXlu5UPQO" border="0" alt="Item" width="115" height="174" />
</div>
<div class="swatch">
</div>
<div class="productName"><a class="productName" href="">Item</a></div>
<span class="price">Price: </span><span class="priceRange">$369 - $659</span>
<div class="ratings"><div class="rating">Rating: </div><div class="starRating35"></div></div>

<div id="clear"></div>
</div>
</li>

</ul>
</td>
</tr>
</table>
</body>

</html>

让您了解我正在尝试做什么:http://www.overstock.com/Home-Garden/Art-Gallery/216/dept.html

您可以让您的浏览器更细/更宽,并查看行的行为方式(我正在尝试做的),然后将其与我的代码进行比较。

最佳答案

尝试内部调整大小处理程序

var windowWidth = $('#prodGrid').width();
var prodGridWidth = 165 + 5;
var numOfUnits = windowWidth / prodGridWidth;
var width = windowWidth / Math.floor(numOfUnits)
$('#prodGrid li').css('width', width - 5 + 'px');

有时你可以摆脱 display: inline-block 和 text-align: justify, like

<html>
<head>
<style>
.products { text-align: justify; height: 54px; overflow: hidden }
.product { display: inline-block; width: 50px; height: 50px; border: 1px solid blue; margin: 1px }
</style>
</head>
<body>
<div class="products">
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
</div>
</body>
</html>

关于javascript - 将无序列表扩展到浏览器宽度 (html/css),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7152986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com