如何向UI选项卡添加交叉淡入淡出?

时间:2011-01-09 13:17:16

标签: jquery jquery-ui jquery-ui-tabs fade

您好我从http://forum.jquery.com/topic/how-to-add-a-cross-fade-to-ui-tabs复制问题,因为我有同样的问题。

大家好

我已使用标准UI标签代码设置了标签式界面。

<script type="text/javascript">
$(function() {
$("#tabbedArea").tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 6000, 'true')
});
</script>

目前,作为一个显示的标签淡出,在下一个标签出现之前留下一个空格。

我希望发生的事情是tab1淡出,tab 2淡入 - 创建交叉淡入淡出。

谁能告诉我如何实现这个目标?

提前致谢

3 个答案:

答案 0 :(得分:6)

我认为Jquery UI默认情况下无法执行此操作。但是jQuery Tabs UI comes with a event "show",您可以在其中编写一些自定义代码来自行切换标签。

$(document).ready(function() {
    $("#tabs").tabs({

        show: function(event, ui) {

            var lastOpenedPanel = $(this).data("lastOpenedPanel");

            if (!$(this).data("topPositionTab")) {
                $(this).data("topPositionTab", $(ui.panel).position().top)
            }         

            //Dont use the builtin fx effects. This will fade in/out both tabs, we dont want that
            //Fadein the new tab yourself            
            $(ui.panel).hide().fadeIn(500);

            if (lastOpenedPanel) {

                // 1. Show the previous opened tab by removing the jQuery UI class
                // 2. Make the tab temporary position:absolute so the two tabs will overlap
                // 3. Set topposition so they will overlap if you go from tab 1 to tab 0
                // 4. Remove position:absolute after animation
                lastOpenedPanel
                    .toggleClass("ui-tabs-hide")
                    .css("position", "absolute")
                    .css("top", $(this).data("topPositionTab") + "px")
                    .fadeOut(500, function() {
                        $(this)
                        .css("position", "");
                    });

            }

            //Saving the last tab has been opened
            $(this).data("lastOpenedPanel", $(ui.panel));

        }

    });
});

现场演示:

http://jsfiddle.net/FFTzU/7/

答案 1 :(得分:2)

很棒的回答理查德正是我需要的!我有一个想法,你的解决方案调用2个动画(淡出和淡入),这在我的js重页上看起来有点不太顺利。我稍微编辑了你的解决方案,使用z-index和1 fade。这至少让我的事情变得更顺畅。

$("#tabs").tabs({
  show: function(event, ui) {
    var lastOpenedPanel = $(this).data("lastOpenedPanel");
    if (!$(this).data("topPositionTab")) {
        $(this).data("topPositionTab", $(ui.panel).position().top)
    }
    // do crossfade of tabs
    $(ui.panel).hide().css('z-index', 2).fadeIn(1000, function() {
      $(this).css('z-index', '');
      if (lastOpenedPanel) 
      {
        lastOpenedPanel
          .toggleClass("ui-tabs-hide")
          .hide();
      }
    });

    $(this).data("lastOpenedPanel", $(ui.panel));
  } 
}).tabs('rotate', 3000);

我在最后添加了旋转,因为这样可以获得相当不错的幻灯片!

汤姆

答案 2 :(得分:0)

我不知道以上答案是否适用于早期版本的jQuery UI,但是showhide属性目前无法以这种方式工作。可以使用beforeActivate来达到效果:

$("#tabbedArea").tabs({
    hide: 1000,
    beforeActivate: function(event, ui){
        ui.newPanel.css('zIndex', 1).fadeIn(1000, function(){
            $(this).css('zIndex', '');
        });
    }
});

请注意,标签面板必须绝对放置。

相关问题