Reputation: 235
I need to build an effect like jQuery's accordion. I have 10 items that fill the screen; when the mouse hovers over an item, it expands in width. How I can make it expand from right to left for the last 5 items?
My HTML :
<div class="wrap">
<div style="width: 165.25px; background: rgb(228, 228, 230);" class="item"></div>
<div style="width: 165.25px; background: rgb(35, 123, 192);" class="item"></div>
<div style="width: 165.25px; background: rgb(20, 4, 4);" class="item"></div>
<div style="width: 165.25px; background: rgb(182, 159, 36);" class="item"></div>
<div style="width: 165.25px; background: rgb(162, 169, 180);" class="item"></div>
<div style="width: 161px; background: rgb(37, 29, 4);" class="item"></div>
<div style="width: 161px; background: red;" class="item"></div>
<div style="width: 161px; background: rgb(88, 45, 45);" class="item"></div>
<div style="width: 161px; background: rgb(202, 41, 176);" class="item"></div>
<div style="width: 161px; background: rgb(24, 207, 185);" class="item"></div>
</div>
This is my css:
.wrap{
width:100%;
float:left;
height: 300px;
overflow: hidden;
}
.item {
float:left;
height: 100%;
display: block;
}
jQuery code:
$('.item').each(function(e){
$(this).css('width', ($(this).parent().width()/10));
});
$('.item').on('mouseenter', function(event) {
$(this).stop().animate({width: "50%"}, {duration: 500});
}).on('mouseleave', function(event) {
$(this).stop().animate({width: ($(this).parent().width()/10)}, {duration: 500});
});
Thanks.
Upvotes: 2
Views: 2094
Reputation: 5176
The idea is to reduce width of other items during increasing width of hovered element.
var items = $('.item');
var itemsCount = items.length;
var fullWidth = $('.item:first').parent().width();
items.each(function()
{
$(this).css('width', fullWidth / itemsCount);
});
items.on('mouseenter', function()
{
items.not(this).stop().animate({ width: fullWidth / itemsCount / 2 }, 500);
$(this).stop().animate({ width: "50%" }, 500 );
});
items.on('mouseleave', function()
{
items.stop().animate({ width: fullWidth / itemsCount }, 500);
});
There is one weird thing: due to fullWidth / itemsCount / 2
instead of correct fullWidth / (itemsCount - 1) / 2
there is empty place on right border of accordion, but with fullWidth / (itemsCount - 1) / 2
last element sometimes disappeares from screen.
Upvotes: 1
Reputation: 4574
You need to add another wrapper div inside with a big width which makes sure that your floating elements don't wrap to the next line. Then, scroll the outer wrapping div to the correct position when the hovered element is #5 or higher:
$('.item').on('mouseenter', function(event) {
//Animate width
$(this).stop().animate({width: $(this).parent().parent().width()/2}, {duration: 500});
if($(this).index() >= 5){
//Animate scrollLeft
var marginLeft = $(this).parent().parent().width() / 10 * 4;
$(this).parent().parent().stop().animate({scrollLeft: marginLeft + 'px'}, {duration: 500});
}
}).on('mouseleave', function(event) {
//Animate width
$(this).stop().animate({width: ($(this).parent().parent().width()/10)}, {duration: 500});
//Animate scrollLeft
$(this).parent().parent().stop().animate({scrollLeft: 0}, {duration: 500});
});
You can see it working here: jsFiddle link
Upvotes: 2