开发者

How can you adjust the height of a jQuery UI accordion?

In my UI I have an accordion setup like this:

<div id="object_list"&g开发者_StackOverflow中文版t;
    <h3>Section 1</h3>
    <div>...content...</div>

    // More sections
</div>

The accordion works properly when it is first formed, and it seems to adjust itself well for the content inside each of the sections. However, if I then add more content into the accordion after the .accordion() call (via ajax), the inner for the section ends up overflowing.

Since the accordion is being formed with almost no content, all the inner divs are extremely small, and thus the content overflows and you get accordions with scrollbars inside with almost no viewing area.

I have attempted to add min-height styles to the object_list div, and the content divs to no avail. Adding the min-height to the inner divs kind of worked, but it messed up the accordion's animations, and adding it to the object_list div did absolutely nothing.

How can I get a reasonable size out of the content sections even when there is not enough content to fill those sections?


autoHeight was deprecated in 1.9, and removed in 1.10.

Use:

$('#id').accordion({heightStyle: 'content'});

to auto size your inner div.

UPDATE:

I see that this is still quite an active post, so I decided to make sure my answer is still valid. It looks like this may no longer work in jQuery UI 1.11. It notes that the [content] property has been deprecated, and to use [panel] instead. Making the code snippet now look something more like this:

$('#id').accordion({heightStyle: 'panel'});

I HAVE NOT YET TESTED THIS, JUST FOUND, AND WILL RETURN AND REMOVE THIS COMMENT WHEN I HAVE TIME TO TEST


When you declare the accordion control div, you can put a height in the style tag for the div. Then you can set the fillSpace: true property to force the accordion control to fill that div space no matter what. This means you can set the height to whatever works best for you page. You could then change the height of the div when you add your code

If you want the accordion to dynamically resize to the content it contains as needed you can do the following trick posted on the jQuery UI website.

//getter
var autoHeight = $( ".selector" ).accordion( "option", "autoHeight" );
//setter
$( ".selector" ).accordion( "option", "autoHeight", false );

This means when you select an area with a lot of text, the accordion will recalculate it.


From the docs it sounds like you'll need to set

clearStyle: true

...and also

autoHeight: false

I believe that using clearStyle allows you to dynamically add content without Accordion getting in the way.

So try this...

$( ".selector" ).accordion({ clearStyle: true, autoHeight: false });


It looks like all the answers here are now using deprecated options.

With the latest version of jQuery UI (1.10.x), you should initialize your accordion with heightStyle: "fill" to get the intended effect..

$(".selector").accordion({ heightStyle: "fill" });

You can read more at the jQuery UI API docs here: http://api.jqueryui.com/accordion/#option-heightStyle

If your page dimensions change dynamically and you need to recalculate your accordion size, you should refresh your accordion using the refresh method:

$(".selector").accordion("refresh");

This is preferred as the resize method is now deprecated.


Setting the DIV's height will do the trick.

$(document).ready(function() {

    $("#accordion").show().accordion({
        autoHeight: false
    });

    $("#accordion div").css({ 'height': 'auto' });
});      


Use heightStyle option to control the height of the accordion and each panel.

$("#accordion").accordion({
  heightStyle: "content"
});

Possible values:

  1. "auto": All panels will be set to the height of the tallest panel.
  2. "fill": Expand to the available height based on the accordion's parent height.
  3. "content": Each panel will be only as tall as its content.


Turning off auto will work... (with any string besides auto or fill) such as the solution telling to use "panel". But...

That is the same as putting in any garbage string for "heightStyle". The "heightStyle" you are looking for is "content".

  • http://api.jqueryui.com/accordion/#option-heightStyle
  • https://github.com/jquery/jquery-ui/blob/master/tests/unit/accordion/options.js
  • https://github.com/jquery/jquery-ui/blob/master/ui/widgets/accordion.js

(values for option "heightStyle" in jQuery UI 1.12)

  • "auto": All panels will be set to the height of the tallest panel.
  • "fill": Expand to the available height based on the accordion's parent height.
  • "content": Each panel will be only as tall as its content.

Example: https://jsfiddle.net/qkxyodpq/5/

Hope that helps.


for me doing following worked accurately.

$( "#accordion" ).accordion({
    autoHeight: false,

});


In your jquery-ui.js search for the following section and change heightstyle: "auto" to heightstyle: "content" so the updated file will look like this.

var accordion = $.widget( "ui.accordion", {
  version: "1.11.4",
  options: {
    active: 0,
    animate: {},
    collapsible: false,
    event: "click",
    header: "> li > :first-child,> :not(li):even",
    heightStyle: "content",
    icons: {
        activeHeader: "ui-icon-triangle-1-s",
        header: "ui-icon-triangle-1-e"
    },

    // callbacks
    activate: null,
    beforeActivate: null
},


Just call the accordions .resize() method, this will recalculate its size. http://docs.jquery.com/UI/Accordion#method-resize


I recently set up an accordion that retrieves content via ajax when a tab is activated and ran into the same problem. I tried using some of the suggestions posted here, but they never quite grew the panel correctly until I set the heightStyle to content.

$("#myaccordion").accordion({
  heightStyle: "content",
  activate: function(event ui) {
    //use ajax to retrieve content here.
  }
});

I'm using jQuery-UI version 1.10.4.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜