0

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);
}
Hammond95
  • 556
  • 7
  • 20
  • *"but i'm still not satisfied"* - What..? That means your question doesn't have a clear problem description. "Generalizing my Function" isn't a real question by the way. – T J Nov 09 '14 at 09:26

2 Answers2

0

Rather than passing both startSize and endSize, you could try passing only the difference between the values, be it positive (in one case) and negative (in other case). Then remove the existing if else block, and change the code to work with the "difference", not startSize and endSize.

Gaurav Aradhye
  • 334
  • 3
  • 14
0

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

}
Markus Kottländer
  • 8,228
  • 4
  • 37
  • 61
  • It doesn't work very good , goes a kind of loopy ... By the way I didn't know this way of expressing `Direction = StartSize < EndSize;` Could you tell me how it is called ? – Hammond95 Nov 09 '14 at 03:43
  • I didn't test it, just an aproach. `StartSize < EndSize` just returns true or false wich gets assigned to Direction, so Direction is true or false. Dont know if it has a name... ^^ – Markus Kottländer Nov 09 '14 at 03:47