Home  >  Article  >  Web Front-end  >  Detailed explanation of the exploration of the three effects of accordion in HTML5 applications

Detailed explanation of the exploration of the three effects of accordion in HTML5 applications

黄舟
黄舟Original
2017-03-31 13:47:321976browse

Abstract: Accordion (also known as "drawer") effect is named after its expansion style that looks like an accordion. Through hierarchical relationships, a clever balance is achieved in information display and page layout. Therefore, it is widely used in Web and App interaction design. In previous projects, Accordion was usually implemented by JavaScript coding. This sharing focuses on exploring two methods that do not rely on JS and use pure CSS3 or HTML5 to achieve their effects. And make a preliminary comparison of its advantages and disadvantages.

Traditional JS implementation method

1. Native JavaScript

2. Calling JS library files, jQuery, jQuery Mobile

$'.menu_lev1').clickfunction) {

    var _this=$this),

        _next=_this.next);

    if _next.is':visible')) {

        $'.menu_lev1').removeClass'on');

        $'.menu_lev2').slideUp600);

        _this.addClass'on');

        _next.slideDown600);

    } else {

        _next.slideUp600);

        _this.removeClass'on');

    }

    return true;

});

Disadvantages of copying code: low efficiency, high cost, behavior and style are tightly coupled.

CSS3 Pseudo-class: target

target is one of new pseudo-classes in CSS3. It can add specified styles to the target element through anchor. Because of the uniqueness of the anchor points in the page, mutually exclusive rotation effects can be achieved.

Sample code 1: h1 first-level directory/h1>

ul id="ac1">

      li>二级菜单1/li>

      li>二级菜单2/li>

      li>二级菜单3/li>

 /ul>

Copy code

ul{ display:none;}

ul:target{display:block;}

Copy codeExample code 2: c1">First-level directory/a>/ h1>

ul id="ac1">

      li>二级菜单1/li> 

      li>二级菜单2/li> 

      li>二级菜单3/li>

/ul>

h1>2">一级目录/a>/h1>

ul id="ac2">

      li>二级菜单1/li> 

      li>二级菜单2/li> 

      li>二级菜单3/li>

/ul>

h1>一级目录/a>/h1>

ul id="ac3">

      li>二级菜单1/li> 

      li>二级菜单2/li> 

      li>二级菜单3/li>

/ul>

Copy code

ul{ display:none;}

ul:target{display:block;}

Copy code Sample code 3: div id="ac1" >

h1>a >一级目录/a>span>/span>/h1>

ul>

      li>二级菜单1/li>

      li>二级菜单2/li>

/ul>

/div>

div id="ac2" >

          h1>a >一级目录/a>span>/span>/h1>

          ul>

               li>二级菜单1/li>

               li>二级菜单2/li>

          /ul>

/div>

div id="ac3" >

          h1>a 3">一级目录/a>span>/span>/h1>

          ul>

               li>二级菜单1/li>

               li>二级菜单2/li>

          /ul>

/div>

Copy code

ul{-webkit-transition:all ease 1s; } 

div:target ul{height:400px;}

div:target span{-webkit-transform:rotate90deg);}

Copy code Css3 pseudo-class: targetl Disadvantages: 1. No duality. 2. Transition animation height cannot be obtained automatically.

HTML5 tag summary & details

##summary & details. are two new tags in HTML5. In addition to having strong

semantic, they also have surprising dynamic effects. Therefore, we can easily create them by seizing this feature. Lightweight accordion effect. Generally speaking, these two tags should be used in pairs.

Sample code 1: details>

 summary>一级目录/summary>  

       ul>

            li>二级菜单/li>

            li>二级菜单/li>

            li>二级菜单/li>

       /ul> 

/details>

Copy code Default style: You can add open to details.

Attribute, so that the details are expanded by default. This tag is currently only supported by the webkit kernel.

Sample code 2: details>

  summary>一级目录/summary>  

       ul>

            li>二级菜单/li>

            li>二级菜单/li>

            li>二级菜单/li>

       /ul> 

      details>

            summary>二级菜单/summary>  

           ul>

               li>三级菜单/li>

               li>三级菜单/li>

               li>三级菜单/li>

           /ul> 

      /details>  

/details>

Copy code description: It can be nested to form. Three-level and more-level menus.

details summary::-webkit-details-marker {background: red;color: #fff;font-size: 200%;} 

summary::-webkit-details-marker { display: none }

summary:after { content: "+";}

details[open] summary:after {content: "-";}

Disadvantages of copying the code: 1. Not mutually exclusive. 2. The transition animation effect is not currently supported.

Summary JS implements the accordion effect. The cost is high and the efficiency is low. Currently, CSS3 can achieve the richest effect. It has low cost and high efficiency. Although it can achieve rich animation effects, HTML5 does not have the duality to achieve the accordion effect and has low cost. High efficiency, but lacks animation effects and is not mutually exclusive. The currently supported browser cores are limited

Looking forward to the introduction and development of CSS3 and HTML5, which will further promote the structure, style, and behavior to some extent. The concept of web design that is separated from others. This reduces the dependence on JS in performance, reduces the cost of web page production, and improves the efficiency of web page operation. There is no reason not to expect that CSS3 and HTML5 will continue to improve and provide more powerful functions in the future.

Recommendation

In web apps with a lot of content, based on the principle of performance priority, gracefully degrade appropriately and use HTML5 tags to achieve the Accordion effect. At least the author of Html5doctor prefers it in WordPress.

The above is the detailed content of Detailed explanation of the exploration of the three effects of accordion in HTML5 applications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]