user736893
user736893

Reputation:

Animate div in list, keep siblings static

I have a vertical list of divs and I'm animating each on hover. I want the rest of them to stay in the same position while the selected one "grows" (I hate to say it, but think MacOS Launcher). I know I could do it if I absolutely positioned each one using a JavaScript loop, but I'd like this to be as dynamic as possible.

Ideas?

HTML:

<div id="leftMenu">
    <div id="lmHome" class="lmSelected"><i class="icon-home icon-2x"></i></div>
    <div id="lmSearch"><i class="icon-search icon-2x"></i></div>
    <div id="lmFeed"><i class="icon-rss icon-2x"></i></div>
    <div id="lmPeople"><i class="icon-group icon-2x"></i></div>
    <div id="lmNew"><i class="icon-plus icon-2x"></i></div>
    <div id="Div2"><i class="icon-calendar icon-2x"></i></div>
    <div id="lmSettings"><i class="icon-cogs icon-2x"></i></div>
    <div id="Div1"><i class="icon-question icon-2x"></i></div>
</div>

CSS:

#leftMenu { /* The container for the buttons on the left menu*/
    position: absolute;
    left: 10px;
    padding-top: 10px;
    height: auto;
}

#leftMenu div { /* The buttons on the left menu*/
    position: relative;
    top: 0px;
    height: 50px;
    width: 50px;
    border: 1px solid gray;
    text-align: center;
    overflow: visible;
}

#leftMenu div i {
    position: relative;
    top: 12px;
}

#leftMenu div:hover {
}

#leftMenu div:last-child {
    border-bottom: 1px solid gray;
}

JavaScript:

$('#leftMenu div').on('mouseenter', function () {
    var self = $(this);
    self.stop()
        .animate({ 'height': '+=10px' }, { duration: 100, queue: false })
        .animate({ 'width': '+=10px' }, { duration: 100, queue: false })
        .animate({ 'top': '-=5px' }, { duration: 100, queue: false })
});

$('#leftMenu div').on('mouseleave', function () {
    var self = $(this);
    self.stop()
        .animate({ 'height': '-=10px' }, { duration: 400, queue: false })
        .animate({ 'width': '-=10px' }, { duration: 400, queue: false })
        .animate({ 'top': '+=5px' }, { duration: 400, queue: false })
});

Upvotes: 3

Views: 468

Answers (2)

Greg
Greg

Reputation: 21909

http://jsfiddle.net/bttah/

Use the right tool for the job. JavaScript, while giving old browsers animation capabilities that were mind-blowing 2 or 3 years ago, is not intended for animation, so here I give you an example of how your animation can be achieved with 3 extra lines of CSS.

#leftMenu { /* The container for the buttons on the left menu*/
    position: absolute;
    left: 10px;
    padding-top: 10px;
    height: auto;
}

#leftMenu div { /* The buttons on the left menu*/
    position: relative;
    top: 0px;
    height: 50px;
    width: 50px;
    border: 1px solid gray;
    text-align: center;
    overflow: visible;
    background: #fff;

    -webkit-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
}

#leftMenu div i {
    position: relative;
    top: 12px;
}

#leftMenu div:hover {
    -webkit-transform: scale(2);
    transform: scale(2);
    z-index: 100;
}

#leftMenu div:last-child {
    border-bottom: 1px solid gray;
}

Upvotes: 4

LeBen
LeBen

Reputation: 2049

It's the perfect case to use CSS transforms, scale() in this case.

Here's a simple example: http://jsfiddle.net/LeBen/vYjNT/1/

There's more options available and you can also add some easing with transitions.

Upvotes: 1

Related Questions