2014-10-02 3 views
2

Мне нужно создать эффект, похожий на аккордеон jQuery. У меня есть 10 элементов, которые заполняют экран; когда мышь нависает над предметом, он расширяется по ширине. Как я могу заставить его развернуть справа налево за последние 5 предметов?CSS развернуть ширину справа налево

Мой 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> 

Это мой CSS:

.wrap{ 
    width:100%; 
    float:left; 
    height: 300px; 
    overflow: hidden; 
} 

.item { 
    float:left; 
    height: 100%; 
    display: block; 
} 

JQuery код:

$('.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}); 
}); 

jsFiddle link

Спасибо.

ответ

2

Вам нужно добавить другую оболочку div внутри с большой шириной, которая гарантирует, что ваши плавающие элементы не будут перенесены на следующую строку. Затем выделите внешний оберточной DIV в правильное положение, когда парил элемент # 5 или выше:

$('.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}); 
}); 

Вы можете увидеть здесь работать: jsFiddle link

1

Идея заключается в том, чтобы уменьшить ширину других элементов во время увеличивая ширину зависающего элемента.

JSFiddle.

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

Существует одна странная вещь: из-за fullWidth/itemsCount/2 вместо правильного fullWidth/(itemsCount - 1)/2 есть пустое место на правой границе гармошку, но с fullWidth/(itemsCount - 1)/2 последнего элемента иногда disappeares с экрана.

Смежные вопросы