我找到并定制了这个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/