简体   繁体   English

实现更改fadeIn的最佳方法fadeOut内容菜单Jquery

[英]Best way to implement change fadeIn fadeOut Content menu Jquery

Looking the best way to implement a menu with javascript (jQuery), where i can change the content clicking on a menu, using a fade out fadeIn effect, that is in html hidden. 寻找用javascript(jQuery)实现菜单的最佳方法,在其中我可以使用淡出的fadeIn效果(在html中隐藏)来更改单击菜单上的内容。

IDEA 理念

Have a menu , when I click want to active the button and the content, and using a fadeOut effect and replace for the content that I clicked. 有一个菜单,当我单击要激活按钮和内容时,并使用fadeOut效果替换为我单击的内容。 ( Could add the active class for better play with css) (可以添加活动类以更好地与css一起玩)

PROBLEMS 问题

The main problem I have is since it have a time to fadeOut and fadeIn the content appear before the other disappearing the other content. 我的主要问题是因为它有时间淡出和淡入内容,在其他内容消失之前先出现。

SOLUTION LOOKING 解决方案查找

I'm looking the best way to implement : 我正在寻找实现的最佳方法:

  • The menu, if "href" is the best way to get the content to show 菜单(如果“ href”是使内容显示的最佳方法)

  • Solve the main problem of the fade out fade in effect.Special if you're fast clicking between button, it seems to break (I increase a little the time in the example to check it better) 解决淡入淡出效果的主要问题。特别是如果您快速在按钮之间单击,它似乎会中断(我在示例中增加了一点时间来更好地检查它)

  • Add the class "active" in the menu and gallery 在菜单和图库中添加“活动”类

The small example to start here 这里开始的小例子

What you need to use is the fadeIn and fadeOut callback property. 您需要使用的是fadeIn和fadeOut回调属性。 With this you can order them properly and have them behave the way you want, like so: 这样,您就可以正确地订购它们,并让它们按照您想要的方式运行,如下所示:

$('#button').click(function(){
    $(this).fadeOut('fast', function(){
         $(this).load('yourcontent.php', function(){
              $(this).fadeIn('fast');
              $(this).addClass('active');
         }
    }
}

If you're using the load function to load your content, the above code should prove helpful. 如果您正在使用load函数来加载内容,那么上面的代码应该很有帮助。 If however you're using static content per button, you can also use the .html function to load the content in the button. 但是,如果每个按钮都使用静态内容,则还可以使用.html函数将内容加载到按钮中。

You can call the button with a jQuery selector, like i did in the above example or you can use the onclick attribute: 您可以使用jQuery选择器调用按钮,就像我在上面的示例中所做的那样,也可以使用onclick属性:

<a onclick="example('one')">Test Button</a>

And then the javascript would be: 然后,JavaScript将是:

function example(id){
     $('#' + id).fadeOut('fast', function(){
         $(this).load('yourcontent.php', function(){
             $(this).fadeIn('fast');
             $(this).addClass('active');
         }
     }
}

UPDATE 3 更新3

I updated the above function to do what i think you meant, with the gallery's id as the parameter. 我更新了上面的功能,按照画廊的id作为参数,按照我的意思做。 This time with the stop function. 这次具有停止功能。

$('#menu li').click(function(){
    $('.gallery').queue("fx", []);
    $('.gallery').stop(true, true);
    var which = $('a', this).attr('href'); 
    $('.gallery.active').fadeOut(2000, function(){
        $(this).removeClass('active');
        if($(which).attr('class') != 'active'){
            $(which).fadeIn(2000, function(){
                $(this).addClass('active');
            });
        }
    });
});

if i understand your question correctly, you want to stop there being such a delay when one gallery disappears and another fades in. the easiest way to do this is to hide the currently showing gallery (rather than fadeOut ) and then fadeIn the newly selected gallery. 如果我正确理解了您的问题,那么您想停止一个画廊消失而另一画廊消失时出现这样的延迟。最简单的方法是隐藏当前显示的画廊(而不是fadeOut ),然后fadeIn新选择的画廊。 If you change your code javascript to this, you will see the result is a lot smoother. 如果将代码javascript更改为此,您将看到结果更加平滑。

  $('#menu li').click(function(event){
      event.preventDefault();
      var $this = $(this);
      var href=  $this.find('a').attr('href');
      $(".gallery").hide();
      $(href).fadeIn(1000);  
  });

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

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