我已经制作了一个加载背景图像的包装器,并且在for循环的帮助下出现了许多块.块的宽度取决于窗口宽度除以10.现在我需要根据窗口的高度使块限制并调整窗口的大小.
工作示例jsfiddle
[1]: [http://jsfiddle.net/RaVDJ/1/][1]
解决方法
你想要这样的东西
http://jsfiddle.net/slash197/RaVDJ/5/吗?
CSS
html { height: 100%; margin: 0px; padding: 0px; } body { background: url(http://wallpaperfast.com/wp-content/uploads/2013/05/Beautiful-Beach-Wallpaper.jpg) no-repeat; background-size: auto 100%; height: 100%; margin: 0px; padding: 0px; } .wrap { margin: 0px; width: 100%; height: 100%; } .wrap div { float: left; background-color: #cc0000; opacity: 0.5; cursor: pointer; margin: 1px; transition: opacity 0.3s linear; } .wrap div:hover { opacity: 0; }
HTML
<div class="wrap"></div>
JS
$(document).ready(function() { addBoxes(); }); $(window).resize(addBoxes); function addBoxes() { $('.wrap').html(""); var size = Math.floor($('.wrap').width()/10); var sizeInner = size - 2; var tw = Math.floor($('.wrap').width()/size); var th = Math.floor($('.wrap').height()/size); for (var i = 0; i < th; i++) { for (var j = 0; j < tw; j++) { $('.wrap').append('<div style="width: ' + sizeInner + 'px; height: ' + sizeInner + 'px;"></div>'); } } }