Javascript 根据屏幕宽度加载缩略图图像
我希望能够加载一个页面上的缩略图图像数量取决于页面的宽度。加载的图像数量取决于页面的宽度Javascript 根据屏幕宽度加载缩略图图像,javascript,html,css,Javascript,Html,Css,我希望能够加载一个页面上的缩略图图像数量取决于页面的宽度。加载的图像数量取决于页面的宽度 <script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script> <style type="text/css"> #dv_frame { width: 100%; /*border: 1px solid #cccccc;*/ te
<script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
<style type="text/css">
#dv_frame {
width: 100%;
/*border: 1px solid #cccccc;*/
text-align: center;
height: 110px;
position: relative;
}
#dv_images {
/*border: solid 1px blue;*/
height: 100px;
margin: auto;
}
.myImage{
width: 100px;
height: 100px;
border: solid 1px red;
float: left;
margin-right: 5px;
margin-left: 5px;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
var container = $("#dv_frame")
var tWidth = 0
var width = container.width()
for (c = 0; c < 100; c++) {
tWidth = tWidth + 110
if (tWidth > (width - 10)) {
$("#dv_images").css("width", (tWidth - 80))
break;
}
addSquare()
}
function addSquare() {
$("#dv_images").append("<div class='myImage'></div>")
}
});
</script>
</head>
<body>
<div id="dv_frame"><div id="dv_images"></div></div>
</body>
</html>
图像(100 x 100)将以并排方式加载,并占据整个屏幕宽度
<script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
<style type="text/css">
#dv_frame {
width: 100%;
/*border: 1px solid #cccccc;*/
text-align: center;
height: 110px;
position: relative;
}
#dv_images {
/*border: solid 1px blue;*/
height: 100px;
margin: auto;
}
.myImage{
width: 100px;
height: 100px;
border: solid 1px red;
float: left;
margin-right: 5px;
margin-left: 5px;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
var container = $("#dv_frame")
var tWidth = 0
var width = container.width()
for (c = 0; c < 100; c++) {
tWidth = tWidth + 110
if (tWidth > (width - 10)) {
$("#dv_images").css("width", (tWidth - 80))
break;
}
addSquare()
}
function addSquare() {
$("#dv_images").append("<div class='myImage'></div>")
}
});
</script>
</head>
<body>
<div id="dv_frame"><div id="dv_images"></div></div>
</body>
</html>
实现这一目标的最佳方式是什么
<script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
<style type="text/css">
#dv_frame {
width: 100%;
/*border: 1px solid #cccccc;*/
text-align: center;
height: 110px;
position: relative;
}
#dv_images {
/*border: solid 1px blue;*/
height: 100px;
margin: auto;
}
.myImage{
width: 100px;
height: 100px;
border: solid 1px red;
float: left;
margin-right: 5px;
margin-left: 5px;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
var container = $("#dv_frame")
var tWidth = 0
var width = container.width()
for (c = 0; c < 100; c++) {
tWidth = tWidth + 110
if (tWidth > (width - 10)) {
$("#dv_images").css("width", (tWidth - 80))
break;
}
addSquare()
}
function addSquare() {
$("#dv_images").append("<div class='myImage'></div>")
}
});
</script>
</head>
<body>
<div id="dv_frame"><div id="dv_images"></div></div>
</body>
</html>
提前谢谢
<script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
<style type="text/css">
#dv_frame {
width: 100%;
/*border: 1px solid #cccccc;*/
text-align: center;
height: 110px;
position: relative;
}
#dv_images {
/*border: solid 1px blue;*/
height: 100px;
margin: auto;
}
.myImage{
width: 100px;
height: 100px;
border: solid 1px red;
float: left;
margin-right: 5px;
margin-left: 5px;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
var container = $("#dv_frame")
var tWidth = 0
var width = container.width()
for (c = 0; c < 100; c++) {
tWidth = tWidth + 110
if (tWidth > (width - 10)) {
$("#dv_images").css("width", (tWidth - 80))
break;
}
addSquare()
}
function addSquare() {
$("#dv_images").append("<div class='myImage'></div>")
}
});
</script>
</head>
<body>
<div id="dv_frame"><div id="dv_images"></div></div>
</body>
</html>
这就是我到目前为止的想法:-
<script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
<style type="text/css">
#dv_frame {
width: 100%;
/*border: 1px solid #cccccc;*/
text-align: center;
height: 110px;
position: relative;
}
#dv_images {
/*border: solid 1px blue;*/
height: 100px;
margin: auto;
}
.myImage{
width: 100px;
height: 100px;
border: solid 1px red;
float: left;
margin-right: 5px;
margin-left: 5px;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
var container = $("#dv_frame")
var tWidth = 0
var width = container.width()
for (c = 0; c < 100; c++) {
tWidth = tWidth + 110
if (tWidth > (width - 10)) {
$("#dv_images").css("width", (tWidth - 80))
break;
}
addSquare()
}
function addSquare() {
$("#dv_images").append("<div class='myImage'></div>")
}
});
</script>
</head>
<body>
<div id="dv_frame"><div id="dv_images"></div></div>
</body>
</html>
#DVU帧{
宽度:100%;
/*边框:1px实心#中交*/
文本对齐:居中;
高度:110px;
位置:相对位置;
}
#DVU图像{
/*边框:纯色1px蓝色*/
高度:100px;
保证金:自动;
}
.myImage{
宽度:100px;
高度:100px;
边框:实心1px红色;
浮动:左;
右边距:5px;
左边距:5px;
}
$(文档).ready(函数(){
变量容器=$(“#dv_帧”)
var tWidth=0
var width=container.width()
对于(c=0;c<100;c++){
tWidth=tWidth+110
如果(tWidth>(宽度-10)){
$(“#dv_图像”).css(“宽度”(tWidth-80))
打破
}
addSquare()
}
函数addSquare(){
$(“#dv_图像”)。附加(“”)
}
});
我不会每次添加图像并检查宽度,而是从一开始就计算您需要的图像数量。将它们添加到不在DOM中的虚拟div中,然后最后将其附加到容器中(以提高速度和效率)
<script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
<style type="text/css">
#dv_frame {
width: 100%;
/*border: 1px solid #cccccc;*/
text-align: center;
height: 110px;
position: relative;
}
#dv_images {
/*border: solid 1px blue;*/
height: 100px;
margin: auto;
}
.myImage{
width: 100px;
height: 100px;
border: solid 1px red;
float: left;
margin-right: 5px;
margin-left: 5px;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
var container = $("#dv_frame")
var tWidth = 0
var width = container.width()
for (c = 0; c < 100; c++) {
tWidth = tWidth + 110
if (tWidth > (width - 10)) {
$("#dv_images").css("width", (tWidth - 80))
break;
}
addSquare()
}
function addSquare() {
$("#dv_images").append("<div class='myImage'></div>")
}
});
</script>
</head>
<body>
<div id="dv_frame"><div id="dv_images"></div></div>
</body>
</html>
var$container=$(“#dv_帧”),
imageWidth=110,//包括边距和填充
imagesNeeded=Math.floor($container.width()/imageWidth),//向下舍入,width/imageWidth
i、 //迭代器
$foo=$('');//空货柜组
对于(i=0;i
示例:获取窗口的宽度,除以100,就得到了需要请求的图像数。我相信,如果你提供一些代码,人们会很乐意帮助你,但似乎你只是想有人为你写这篇文章?很公平,我已经输入了我到目前为止所拥有的。我只是想知道是否有更好的方法
<script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
<style type="text/css">
#dv_frame {
width: 100%;
/*border: 1px solid #cccccc;*/
text-align: center;
height: 110px;
position: relative;
}
#dv_images {
/*border: solid 1px blue;*/
height: 100px;
margin: auto;
}
.myImage{
width: 100px;
height: 100px;
border: solid 1px red;
float: left;
margin-right: 5px;
margin-left: 5px;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
var container = $("#dv_frame")
var tWidth = 0
var width = container.width()
for (c = 0; c < 100; c++) {
tWidth = tWidth + 110
if (tWidth > (width - 10)) {
$("#dv_images").css("width", (tWidth - 80))
break;
}
addSquare()
}
function addSquare() {
$("#dv_images").append("<div class='myImage'></div>")
}
});
</script>
</head>
<body>
<div id="dv_frame"><div id="dv_images"></div></div>
</body>
</html>