0

我找到了一个非常适合我工作的代码。我想在 jquery 8 no line 中设置 div 高度自动。请看链接。

http://jsfiddle.net/alexis_tondelier/Gf2dv/

HTML

<div class="item">
    <div class="item-overlay"></div>        
</div>
<div class="item">
    <div class="item-overlay"></div>
</div>

jQuery

$(document).ready(function () {
var $active,
    togglerSelector = '.item',
    toggledSelector = '.item-overlay',
    $items = $(togglerSelector),
    animationDuration = 300,
    activeClassName = 'active',
    activeStyle = {height: 300},
    notActiveStyle = {height: 0},
    hideActive = function() {
        $active && $active
            .stop()
            .animate(notActiveStyle, function() {
                $(this).removeClass(activeClassName).hide();
            });    
        $active = void 0;        
    },
    showActive = function($element) {
        $element && ($active = $element)
            .stop()
            .show()
            .animate(activeStyle)
            .addClass(activeClassName)
    };

$items.on('click', function(event) {
    var $this = $(this),
        $overlay = $this.children(toggledSelector),
        theSame = $overlay.is($active);
    hideActive();
    theSame || showActive($overlay);
});
$(this).on('click', function(event) {
    var $target = $(event.target),
        isOutClick = !$target.closest(togglerSelector).length;
    isOutClick && hideActive();
});

});

谢谢....

4

0 回答 0