减小所有当前正方形的像素大小

时间:2016-11-09 04:01:27

标签: javascript dom pixel

我编写了一个函数来减小当前所有方块的大小,通过随机int 5-15,例如将所有方块减少7个像素,如果减少使其大小小于2x2,则该函数不得减小平方。在这种情况下,我可以保留原样,也可以完全删除它。

继承我的功能,虽然它不起作用但是只是停止在一个点上继续变小。

enter image description here

function px1down(){

    var allsquares = document.querySelectorAll("#squarearea div");
    var ranpixsize = getRndInteger(5,15);
    for (var i = 0; i<allsquares.length; i++){


        allsquares[i].style.width = parseInt(allsquares[i].style.width, 10) - ranpixsize + 'px';
        allsquares[i].style.height = parseInt(allsquares[i].style.height, 10) - ranpixsize + 'px';

        if(allsquares[i].style.height * allsquares[i].style.width < 4 + 'px'){
            allsquares[i].style.height = parseInt(allsquares[i].style.height, 10);
            allsquares[i].style.width = parseInt(allsquares[i].style.width, 10);
            alert('you cannot reduce the score to less than 2x2 pixels!');
        }

    }
}

...另外我已经编写了一个增加像素大小的函数,但是最终制作的正方形超出了700x300矩形div,正方形在这里包含了代码吗?

function px1(){

    var allsquares = document.querySelectorAll("#squarearea div");
    var ranpixsize = getRndInteger(5,15);
    for (var i = 0; i<allsquares.length; i++){

        allsquares[i].style.width = parseInt(allsquares[i].style.width, 10) + ranpixsize + 'px';
        allsquares[i].style.height = parseInt(allsquares[i].style.height, 10) + ranpixsize + 'px';


      if(allsquares[i].style.left < 0 + 'px'){
            allsquares[i].style.width = parseInt(allsquares[i].style.width, 10);
            allsquares[i].style.height = parseInt(allsquares[i].style.height, 10);
        }
        else if(allsquares[i].style.top < 0 + 'px'){
            allsquares[i].style.width = parseInt(allsquares[i].style.width, 10);
            allsquares[i].style.height = parseInt(allsquares[i].style.height, 10);
        }
        else if(allsquares[i].style.width>700 + 'px'){
            allsquares[i].style.width = parseInt(allsquares[i].style.width, 10);
            allsquares[i].style.height = parseInt(allsquares[i].style.height, 10);
        }
        else if(allsquares[i].style.height>300 + 'px'){
            allsquares[i].style.width = parseInt(allsquares[i].style.width, 10);
            allsquares[i].style.height = parseInt(allsquares[i].style.height, 10);
        }
    }
}

0 个答案:

没有答案