2011-04-06 60 views
0

我有一個腳本,它將在未選中時更改元素的不透明度。這部分腳本工作正常。不過,我還希望在單擊某個元素時出現隱藏的div。我有這樣的計劃,當點擊第一個元素(這是一個圖片)時,第一個隱藏的div將出現,當第二個圖片被點擊時,第二個隱藏的div出現。我認爲使用類或類似的東西來檢查div上的給定ID的不透明度,然後是顯示div的< 1可能會更有用。問題在於,我不知道如何只保留一個div在頁面上可見。我嘗試了一些東西,現在這個腳本不起作用,但它有點接近。使用eq()和index()顯示隱藏div的問題

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> 
<style> 
.opac { 
     filter:alpha(opacity=50); 
     -moz-opacity:0.5; 
     -khtml-opacity: 0.5; 
     opacity: 0.5; 
     border:thick; 
} 
.hidden{ 
    display:none; 
} 

</style> 
<script> 
$(document).ready(function(){ 
    $('a.images').(function(){ 
     // Make all images (except this) transparent 
     $('a.images').not(this).stop().animate({opacity: 0.15}, 300); 
     // Make this opaque 


     $('a.images').each(function(e){ 
      $(this).bind('click', function(e){ 
       var hideIs = $(this).index(); 
       $('.hidden').eq(hideIs).show(250); 
      }); 
     $(this).stop().animate({opacity: 1.0}, 300); 
    }); 

}); 


}); 

</script> 

</head> 

<body> 
<div id="images"> 
<a class="images" href="#"><img src="../appalachia.jpg" height="133" /></a> 
<div class="hidden" >text here</div> 
<a class="images" href="#"><img src="../appalachia.jpg" height="133" /></a> 
<div class="hidden">second text here</div> 
<a class="images" href="#"><img src="../appalachia.jpg" height="133" /></a> 
<div class="hidden">third text here 
<a class="images" href="#"><img src="../appalachia.jpg" height="133" /></a> 
<div class="hidden" >fourth text here</div> 
</div> 

</body> 
</html> 
+2

你的意思'$( 'a.images')。每個(函數(){',而不是'$( 'a.images')。(函數( ){'? – climbage 2011-04-06 18:27:38

回答

1

你確定你不只是想使用標籤插件?這是他們設計要完成的行爲,而且他們已經具備了所有這些效果。

http://jqueryui.com/demos/tabs/

http://flowplayer.org/tools/tabs/index.html

jQuery的工具選項卡插件只有0.9 KB!

+0

我一定要看看,但爲了我自己的好奇心, d想知道我錯在哪裏:) – dzilla 2011-04-06 18:36:54

+0

對不起,我刪除了我以前的評論 - 您的標記出現錯誤(第三個DIV未關閉),所以我的演示鏈接實際上並未起作用。爲了明智地實現這一點,你應該看看上面鏈接的兩個腳本的來源,但是你可以在這裏找到你想要做的事情的最佳方式:http:// jsfiddle .net/AFLLT/ – schizodactyl 2011-04-06 19:40:10

+0

這就是我一直在尋找的! – dzilla 2011-04-06 20:01:20

0

代替

$('.hidden').eq(hideIs).show(250); 

使用本:

$('.hidden.shown').removeClass('shown').hide(250); 
$('.hidden').eq(hideIs).addClass('shown').show(250); 

該會工作

做些什麼:它實際上是 '標記' 前面顯示的div爲shown,使在它標記另一個之前,它隱藏了前一個。

+0

您的解決方案適用於顯示div,但它使我失去了不透明度功能,它在實際顯示內容之前也彈跳了好幾次,我不知道爲什麼會發生這種情況 – dzilla 2011-04-06 18:38:10

+0

這裏是一個小提琴http: //jsfiddle.net/dzilla/ZZm7V/,我可以讓div顯示,而div會淡入淡出,但不是在同一時間。 – dzilla 2011-04-06 18:49:26