javascript - 达到用户的特定页面宽度时如何停止追加?

标签 javascript jquery html css

如何停止附加 <div>是什么时候到达用户页面的末尾?我是否必须构建一个固定宽度的 div 作为可以找到浏览器默认窗口设置的容器?

$(document).ready(function () {

var color_array = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"];
var randColor = color_array[Math.floor(Math.random() * color_array.length)];

setInterval(function() {
        for (i = 1; i <= 5; i++) {
            $("#box").append("<div style='background-color:" + randColor + "; height:20px; width:20px; float:left;'></div>"); 
            randColor = color_array[Math.floor(Math.random() * color_array.length)]; }
        }, .000001);
});

最佳答案

(box = document.getElementById('box')).style.display = 'table'; //Shrink-wrap
while (box.offsetWidth < innerWidth) {
    box.innerHTML += "<div style='background-color:" + color_array[Math.floor(Math.random() * color_array.length)] + "; height:20px; width:20px; float:left;'></div>"; //No need for a randColor var
}
box.style.display = ''; //Get rid of inline styles

关于javascript - 达到用户的特定页面宽度时如何停止追加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22021806/

相关文章:

javascript - 从特定 div 过滤并执行脚本

jQuery 使用两个按钮在三种不同样式之间更改单个 div 的类

javascript - AngularJS - 引用 $route 服务的导航 Controller

html - 柔性网格问题嵌套多个柔性方向组合

javascript - 到达最后一个选项卡时提交多选项卡页面

javascript - lodash 的 "nth"函数有什么用?

javascript - 如何在 <div> 后面的 <div> 上锁定滚动

javascript - 查看:after pseudo class中WebElement是否有具体内容

javascript - 如何在页面顶部将多个div设置为 “position: fixed and top:0”

javascript - 用于删除所有出现的模式的正则表达式