slideToggle在手风琴结束时创造了一个摆动

时间:2013-01-08 17:31:48

标签: javascript jquery

在幻灯片上创建一个手风琴 - 滑动的元素下面的元素似乎向下移动一个px然后再向上,从而产生一种颤抖效果。

$(document).ready(function() {

//Promos banners rotation and accordion
$(function(){
    var $accordionList = $('.accordion').find('li');
    var numberOfItems = $accordionList.length;
    var currentItem = 0;

    // Set first item to active
    $accordionList.eq(currentItem).addClass('active').find('.content').slideToggle(800, function() {});

    // Loops through promos
    var infiniateLoop = setInterval(function() {

        if(currentItem == numberOfItems - 1){
            currentItem = 0;
        }
        else {
            currentItem++;
        }

        // Remove active class, if is has it, and close content
        $accordionList.parent().find('li.active').removeClass('active')
            .find('.content').slideToggle(800, function() {
        });

        // Add active class and open content
        $accordionList.eq(currentItem).addClass('active').find('.content').slideToggle(800, function() {
        });

    }, 4000 );

    // Click to show promo
    $accordionList.on('click', function () {
        // Stop rotation
        clearInterval(infiniateLoop);

        var $accordionHead = $(this);

        // Remove active class, if is has it, and close content
        if($accordionHead.hasClass('active')) {
            // Do nothing
        }
        else {
            $accordionHead.parent().find('li.active').removeClass('active')
                .find('.content').slideToggle(800, function() {
            });

            // Add active class and open content
            $accordionHead.addClass('active').find('.content').slideToggle(800, function() {
            });
        };
    });
});

});

Fiddle here demonstrating the problem

我已经看到一些建议,你修复了内容div的高度 - 但网站是响应性的,因此无法正常工作。

1 个答案:

答案 0 :(得分:0)

雅,我以前遇到过这个问题。我最喜欢的解决办法是制作自己的.slideToggle()

div = $('div');
height = div.height();
width = div.width();

$('div').click( function() {
  if ($(this).hasClass('hidden')) {
    $(this).animate({height: "0", width: "0"}, 200).hide().addClass('hidden');
  } else {
    $(this).animate({height: height, width: width}, 200).show().removeClass('hidden');
  }
});

如果你愿意,你甚至可以将它包装在原型函数中。