看看我的例子或第二个。也许会启发你。我的代码中有很多混乱;)
sandbox.zije.sk/js/index.php的内容:
<html>
<head>
<title>JS</title>
<script type="text/javascript" src="./jquery.js"></script>
<style>
body{ padding:0; margin:0; }
div.rect{ float:left; }
div.wrapper{ width:900px; height:400px; background: red; }
</style>
</head>
<body>
<?php foreach(range(1,12) as $v) echo '<div class="rect" style="background:#'.rc().'"></div>'; ?>
</body>
</html>
<script type="text/javascript">
function mod(a, n) {
return a - (n * Math.floor(a/n));
}
function adjust(c){
var h = window.innerHeight;
var w = window.innerWidth;
var ss = w>h?h:w;
var ls = w>h?w:h;
var cnt = $(c).length;
var i = 1;
var step = 0.01;
var box_strana;
var box_obsah;
var wrapper_obsah;
var wrapper_obsah_ef;
var limit = 5000;
var index = 0;
while(true){
if(index++>limit) break;
box_strana = ss/i;
box_obsah = box_strana*box_strana;
wrapper_obsah = h*w;
wrapper_obsah_ef = (h-mod(h,box_strana))*(w-mod(w,box_strana));
if((cnt*box_obsah)<=wrapper_obsah_ef) break;
i = parseFloat(parseFloat(i) + parseFloat(step));
}
console.log(h);
console.log(box_strana);
console.log(parseInt(box_strana));
console.log(mod(h,parseInt(box_strana)));
console.log(mod(h,parseInt(box_strana)));
console.log(parseInt(mod(h,parseInt(box_strana))/parseInt(h/parseInt(box_strana))));
$(c).width(parseInt(box_strana)).height(parseInt(box_strana))
.css('paddingRight',parseInt(mod(w,parseInt(box_strana))/parseInt(w/parseInt(box_strana))))
.css('paddingBottom',parseInt(mod(h,parseInt(box_strana))/parseInt(h/parseInt(box_strana))));
};
$(document).ready(function(){
adjust('.rect');
});
</script>
<?php
function rc(){
mt_srand((double)microtime()*1000000);
$c = '';
while(strlen($c)<6){
$c .= sprintf("%02X", mt_rand(0, 255));
}
return $c;
}
?>
sandbox.zije.sk/js/more.php的内容:
<html>
<head>
<title>JS</title>
<script type="text/javascript" src="./jquery.js"></script>
<style>
body{ padding:0; margin:0; }
div.rect{ float:left; }
div.wrapper{ width:900px; height:400px; background: red; }
</style>
</head>
<body>
<?php foreach(range(1,600) as $v) echo '<div class="rect" style="background:#'.rc().'"></div>'; ?>
</body>
</html>
<script type="text/javascript">
function mod(a, n) {
return a - (n * Math.floor(a/n));
}
function adjust(c){
var h = window.innerHeight;
var w = window.innerWidth;
var ss = w>h?h:w;
var ls = w>h?w:h;
var cnt = $(c).length;
var i = 1;
var step = 0.01;
var box_strana=1;
var box_obsah;
var wrapper_obsah;
var wrapper_obsah_ef;
var limit = 5000;
var index = 0;
while(true){
if(index++>limit) break;
box_obsah = box_strana*box_strana;
wrapper_obsah = h*w;
// wrapper_obsah_ef = (h-mod(h,box_strana))*(w-mod(w,box_strana));
wrapper_obsah_ef = parseInt((h-mod(h,box_strana))*(w-mod(w,box_strana)));
if(parseInt(cnt*box_obsah)>wrapper_obsah_ef){ box_strana--; break; }
// console.log()
box_strana++;
}
$(c).width(parseInt(box_strana)).height(parseInt(box_strana))
.css('paddingRight',parseInt(mod(w,parseInt(box_strana))/parseInt(w/parseInt(box_strana))))
.css('paddingBottom',parseInt(mod(h,parseInt(box_strana))/parseInt(h/parseInt(box_strana))));
};
$(document).ready(function(){
adjust('.rect');
//$(window).on('resize', function(){ adjust('.rect') });
});
</script>
<?php
function rc(){
mt_srand((double)microtime()*1000000);
$c = '';
while(strlen($c)<6){
$c .= sprintf("%02X", mt_rand(0, 255));
}
return $c;
}
?>