添加默认活动状态以悬停滑动手风琴li

时间:2013-10-23 18:58:03

标签: jquery html css

我正在使用水平滑动手风琴,并且我在悬停时让每个元素变为活动(展开),但是在休眠状态下,初始活动元素应该展开,而不是所有LI折叠。

请参阅示例 http://jsfiddle.net/WT8Tg/

HTML

        <ul class="accordion" id="accordion">
            <li class="bg1">
                <div class="heading">Guler</div>
                <div class="bgDescription"></div>
                <div class="description">
                    <h2>Guler</h2>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                        sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
                        ut aliquip ex ea commodo consequat. Duis aute irure dolor in
                        reprehenderit in voluptate velit esse cillum dolore eu fugiat
                        nulla pariatur.</p>
                    <a href="#">more &rarr;</a>
                </div>
            </li>
            <li class="bg2">
                <div class="heading">Phillips</div>
                <div class="bgDescription"></div>
                <div class="description">
                    <h2>Phillips</h2>
                    <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem
                        accusantium doloremque laudantium, totam rem aperiam, eaque ipsa
                        quae ab illo inventore veritatis et quasi architecto beatae vitae
                        dicta sunt explicabo. </p>
                    <a href="#">more &rarr;</a>
                </div>

            </li>
            <li class="bg3">
                <div class="heading">Diamanti</div>
                <div class="bgDescription"></div>
                <div class="description">
                    <h2>Diamanti</h2>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                        sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
                        ut aliquip ex ea commodo consequat. Duis aute irure dolor in
                        reprehenderit in voluptate velit esse cillum dolore eu fugiat
                        nulla pariatur.</p>
                    <a href="#">more &rarr;</a>
                </div>

            </li>
            <li class="bg4 bleft active">
                <div class="heading">Meiklejohn</div>
                <div class="bgDescription"></div>
                <div class="description">
                    <h2>Meiklejohn</h2>
                    <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem
                        accusantium doloremque laudantium, totam rem aperiam, eaque ipsa
                        quae ab illo inventore veritatis et quasi architecto beatae vitae
                        dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
                        sit aspernatur aut odit aut fugit, sed quia consequuntur magni
                        dolores eos qui ratione voluptatem sequi nesciunt.</p>
                    <a href="#">more &rarr;</a>
                </div>

            </li>
        </ul>

CSS

    ul.accordion > li.active {
            width:480px;
    }

    ul.accordion{
        list-style:none;   
        position:absolute;
        right:80px;
        top:0px;
        font-family: Cambria, serif;
        font-size: 16px;
        font-style: italic;
        line-height: 1.5em;
    }
    ul.accordion li{
        float:right;
        width:115px;
        height:480px;
        display:block;
        border-right:2px solid #fff;
        border-bottom:2px solid #fff;
        background-color:#fff;
        background-repeat:no-repeat;
        background-position:center center;
        position:relative;
        overflow:hidden;
        cursor:pointer;
        -moz-box-shadow:1px 3px 15px #555;
        -webkit-box-shadow:1px 3px 15px #555;
        box-shadow:1px 3px 15px #555;
    }




    ul.accordion li.bg1{
        background-image:url(../images/1.jpg);
    }
    ul.accordion li.bg2{
        background-image:url(../images/2.jpg);
    }
    ul.accordion li.bg3{
        background-image:url(../images/3.jpg);
    }
    ul.accordion li.bg4{
        background-image:url(../images/4.jpg);
    }
    ul.accordion li.bleft{
        border-left:2px solid #fff;
    }
    ul.accordion li .heading{
        background-color:#fff;
        padding:10px;
        margin-top:60px;
        opacity:0.9;
        text-transform:uppercase;
        font-style:normal;
        font-weight:bold;
        letter-spacing:1px;
        font-size:14px;
        color:#444;
        text-align:center;
        text-shadow:-1px -1px 1px #ccc;
    }
    ul.accordion li .description{
        position:absolute;
        width:480px;
        height:175px;
        bottom:0px;
        left:0px;
        display:none;
    }
    ul.accordion li .description h2{
        text-transform:uppercase;
        font-style:normal;
        font-weight:bold;
        letter-spacing:1px;
        font-size:45px;
        color:#444;
        text-align:left;
        margin:0px 0px 15px 20px;
        text-shadow:-1px -1px 1px #ccc;
    }
    ul.accordion li .description p{
        line-height:14px;
        margin:10px 22px;
        font-family: "Trebuchet MS", sans-serif;
        font-size: 12px;
        font-style: italic;
        font-weight: normal;
        text-transform: none;
        letter-spacing: normal;
        line-height: 1.6em;
    }
    ul.accordion li .description a{
        position:absolute;
        bottom:5px;
        left:20px;
        text-transform:uppercase;
        font-style:normal;
        font-size:11px;
        text-decoration:none;
        color:#888;
    }
    ul.accordion li .description a:hover{
        color:#333;
        text-decoration:underline;
    }

    ul.accordion li .bgDescription{
        background:transparent url(../images/bgDescription.png) repeat-x top left;
        height:340px;
        position:absolute;
        bottom:0px;
        left:0px;
        width:100%;
        display:none;
    }

JQuery的

         $(function() {
            $('#accordion > li').hover(
                function () {
                    var $this = $(this);
                    $this.stop().animate({'width':'480px'},500);
                    $('.heading',$this).stop(true,true).fadeOut();
                    $('.bgDescription',$this).stop(true,true).slideDown(500);
                    $('.description',$this).stop(true,true).fadeIn();
                },
                function () {
                    var $this = $(this);
                    $this.stop().animate({'width':'115px'},1000);
                    $('.heading',$this).stop(true,true).fadeIn();
                    $('.description',$this).stop(true,true).fadeOut(500);
                    $('.bgDescription',$this).stop(true,true).slideUp(700);
                }
            );

            $(function() {      
                $('#accordion').on('click', 'li', function() {  
                    $('.accordion li.active').removeClass('active'); //Remove any "active" class  
                    $('li', this).addClass('active'); //Add "active" class to selected tab  


                    //Commented since it is not available
                    //$(activeTab).show(); //Fade in the active content  
                    return false;  
                }); 
            });

        });

1 个答案:

答案 0 :(得分:0)

您只需添加一些规则即可适应该行为。

http://jsfiddle.net/WT8Tg/3/

我在悬停时添加了这个:

$('li').stop().animate({'width':'115px'},1000);

我在悬停结束时添加了这个:

$('.bg4').stop().animate({'width':'480px'},500);

我强烈建议使用类来分配宽度,而不是在Javascript中对其进行硬编码。然后,您可以为删除/添加类添加动画。