如何调整 jQuery UI 手风琴的高度?

发布于 2024-09-01 07:58:48 字数 549 浏览 5 评论 0原文

在我的用户界面中,我有一个像这样的手风琴设置:

<div id="object_list">
    <h3>Section 1</h3>
    <div>...content...</div>

    // More sections
</div>

手风琴在第一次形成时可以正常工作,并且它似乎可以很好地适应每个部分内的内容。但是,如果我在 .accordion() 调用(通过 ajax)之后向手风琴中添加更多内容,则该部分的内部最终会溢出。

由于手风琴是在几乎没有内容的情况下形成的,因此所有内部 div 都非常小,因此内容会溢出,并且您会得到内部带有滚动条的手风琴,几乎没有查看区域。

我尝试将最小高度样式添加到 object_list div,但内容 div 无济于事。将 min-height 添加到内部 div 有点有效,但它弄乱了手风琴的动画,并将其添加到 object_list div 绝对没有任何作用。

即使没有足够的内容来填充这些部分,如何才能获得合理的内容部分大小?

In my UI I have an accordion setup like this:

<div id="object_list">
    <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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

婴鹅 2024-09-08 07:58:48

autoHeight 在 1.9 中已弃用,并在 1.9 中删除。 com/upgrade-guide/1.10/" rel="noreferrer">1.10。

用途:

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

自动调整内部 div 的大小。

更新:

我发现这仍然是一个非常活跃的帖子,所以我决定确保我的答案仍然有效。看起来这可能不再适用于 jQuery UI 1.11。它指出 [content] 属性已被弃用,并使用 [panel] 代替。使代码片段现在看起来更像这样:

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

我还没有测试这个,只是找到了,当我有时间测试时将返回并删除此评论

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

避讳 2024-09-08 07:58:48

当您声明折叠控件 div 时,您可以在 div 的样式标记中添加高度。然后,您可以设置 fillSpace: true 属性来强制手风琴控件填充该 div 空间,无论如何。这意味着您可以将高度设置为最适合您页面的高度。然后,您可以在添加代码时更改 div 的高度

如果您希望手风琴根据需要动态调整其包含的内容的大小,您可以执行以下技巧 发布在 jQuery UI 网站

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

这意味着当您选择包含大量文本的区域时,手风琴将重新计算它。

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.

绿萝 2024-09-08 07:58:48

从文档听起来你需要设置

clearStyle: true

......而且

autoHeight: false

我相信使用clearStyle可以让你动态添加内容,而不会受到 Accordion 的干扰。

所以试试这个...

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

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 });
小镇女孩 2024-09-08 07:58:48

看起来这里的所有答案现在都使用已弃用的选项。

使用最新版本的 jQuery UI (1.10.x),您应该使用 heightStyle: "fill" 初始化手风琴以获得预期效果。

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

您可以在此处阅读 jQuery UI API 文档了解更多信息: http://api.jqueryui.com/accordion/#option-heightStyle

如果您的页面尺寸动态变化并且如果您需要重新计算手风琴大小,则应使用 refresh 方法刷新您的手风琴:

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

这是首选方法,因为 resize 方法现已弃用。

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.

翻身的咸鱼 2024-09-08 07:58:48

设置 DIV 的高度就可以了。

$(document).ready(function() {

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

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

Setting the DIV's height will do the trick.

$(document).ready(function() {

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

    $("#accordion div").css({ 'height': 'auto' });
});      
放手` 2024-09-08 07:58:48

使用 heightStyle 选项来控制手风琴和每个面板的高度。

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

可能的值:

  1. “auto”:所有面板都将设置为最高面板的高度。
  2. “fill”:根据手风琴的父级高度扩展到可用高度。
  3. “内容”:每个面板的高度取决于其内容。

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.
要走就滚别墨迹 2024-09-08 07:58:48

关闭自动功能将起作用...(使用除自动或填充之外的任何字符串),例如告诉使用“面板”的解决方案。但是...

这与为 "heightStyle" 放入任何垃圾字符串相同。您要查找的 "heightStyle""content"

(选项“heightStyle”的值 在 jQuery UI 1.12 中)

  • "auto":所有面板都将设置为最高面板的高度。
  • "fill":根据手风琴的父高度扩展到可用高度。
  • “内容”:每个面板的高度取决于其内容。

示例:
https://jsfiddle.net/qkxyodpq/5/

希望有所帮助。

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".

(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.

仲春光 2024-09-08 07:58:48

对我来说,以下操作准确无误。

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

});

for me doing following worked accurately.

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

});

夜夜流光相皎洁 2024-09-08 07:58:48

在 jquery-ui.js 中搜索以下部分,并将 heightstyle: "auto" 更改为 heightstyle: "content",因此更新后的文件将如下所示。

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
},

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
},
花海 2024-09-08 07:58:48

只需调用手风琴 .resize() 方法,这将重新计算其大小。
http://docs.jquery.com/UI/Accordion#method-resize

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

淡忘如思 2024-09-08 07:58:48

我最近设置了一个手风琴,当激活选项卡时通过 ajax 检索内容并遇到了同样的问题。我尝试使用此处发布的一些建议,但在我将 heightStyle 设置为内容之前,它们从未完全正确地增长了面板。

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

我正在使用 jQuery-UI 版本 1.10.4。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文