概括我的功能

时间:2014-11-09 02:55:31

标签: javascript function resize generalization

我编写了这个函数,它通过选定的角落重新调整元素

function RelativeCornerResizer(Elements , StartSize , EndSize , Unit , Left , Top)
{   
    //var I = StartSize ;
    var SSpx = "" , LeftPx="" , TopPx="";

    LeftPx = (Left)+Unit; 
    TopPx = (Top)+Unit;


    if(StartSize < EndSize)
    {       
            StartSize+=2;

            SSpx = StartSize+Unit;

            if(Left!=0) LeftPx = (Left-StartSize)+Unit;
            if(Top!=0)  TopPx = (Top-StartSize)+Unit;

            $(Elements).css({'width': SSpx,'height': SSpx});
            $(Elements).css({'left' : LeftPx , 'top' : TopPx});

            setTimeout(function(){RelativeCornerResizer(Elements , StartSize , EndSize , Unit , Left , Top)},1);
    }

}

现在这只将小元素调整为较大的元素,我想将其概括为从大到小调整大小,所以:

如果我的StartSize大于EndSize ---&gt;大到小 否则---&gt;小到大

一个简单的if if可以循环它从SMALL变为BIG到SMALL变为BIG [...]

有没有办法在函数中引入另一个参数?




我修改了Markus提出的解决方案,但它确实有效,但我仍然不满意......

function RelativeCornerResizer(Elements , StartSize , EndSize , Unit , Left , Top)
{   
    var SSpx = "" , LeftPx = "" , TopPx = "", Direction = StartSize < EndSize , Condition ;

    LeftPx = (Left) + Unit;
    TopPx = (Top) + Unit;

    if (Direction) 
    {   StartSize+=2;
        Condition = StartSize < EndSize ;
    }
    else
    {   StartSize-=2;
        Condition = StartSize > EndSize ;
    }

    SSpx = (StartSize) + Unit;
    LeftDiff = Direction ? Left - StartSize : Left + StartSize;
    TopDiff = Direction ? Top - StartSize : Top + StartSize;

    if(Left!=0) LeftPx = (LeftDiff) + Unit;
    if(Top!=0)  TopPx = (TopDiff) + Unit;

    $(Elements).css({'width': SSpx,'height': SSpx, 'left' : LeftPx , 'top' : TopPx});

    if(Condition)
        setTimeout(function(){RelativeCornerResizer(Elements , StartSize , EndSize , Unit , Left , Top)},1);
}

2 个答案:

答案 0 :(得分:0)

您可以尝试仅传递值之间的差异,无论是正值(在一种情况下)还是负值(在其他情况下),而不是传递startSize和endSize。然后删除现有的if else块,并更改代码以使用&#34;差异&#34;而不是startSize和endSize。

答案 1 :(得分:0)

这是你想要的吗?

function RelativeCornerResizer(Elements , StartSize , EndSize , Unit , Left , Top)
{   
    var SSpx = "" , LeftPx = "" , TopPx = "", Direction = StartSize < EndSize;

    LeftPx = (Left) + Unit;
    TopPx = (Top) + Unit;

    if (Direction) Startsize += 2 else Startsize -= 2;

    SSpx = (StartSize) + Unit;
    LeftDiff = Direction ? Left - StartSiz : Left + StartSiz;
    TopDiff = Direction ? Top - StartSiz : Top + StartSiz;

    if(Left!=0) LeftPx = (LeftDiff) + Unit;
    if(Top!=0)  TopPx = (TopDiff) + Unit;

    $(Elements).css({'width': SSpx,'height': SSpx, 'left' : LeftPx , 'top' : TopPx});

        setTimeout(function(){RelativeCornerResizer(Elements , StartSize , EndSize , Unit , Left , Top)},50);

}