简体   繁体   English

jQuery:在主体上附加计算所得的div

[英]jQuery:Append calculated div on body

I am calculating width and height of a box according to window's height and width and divided it by 12 to get 12 equal sized boxes on body. 我正在根据窗口的heightwidth计算一个盒子的宽度和高度,并将其除以12,得到12个相等大小的盒子。

I created this script to append box on body on click- 我创建了此脚本,以便在点击时在正文上附加框

$('body').append('<div class="box"></div>');
        $('.box').css('display','block');

This i how i calculated width of this box- 这是我如何计算此框的宽度

var width=$(window).width();
var span12= width/12;

And for height - 对于身高 -

 var height=$(window).height();
    var spanheight=height/12;

I want to append all the possible boxes on click event. 我想在点击事件上附加所有可能的框。 Is that possible? 那可能吗?

Fiddle 小提琴

You need to define css properties for box every time you create a new <div> , 每次创建新的<div> ,都需要为box定义css属性,
Because $('.box').css('display','block'); 因为$('.box').css('display','block'); will only make an effect to already loaded content. 只会影响已经加载的内容。

http://jsfiddle.net/Em23e/8/ http://jsfiddle.net/Em23e/8/

You can try it like this: 您可以这样尝试:

http://jsfiddle.net/Em23e/12/ http://jsfiddle.net/Em23e/12/

it uses a loop to display the 12 boxes, or if you multiply it again with Number it will display all possible boxes in the screen. 它使用循环来显示12个框,或者再次将其乘以Number,它将在屏幕上显示所有可能的框。

The main issue in your calculation is that you don't take into account the 3px around each box. 计算中的主要问题是您没有考虑每个框周围的3px。

margin: 3px;

Here is some working JavaScript code to replace the JavaScript you posted. 这是一些可替换您发布的JavaScript的有效JavaScript代码。 This code loops through the body height and width and creates div tiles across the entire visible area. 此代码循环遍历主体的高度和宽度,并在整个可见区域中创建div切片。

Here is the fiddle: http://jsfiddle.net/yUqXs/5/ 这是小提琴: http : //jsfiddle.net/yUqXs/5/

$(function(){
var winwidth = $(window).width();
var width=winwidth;
var span12= width/12;
var winheight = $(window).height();
var height=winheight;
var spanheight=height/12;

$('.button').click(function(){
    var $body = $('body');
    var totalw = 0;
    var totalh = 0;
    var totalw2 = span12;
    var totalh2 = spanheight;

    while (totalh2 < winheight)
    {
        totalw = 0;
        totalw2 = span12;
        while (totalw2 < winwidth)
        {
            $body.append('<div class="box" style="display: block; background: green; width: '+span12+'px; height: '+spanheight+'px; float: left; position: absolute; top: '+totalh+'px; left: '+totalw+'px;">&nbsp;</div>');
            totalw += span12;
            totalw2 += span12;
        }
        totalh += spanheight;
        totalh2 += spanheight;
    }
});
});

Not sure but Probably you want this, updated your code. 不确定,但可能您想要这样做,因此更新了代码。

You need to update the css properties everytime you are adding the box. 每次添加框时,都需要更新css properties check this fiddle 检查这个小提琴

Try using this 试试这个

$(function(){

var width=$(window).width();
var span12= width/12;
var height=$(window).height();
var spanheight=height/12;

$('.button').click(function(){
$('body').append('<div class="box"></div>');
    $('.box').css('display','block');
    $('.box').css("height",spanheight);
    $('.box').css("width",span12);
    $('.box').css("background","green");
    $('.box').css("margin","10px");
});
});

What happen in your case is your div is appending to body but, it's height and width is '0', so you have to set it on click function. 在您的情况下发生的是您的div追加到主体,但是它的高度和宽度为'0',因此必须在click函数上进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM