Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How avoid sibling element from shaking when animate with jquery

I have two div elements side by side. When i move the mouse over the first and animates it, the next one strangely shakes. See here: http://jsfiddle.net/YqZSv/1/ I've noticed it only happens when padding and border are involved. If i replace border with margin, the 'shaking' effect stops.

HTML

<div class='a'></div>
<div class='b'></div>

CSS

.a {
   width: 80px;
   height: 80px;
   padding: 10px;
   border: 0px solid yellow;
   background-color: red;
   display: inline-block
}

.b {
   width: 100px;
   height: 100px;
   background-color: blue;
   display: inline-block;
   margin-left: 20px;
}

jQuery

$('.a').mouseenter(function(){
    $(this).animate({
        'padding': 0,
        'borderWidth': 10
    });
}).mouseleave(function(){
    $(this).animate({
       'padding': 10,
       'borderWidth': 0
    });
});

I can't use margin instead of border because i use a background image with border origin, so i don't want it to move together with its content.

Any help?

like image 809
vbsessa Avatar asked Jun 15 '13 20:06

vbsessa


3 Answers

Tell the browser to keep both the padding and the border-width inside the defined height/width so it doesn't think the size is changing:

div.a { box-sizing:border-box; }

http://jsfiddle.net/exvEa/

like image 153
Malk Avatar answered Oct 20 '22 13:10

Malk


If you DO NOT Mind some modification... I'd go for CSS Positioning.

Though this will have an additional tag Something like:

<div id="mother">
    <div class='a'></div>
<div class='b'></div>
    </div>

and the in your original CSS:

#mother{
position:relative;  width:210px;
}
    .a {
       width: 80px;
       height: 80px;
       padding: 10px;
       border: 0px solid yellow;
       background-color: red;
       display: inline-block;
       position:absolute; 
       left:0px; 
    }

    .b {
       width: 100px;
       height: 100px;
       background-color: blue;
       display: inline-block;
       margin-left: 20px;
       position:absolute; 
       right:0px; 
    }

jQuery:

$('.a').mouseenter(function(){
    $(this).animate({
        'padding': 0,
        'borderWidth': 10
    });
}).mouseleave(function(){
    $(this).animate({
       'padding': 10,
       'borderWidth': 0
    });
});

Try that....

http://jsfiddle.net/8jFyL/

like image 3
ErickBest Avatar answered Oct 20 '22 13:10

ErickBest


If small html/css changes aren't problem: http://jsfiddle.net/YqZSv/8/

HTML:

<div class="box">

<div class='a'></div>
</div>
<div class="box">
<div class='b'></div>
    </div>

CSS:

.box {
width:100px;
    height:100px;
    margin-right:20px;
    float:left;
}
.a {
    width: 80px;
    height: 80px;
    padding: 10px;
    border: 0px solid yellow;
    background-color: red;
    display: inline-block
}

.b {
    width: 80px;
    height: 80px;
     padding: 10px;
    background-color: blue;
    display: inline-block;

}

Basically, one div as wrapper...

like image 2
sinisake Avatar answered Oct 20 '22 13:10

sinisake