简体   繁体   English

jCarousel没有被隐藏在div中

[英]jCarousel not getting drawn inside a hidden div

I am using a div to populate a ul/li list and then draw a jCarousel out of it. 我正在使用div来填充ul / li列表,然后从中绘制一个jCarousel。 So this works fine: 所以这很好用:

$('#mycarousel').jcarousel();

Here is the problem: 这是问题所在:

The div containing the ul/li items could be hidden by the click of another button. 单击另一个按钮可以隐藏包含ul / li项目的div。 When the div is hidden, and I re-size the browser window, the jCarousel also attempts to redraw itself, but since it is hidden, it is not able to draw it properly. 当div被隐藏,并且我重新调整浏览器窗口的大小时,jCarousel也会尝试重绘自己,但由于它是隐藏的,因此无法正确绘制它。 The result is that everything is jumbled up in the list (if I click the button again to make it visible). 结果是所有内容都在列表中混乱(如果我再次单击该按钮使其可见)。 But again if I re-size the window now (the jumbled up jCarousel is NOT hidden now), it redraws itself correctly. 但是,如果我现在重新调整窗口的大小(混乱的jCarousel现在不被隐藏),它会正确地重绘自己。

I tried getting ahold of the jCarousel instance and reload itself as soon as the button is clicked to make the div visible (the way it re-sizes itself when it is visible and window is re-sized). 我尝试获取jCarousel实例的ahold,并在单击按钮后立即重新加载div以使div可见(当它可见并重新调整窗口大小时重新调整大小的方式)。

To get the jCarousel, I am using: 要获得jCarousel,我正在使用:

JQuery('#mycarousel').data('jcarousel') 

and it is returned as null. 并返回null。

How can I get the jCarousel to draw correctly? 如何才能正确绘制jCarousel?

What makes you assume that the $().jcarousel() call does anything with .data() ? 是什么让你认为$().jcarousel()调用与.data()做任何事情? Better to stick with the API provided by the plugin anyway, rather than guessing at how it works under the hood. 最好还是坚持使用插件提供的API,而不是猜测它是如何工作的。 Anyway, to answer your question... 无论如何,回答你的问题......

The problem is that, when a div is hidden, it has no height or width. 问题是,当隐藏div时,它没有高度或宽度。 Use the "off-left technique" rather than hiding the div, like this: 使用“左偏技术”而不是隐藏div,如下所示:

#mycarousel {
    height: 100px; /* whatever height your div will have when shown */
    width: 100px;  /* whatever width your div will have when shown */
    position: absolute:
    left: -10000px;
}

When you want to show it, use $('#mycarousel').css('position', 'static') to remove the absolute positioning, and the div will jump into place. 当你想要显示它时,使用$('#mycarousel').css('position', 'static')来移除绝对定位,div将跳转到位。

A little more info here . 这里有更多信息。

Little more debugging and found that when the browser ressizes (and the carousel is already visible), its reload function is called to adjust its position, so to help myself in the hide/show div scenario, I ended up calling the carousel api's reload function after the wrapping div becomes visible. 稍微调试一下,发现当浏览器重新设置(并且轮播已经可见)时,会调用其重载函数来调整其位置,所以为了帮助自己隐藏/显示div场景,我最终调用了轮播api的重载功能包装div变得可见之后。

a bit of effort was to actually get hold of the jcarousel instance. 实际上抓住了jcarousel实例需要付出一些努力。 so it was a two step process... 所以这是一个两步的过程......

  1. get hold of the carousel instance. 抓住轮播实例。

      var cInstance = null; cInitCallback = function(c){ cInstance = c; }; $('#mycarousel').jcarousel({ initCallback: cInitCallback, }); 
  2. reload the carousel on the show of the div 在div的节目上重新加载旋转木马

      cInstance.reload(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM