Search code examples
javascriptfunctionresizegeneralization

Generalizing my Function


I wrote this function that resize an element by a selected corner

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);
    }

}

now this resize only little elements to bigger ones , I want to generalize it to resizing from big to small , so :

if my StartSize is greater than EndSize ---> BIG to SMALL else ---> SMALL to BIG

a simple if else could loop it changing from SMALL to BIG to SMALL to BIG [...]

there's a way without introducing another params to the function ?




I modified the solution proposed by Markus , and it works , but i'm still not satisfied...

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);
}

Solution

  • Is that kind of what you want?

    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);
    
    }