未加星标

A Better jQuery Accordion

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

This will be an exercise in using the jQuery slide methods effectively. We'll start with a basic accordion, then improve it.

January 18th, 2012

First Version: <!DOCTYPE html> <html> <head> <style> * { margin:0; padding:0; } /* a simple reset */ .head, li, h2 { margin-bottom:15px; } .head { display:block; } .content { display:none; } </style> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('.head').click(function(e){ e.preventDefault(); $(this).closest('li').find('.content').slideToggle(); }); }); </script> </head> <body> <ul> <li> <a href='#' class='head'>Heading 1</a> <div class='content'>Content 1 </div> </li> <li> <a href='#' class='head'>Heading 2</a> <div class='content'>Content 2</div> </li> <li> <a href='#' class='head'>Heading 3</a> <div class='content'>Content 3</div> </li> </ul> </body> </html> See theDemo The Script

The unordered list will be the structure for the accordion, with each list item being one of the panels. I think documenting the code will make it easier to understand than trying to explain the process, so here is the same script broken up with comments:

// Wait until the DOM is loaded, before trying to query the document $(document).ready(function(){ // select the .head elements, and bind an event handler to them $('.head').click(function(e){ // prevent the default action of the click event // i.e. prevent the browser from redirecting to the link's href e.preventDefault(); // 'this' is the execution context of the event handler // it refers to the clicked .head element $(this) // Walk up the DOM tree to find the closest list item // It is the closest shared ancestor of both .head and .content .closest('li') // Search the list item for .content .find('.content') // Trigger the sliding animation on .content .slideToggle(); }); }); Problem 1: It's Jumpy

The demo demonstrated that the first accordion's animation wasn't fluid at all. jQuery miscalculated the height of the content ('.content') because jQuery gave it an absolute position before measuring the height. This caused the dimensions to change. Giving the content a fixed width and/or height would fix this, but that would be impractical for fluid layouts or content of varying size. An alternative solution is to give the content's parent element a non-static position. This will insure that the content is positioned relative to the parent, which constrains the content to the width and height we expect it to have.

li { position:relative; } Problem 2: It's Still Jumpy

There is a bit of a jump at the start of slideDown and end of slideUp from the browser collapsing adjoining vertical margins . When the slideUp animation completes, jQuery will hide the element being animated. This means there will no longer be an element in the document flow between the .head element's margin and the list item's margin, triggering a collapse. This can be prevented if the margins are separated by padding, border, or clearance. Here are a few examples:

Use overflow:auto or overflow:hidden on the list item to achieve clearance. I recommend using hidden overflow as auto overflow can cause additional jumping:

li { overflow:hidden; }

Or use a transparent border:

li { border-bottom:1px solid transparent; }

Or use padding instead of margin:

.head, li, h2 { padding-bottom:15px; }

There is currently an open ticket about this on bugs.jquery.com.

Problem 3: The Animation Queues Up

Clicking on the <h2> two or more times in quick succession causes the animation to queue up. Normally .stop() is used in this situation, but that causes a major problem with .slideUp() . When called, .slideUp() will record the current height as the height .slideDown() should return to. If .stop() is called during the slideDown animation, an incomplete height will be recorded making the unrevealed content inaccessible until the document is reloaded. One way to work around that is to only call $.slideToggle() if the content is not currently being animated.

$('.head').click(function(e){ e.preventDefault(); $(this).closest('li').find('.content').not(':animated').slideToggle(); }); Caching

The results of $(this).closest('li').find('.content') could be cached so that the document doesn't have to be queried on each interaction. A recommended practice, but probably not worth the extra code in this case since we have to create a closure to hold it. The click event happens infrequently (or never if the user chooses not to interact with the accordion). If we were binding to a high frequency event, such as mousemove, caching would be much more important.

// .each() is used to create a closure to store a cache of the query. $('.head').each(function(){ var $content = $(this).closest('li').find('.content'); $(this).click(function(e){ e.preventDefault(); $content.not(':animated').slideToggle(); }); }); See theDemo

Hey,

Follow me on Twitter and Github , that's where I'm most active these days. I welcome email (), but I'm afraid I no longer have time to answer personal requests for help regarding my plugins or posts. Thanks!

Follow @jacklmoore

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: jQueryGitHeadTwitter
分页:12
转载请注明
本文标题:A Better jQuery Accordion
本站链接:http://www.codesec.net/view/482663.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(26)