最初隐藏时,Flexslider无法正常工作

时间:2013-09-13 11:28:02

标签: jquery flexslider

最初我隐藏了我的flexslider,直到用户点击链接然后它才会打开。但由于某种原因,滑块在显示后才会工作,直到我调整浏览器窗口,这很奇怪!当我不隐藏flexslider它工作正常。下面是我用来打开和关闭flex滑块的CSS,HTML和jquery。任何熟悉flex滑块的人都会知道为什么会这样?谢谢!

CSS

section#hotspots .dropdown-area {   
    width: 100%;
    float: left;
    display: none;
}

HTML

<h1>Hot Spots <span class="toggle-button"></span></h1>

<section class="dropdown-area">
    <div class="flex-container">
        <div class="flexslider" id="secondary-slider">
            <ul class="slides">
                <li><img src=
                "%3C?php%20bloginfo('template_directory');%20?%3E/images/212/Portland-1.jpg"></li>

                <li><img src=
                "%3C?php%20bloginfo('template_directory');%20?%3E/images/212/Portland-2.jpg"></li>

                <li><img src=
                "%3C?php%20bloginfo('template_directory');%20?%3E/images/212/Portland-3.jpg"></li>
            </ul>

            <ul class="flex-control-nav">
                <li>
                    <a href="">LOLA COFFEE</a> /
                </li>

                <li>
                    <a href="">PUBLIC MARKET CAFE</a> /
                </li>

                <li>
                    <a href="">MATT'S BIG BREAKFAST</a> /
                </li>
            </ul>
        </div><!--End Flexslider-->
    </div><!--End Flex-container-->
</section>

JS

$(window).load(function() {
$("#hotspots .toggle-button").click(function() {
    $("#hotspots .dropdown-area").slideToggle("slow");
    $("#hotspots span").toggleClass("toggle-button close");
});
});

3 个答案:

答案 0 :(得分:1)

我假设您使用的是最新版本的flexslider,在这种情况下,您可以使用更高效的setup()。我还创建了一个单独的事件来在第一次点击.toggle按钮时运行setup()函数。

这样的事情:

$toggleButtons = $('#hotspots .toggle-button');

//execute once, then remove this event
$toggleButtons.on('click.flexSetup', function() { 
   $('.flexslider').data('flexslider').setup();
   //remove this click event
   $toggleButtons.off('click.flexSetup');
});

//your regular click events
$toggleButtons.on('click', function() { 
    $("#hotspots .dropdown-area").slideToggle("slow");
    $("#hotspots span").toggleClass("toggle-button close");
});

当然,如果你想要快速而肮脏的解决方案,你可以使用绝对定位而不是隐藏flexslider。

section#hotspots .dropdown-area {  
   position: absolute; left: -10000px;
}

当你想要“显示”flexslider时,只需将position和left属性设置为默认的css值。 (position: static&amp; left: auto在您的情况下)

答案 1 :(得分:0)

这是因为flexslider在页面加载时初始化但具有隐藏内容。因此,您只需要在切换点击时使用init flexslider:

$(window).load(function() {
  var fs_init = 0; //init var
  $("#hotspots .toggle-button").click(function () {
      $("#hotspots .dropdown-area").slideToggle("slow");
      $("#hotspots span").toggleClass("toggle-button close");
      if (fs_init == 0) {fs_init++; //to prevent other initializations
        $('.flexslider').flexslider({
            animation: "slide" //enter you params here
        });            
      }
  });
});

注意:不要将滑块初始化两次,只是来自切换事件,而不是onLoad

答案 2 :(得分:0)

我努力在手风琴/崩溃的点击事件上调整.flexslider的大小

$('.acordeon-trigger').click(function () {
     // here all your accordion stuff
     $('.flexslider').resize(); // this is it
});
相关问题