jQuery简单手风琴效果(Accordion)学习总结

胡越
2023-12-01

一、基本手风琴效果——参考jQuery UI官网(补充:这个是jQuery UI效果)

<script>
$(function() {
    $("#accordion")
        .accordion({
            header: "> div > h3"
        })
        .sortable({
            axis: "y",
            handle: "h3",
            stop: function( event, ui ) {
                // IE doesn't register the blur when sorting
                // so trigger focusout handlers to remove .ui-state-focus
                ui.item.children( "h3" ).triggerHandler( "focusout" );
            }
        });
    });
</script>
</head>

<body>
<div class="demo">
    <div id="accordion">
        <div class="group">
            <h3><a href="#">Section 1</a></h3>
            <div>
                <p>Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.</p>
            </div>
        </div>
        <div class="group">
            <h3><a href="#">Section 2</a></h3>
            <div>
                <p>Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna. </p>
            </div>
        </div>
        <div class="group">
            <h3><a href="#">Section 3</a></h3>
            <div>
                <p>Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui. </p>
                <ul>
                    <li>List item one</li>
                    <li>List item two</li>
                    <li>List item three</li>
                </ul>
            </div>
        </div>
        <div class="group">
            <h3><a href="#">Section 4</a></h3>
            <div>
                <p>Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est. </p><p>Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. </p>
            </div>
        </div>
    </div>
</div><!-- End demo -->

二、其他简单代码

<script>
$(document).ready(function () {
    //先隐藏所有Item
    $(".item").hide();
    //显示第一个ItemItem
    $(".item:first").show();
    $("#accordion h2").click(function () {
        //如果当前Item是隐藏
        if ($(this).next().is(":hidden")) {
            //先slideUp所有Item
            $(".item").slideUp();
            //slideDown当前Item
            $(this).next().slideDown();                   
        }
    });
})
</script>

<div class="demo">
    <div id="accordion">
        <h2><a href="#">Section 1</a></h2>
        <div class="item">
            <a target="_blank" href="#"><img src="image/green.png" alt="" /></a>
            <p></p>
        </div>
        <h2><a href="#">Section 2</a></h2>
        <div class="item">
            <a target="_blank" href="#"><img src="image/green.png" alt="" /></a>
            <p></p>
        </div>
        <h2><a href="#">Section 3</a></h2>
        <div class="item">
            <a target="_blank" href="#"><img src="image/green.png" alt="" /></a>
            <p></p>
        </div>
    <!--more item-->
    </div>
</div><!-- End demo -->

三、带弹性减震效果——使用了easing插件

<div id="main">
  <h1>Colorful Content Accordion, CSS &amp; jQuery</h1>  
  <h2>View the <a href="http://tutorialzine.com/2009/12/colorful-content-accordion-css-jquery/">original tutorial &raquo;</a></h2>
  <ul class="container">
      <li class="menu">      
          <ul>
            <li class="button"><a href="#" class="green">Kiwis <span></span></a></li>
            <li class="dropdown">
                <ul>
                    <li><a href="#" onclick="$('.button a').eq(2).click();return false;">Open Grapes Section</a></li>
                    <li><a href="#" onclick="$('.dropdown').slideUp('slow');return false;">Close This Section</a></li>
                    <li><a href="http://en.wikipedia.org/wiki/Kiwifruit">Read on Wikipedia</a></li>
                    <li><a href="http://www.flickr.com/search/?w=all&amp;q=kiwi&amp;m=text">Flickr Stream</a></li>
                </ul>
            </li>
          </ul>          
      </li>      
      <li class="menu">      
          <ul>
            <li class="button"><a href="#" class="orange">Oranges <span></span></a></li> 
            <li class="dropdown">
                <ul>
                    <li><a href="#" onclick="$('.button a:last').click();return false;">Open Last Section</a></li>
                    <li><a href="http://en.wikipedia.org/wiki/Orange_%28fruit%29">Wikipedia Page</a></li>
                    <li><a href="http://www.flickr.com/search/?w=all&amp;q=oranges&amp;m=text">Flickr Photos</a></li>
                </ul>
            </li>
          </ul>          
      </li>      
      <li class="menu">      
          <ul>
            <li class="button"><a href="#" class="blue">Grapes <span></span></a></li>
            <li class="dropdown">
                <ul>
                    <li><a href="http://en.wikipedia.org/wiki/Grapes">Wiki page</a></li>
                    <li>Text label 1</li>
                    <li>Text label 2</li>
                    <li><a href="http://www.flickr.com/search/?w=all&amp;q=grapes&amp;m=text">Flickr Stream</a></li>
                </ul>
            </li>
          </ul>          
      </li>    
      <li class="menu">      
          <ul>
            <li class="button"><a href="#" class="red">Strawberries <span></span></a></li>
            <li class="dropdown">
                <ul>
                    <li><a href="http://en.wikipedia.org/wiki/Strawberry">Wiki page</a></li>
                    <li><a href="http://www.flickr.com/photos/mojeecat/368540120/">Strawberry Pie</a></li>
                    <li><a href="http://www.flickr.com/search/?w=all&amp;q=strawberries&amp;m=text">Photo Stream</a></li>
                </ul>
            </li>
          </ul>
          
      </li>
  </ul>

jQuery代码

$(document).ready(function(){
    /* This code is executed after the DOM has been completely loaded */
    /* Changing thedefault easing effect - will affect the slideUp/slideDown methods: */
    $.easing.def = "easeOutBounce";
    /* Binding a click event handler to the links: */
    $('li.button a').click(function(e){    
        /* Finding the drop down list that corresponds to the current section: */
        var dropDown = $(this).parent().next();        
        /* Closing all other drop down sections, except the current one */
        $('.dropdown').not(dropDown).slideUp('slow');
        dropDown.slideToggle('slow');        
        /* Preventing the default event (which would be to navigate the browser to the link's address) */
        e.preventDefault();
    })    
});

 四、手风琴菜单效果

<div id="nav-accordion">
    <p><a href="">Home ></a><a href=""> HomeworkHelp ></a> <a href="">Alberta </a></p>
    <ul id="accordion">
        <li><a href="/" class="">home</a></li>
        <li><a href="/" class="">service</a></li>
         <ul>
           <li><a href="/us/tutoring-services">Tutoring Services</a></li>
              <ul>
                <li><a href="/us/tutoring-services/tutor"> Tutor</a></li>
                <li><a href="/us/tutoring-services/tutoring"> Tutoring</a></li>
               </ul>
                   <li><a href="/us/tutoring-services/tutor">Homework Help</a></li>
                       <ul>
                          <li><a href="/us/tutoring-services">Math Tutor</a></li>
                          <li><a href="/us/homework-help/reading-tutor"> Reading Tutor</a></li>
                          <li><a href="/us/homework-help/writing-tutor"> Writing Tutor</a></li>
                          <li><a href="/us/homework-help/english-tutor"> English Tutor</a></li>
                       </ul>
                    <li><a href="/us/tutoring-centres"> Tutoring Centres</a></li>
                        <ul>
                          <li><a href="/us/tutoring-centres/childrens-learning-centre"> Children's Learning Centre</a></li>
                          <li><a href="/us/tutoring-centres/kids-learning-centre"> Kid's Learning Centre</a></li>
                        </ul>
                </ul>                
                <li style="background-image: none;"><a href="/us/location" class="">locations</a></li>
         </ul>
</div>

jquery

$(document).ready(function(e){
    $("#nav-accordion").hide();
    $("#menu-button").click(function(){
        $("#nav-accordion").slideToggle();        
        $('#accordion > ul:eq(0)').show();
    });            
    $("#accordion > li:eq(1)").click(function(){
        if(false == $(this).next().is(':visible')) {
               $('#accordion > ul').slideUp(500);
        }
        $(this).next().slideToggle(500);
    });
    
    $("#accordion ul > li ").click(function(){
        if(false == $(this).next().is(':visible')) {
            $('#accordion ul > ul').slideUp(500);
        }
        $(this).next().slideToggle(500);
    });
});

 

 

 

 

转载于:https://www.cnblogs.com/JoannaQ/archive/2012/10/06/2712710.html

 类似资料: