简体   繁体   English

jQuery显示块和fadeIn

[英]jQuery display block and fadeIn

I'm trying to create a very basic tab system in which tab should appear as fadeIn effect. 我正在尝试创建一个非常基本的标签系统,其中的标签应显示为fadeIn效果。

In the following code, .field is set to display:none with css. 在以下代码中, .field设置为display:none with css。 The code would display the tabs correctly, however it just does not show the fadeIn effect. 该代码将正确显示选项卡,但是仅不显示fadeIn效果。

HTML: HTML:

<div class="block">
    <div class="controls">
        <a class="one" href="#">First</a>
        <a class="two" href="#">Second</a>
    </div>
    <div class="field field-one active">Field 1</div>
    <div class="field field-two">Field 2</div>
</div>

jQuery: jQuery的:

$('.controls .one').click(function(){
        $('.field').removeClass('active');
        $('.field-one').addClass('active').fadeIn();
});

$('.controls .two').click(function(){
        $('.field').removeClass('active');
        $('.field-two').addClass('active').fadeIn();
});

CSS: CSS:

.field{
    display: none;
}

.field.active{
    display: block;
}

Demo: 演示:

http://jsfiddle.net/ReyBd/ http://jsfiddle.net/ReyBd/

Can't you use hide and fadeIn only? fadeIn只使用hide and fadeIn吗? is active class really needed? 真的需要活动班吗?

Check this jsfiddle 检查这个jsfiddle

When you add the class active, it displays as block immediately because of you css rule and therefore, it can't fade in. 当您添加活动类时,由于使用css规则,该类立即显示为块,因此无法淡入。

You don't need to add/remove the classes at all, because the fadeIn() method in jQuery automatically handles this for you, by applying an inline style for opacity and display: block, so it overrides your css. 您根本不需要添加/删除类,因为jQuery中的fadeIn()方法通过为opacity和display:块应用内联样式自动为您处理此问题,因此它会覆盖您的CSS。

Also, you can use the fadeToggle method, which checks the state of the element and toggles it. 另外,您可以使用fadeToggle方法,该方法检查元素的状态并进行切换。

So, just do this instead (make sure to leave the first css rule .field {display:none;} ): 因此,请改为执行此操作(确保保留第一个CSS规则.field {display:none;} ):

$('.controls .one').click(function(){
     $('.field').fadeToggle();
});

I tweaked some items positioning using absolute so that the tabbed items fade over each other (rather than stacking like the default behavior is). 我使用绝对值调整了某些项目的位置,以使选项卡式项目彼此渐隐(而不是像默认行为那样堆叠)。

Also, active class is unnecessary for what you are trying to do, but if you need active class for something else that you will be adding later, I left it in. 同样,活动类对于您要执行的操作不是必需的,但是如果您需要活动类用于稍后将添加的其他内容,我将其保留。

Javascript: Javascript:

$('.field').removeClass('active').hide();
$('.field-one').addClass('active').show();

$('.controls .one').click(function(){
        $('.field').removeClass('active').fadeOut();
        $('.field-one').addClass('active').fadeIn();
});

$('.controls .two').click(function(){
        $('.field').removeClass('active').fadeOut();
        $('.field-two').addClass('active').fadeIn();
});

HTML: HTML:

 <div class="block">
        <div class="controls">
            <a class="one" href="#">First</a>
            <a class="two" href="#">Second</a>
        </div>
        <div class="container">
        <div class="field field-one active">Field 1</div>
        <div class="field field-two">Field 2</div>
        </div>
    </div>

CSS: CSS:

.field{ width: 100px;
    height: 100px;
    background: green;
    position: absolute;
    top: 0;
    left: 0; }

.container { position: relative; }

.field.active{  }

.field-two{ background: yellow; }

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

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