jquery - 向JQuery添加不同的 "open"和 "if already open"效果

标签 jquery html effects

我找到并定制了这个JQuery脚本,当按下不同的链接时,它会显示不同的内容。

但是,当 targetDiv 已经可见时,我希望更改效果是淡入淡出,因此当您打开和关闭 targetDiv 时它只是滑动效果。

此外,我不明白为什么内容在页面加载后大约半秒内就可见。我可以避免这种情况吗?

这是 JSFiddle 上的:http://jsfiddle.net/XwN2L/179/

JQuery(最新版本):

jQuery(function(){
    jQuery('.targetDiv').hide();

    jQuery('#close').click(function(){
              jQuery('.targetDiv').slideUp();
    });

        jQuery('.showSingle').click(function(){
              jQuery('.targetDiv').hide();
              jQuery('#div'+$(this).attr('target')).slideDown();
        });
});

HTML:

<div class="buttons">
<a  class="showSingle" target="1">Div 1</a>
<a  class="showSingle" target="2">Div 2</a>
<a  class="showSingle" target="4">Div 4</a>
<a id="close">Close</a>
</div>

<br><br><br><br>
Lorem Ipsum<br>

<div id="div1" class="targetDiv">
<iframe width="420" height="315" src="http://www.youtube.com/embed/c1_LON8Ib2o" frameborder="0" allowfullscreen></iframe>
</div>

<div id="div2" class="targetDiv">
<iframe width="420" height="315" src="http://www.youtube.com/embed/s4GbpG-PypM" frameborder="0" allowfullscreen></iframe>
</div>

<div id="div4" class="targetDiv">
<iframe width="560" height="315" src="http://www.youtube.com/embed/DHef3iAjxiM" frameborder="0" allowfullscreen></iframe>
</div>
Lorem Ipsum

最佳答案

工作演示 http://jsfiddle.net/efgFL/2/

使用is(':visible')链接:http://api.jquery.com/visible-selector/

对于第二部分,blah 文本不在任何发生显示/隐藏的容器中,这就是原因。

希望这有帮助,

代码

jQuery(function(){
    jQuery('.targetDiv').hide();

    jQuery('#close').click(function(){
              jQuery('.targetDiv').slideUp();
    });

        jQuery('.showSingle').click(function(){
            if( jQuery('.targetDiv').is(':visible')){
                jQuery('.targetDiv').hide();
               jQuery('#div'+$(this).attr('target')).fadeIn();
            }else{
              jQuery('.targetDiv').hide();
              jQuery('#div'+$(this).attr('target')).slideDown();
            }
        });
});​

关于jquery - 向JQuery添加不同的 "open"和 "if already open"效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11168975/

相关文章:

javascript - Shadowbox 在 IE 中不工作

jquery - 具有自动刷新功能的 MVC 部分 View

javascript - ASP.NET初学者关于悬停效果的问题

action - NGRX - 对单一效果执行多个操作

jquery - 如何在jquery上一一显示每个div?

javascript - 从打开的函数中关闭花式框 'fancybox'

javascript - 使用 jquery 设置 MVC CheckBoxFor 值

python - 在 Python 上使用 Netscape 书签文件格式?

ios - Phonegap 2.2.0 iOS文件传输错误代码2

html - CSS GRID - 具有空单元格和自动放置的网格模板区域