2014-12-30 38 views
1

我正在網站上構建投資組合部分,該部分將被限制爲4項。每個項目都有一個可見的圖像和說明,以及默認情況下隱藏的詳細信息部分。使用jQuery在單頁網站上切換內容div

這個想法是當點擊該項目的鏈接時,細節部分淡入。我有一個工作小提琴,我放在一起,但如果已經有一個可見的細節部分,並且您單擊另一個鏈接,則會發生一些粗糙的重疊。

這是我使用來控制每一個細節部分是什麼的一個片段:

你可以在這裏看到小提琴:http://jsfiddle.net/ERP7L/6/

$("a#project-1-link").click(function(){ 
    $("div#project-2-details, div#project-3-details, div#project-4-details").fadeOut("",function(){ 
     $("div#project-1-details").fadeIn(""); 
    }); 
}); 

有沒有更好的方式來完成這一點,將清理增加這個滯後?我嘗試在fadeIn上使用延遲,但它也不起作用。

在此先感謝!

回答

1

你需要使用一個回調的.fadeOut(),這是語法:

$(divToHide).fadeOut(400, function() { 
    $(divToShow).fadeIn(); 
}); 

這僅適用於(順利)如果​​功能只調用一次 - 在可見的元素。所以,最好的辦法是找到可見DIV和目標只是一個,而不是針對所有的-details的div:

var visibleDiv = $('.nav ~ div').filter(function() { return $(this).is(':visible'); }); 

然後回調看起來像這樣:

$(visibleDiv).stop().fadeOut(400, function() { 
    $(divToShow).fadeIn(); 
}); 

我d還加入.stop()以防止多重淡入淡出。

另外,值得注意的是,這是可行的,無需爲每個鏈接創建不同的功能。只需使用類似的東西:

$('.nav a').click(function(event) { 
    var currentElement = $(event.currentTarget); 
    var divToShow = $(currentElement).attr('id').replace('link', 'details'); 
    .... 
}); 

要獲得div,您需要顯示從任何鏈接單擊。因此,所有一起jQuery的樣子:

$('.nav a').click(function(event) { 
    var currentElement = $(event.currentTarget); 
    var divToShow = $(currentElement).attr('id').replace('link', 'details'); 

    var visibleDiv = $('.nav ~ div').filter(function() { return $(this).is(':visible'); }); 
    if (visibleDiv.length) // if there is a div already visible, fade it out then fade in the target div 
    { 
     $(visibleDiv).stop().fadeOut(400, function() { 
      $('#' + divToShow).fadeIn(); 
     }); 
    } 
    else $('#' + divToShow).fadeIn(); // fade it in. 
}); 

我還補充說,if語句,因此有在第一個div褪色沒有延遲。

JSFiddle Here。希望這可以幫助!

+0

這效果很好!唯一不起作用的是我的滾動到錨點(這是一個單獨的腳本,我在我的網站的其餘部分使用了獨立的腳本,從這裏取得:http://css-tricks.com/snippets/jquery/smooth-滾動/)。每個項目鏈接都有一個href =「project-1-details」屬性,應該引起滾動到錨定功能。如果項目1的細節已經可見,並且我點擊了項目1鏈接,這個滾動到錨點的腳本運行良好。但它不會在第一次點擊時滾動。我將如何去添加額外的功能? –

+0

@EricWood我沒有看到代碼就說不出來。關掉我的頭頂,也許會在你淡入目標後觸發'project-link''''標籤的第二次點擊。正如在這個小提琴:http://jsfiddle.net/ERP7L/12/ – bowheart

+0

你可以看到這個問題在這裏播放:http://jsfiddle.net/ERP7L/14/ - 第二次你點擊鏈接,你會得到我正在尋找的行動。我還向關閉按鈕添加了相同的腳本,因此它將您滑回到導航部分,並且這似乎工作得很好。我還專門針對js中的#project-1-details等div,以便頁面上的其他div不受影響。我確實找到了一種方法來添加該函數(請參閱http://jsfiddle.net/ERP7L/15/),但有時它會有點矮胖,直到div已經淡入,纔會滑動。 –

0

一個竅門是使用position: absolute,以便DIV元素(內容)佔據相同的空間並很好地交叉淡入淡出。我還通過使用className而不是id刪除了大量重複代碼。

固定的jsfiddle:​​

可運行的代碼片段:

$(document).ready(function() { 
 
    $("A", ".nav").click(function() { 
 
    var targetId = $(this).data("target"); 
 
    var $targetDiv = $("#project-" + targetId + "-details"); 
 
    $(".project-details").fadeOut(); 
 
    $targetDiv.fadeIn(); 
 
    }); 
 
    $("a.close").click(function() { 
 
    $(".project-details").fadeOut(); 
 
    }); 
 
});
div.nav { 
 
    margin: 20px 0; 
 
} 
 
.project-details { 
 
    position: absolute; 
 
    z-index: 1; 
 
    display: none; 
 
    background-color: black; 
 
    color: white; 
 
    padding: 40px; 
 
} 
 
a.close { 
 
    cursor: pointer; 
 
    background: white; 
 
    color: black; 
 
    padding: 10px; 
 
    position: relative; 
 
    top: -30px; 
 
    left: -40px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> 
 
<div class="nav"> 
 
    <a href="#" id="project-1-link" data-target="1">One</a> 
 
    <a href="#" id="project-2-link" data-target="2">Two</a> 
 
    <a href="#" id="project-3-link" data-target="3">Three</a> 
 
    <a href="#" id="project-4-link" data-target="4">Four</a> 
 
</div> 
 
    <div id="project-1-details" class="project-details"> 
 
    <a class="close">x</a> 
 
    <strong>One.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</div> 
 
    <div id="project-2-details" class="project-details"> 
 
    <a class="close">x</a> 
 
    <strong>Two.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</div> 
 
    <div id="project-3-details" class="project-details"> 
 
    <a class="close">x</a> 
 
    <strong>Three.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</div> 
 
    <div id="project-4-details" class="project-details"> 
 
    <a class="close">x</a> 
 
    <strong>Four.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</div>

0

怎麼樣了這一點:

$('body').on('click', '#nav > a', function(){ 
 
    var detail = $('#nav-details > div:eq(' + $(this).index() + ')'); 
 
    detail.siblings().fadeOut().promise().done(function(){ 
 
    detail.fadeIn(); 
 
    }); 
 
}); 
 
$('body').on('click', '#nav-details .close', function(){ 
 
    $(this).parent().fadeOut(); 
 
});
#nav{ 
 
    margin:20px 0; 
 
} 
 
#nav-details > div{ 
 
    display:none; 
 
    background-color:black; 
 
    color:white; 
 
    padding:40px; 
 
    position: relative; 
 
} 
 
span.close{ 
 
    cursor:pointer; 
 
    background:white; 
 
    color:black; 
 
    padding:10px; 
 
    position:absolute; 
 
    top:0; 
 
    left:0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="nav"> 
 
    <a href="#">One</a> 
 
    <a href="#">Two</a> 
 
    <a href="#">Three</a> 
 
    <a href="#">Four</a> 
 
</div> 
 
<div id="nav-details"> 
 
    <div> 
 
    <span class="close">x</span> 
 
    <strong>One.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 
 
    </div> 
 
    <div> 
 
    <span class="close">x</span> 
 
    <strong>Two.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 
 
    </div> 
 
    <div> 
 
    <span class="close">x</span> 
 
    <strong>Three.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 
 
    </div> 
 
    <div> 
 
    <span class="close">x</span> 
 
    <strong>Four.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 
 
    </div> 
 
</div>

+0

親愛的downvoter,我很樂意知道你的理由。 – Taxellool

+0

我也是如此,從我這裏得到讚賞 –

+0

是的,我覺得有些嫉妒的靈魂只是通過了所有的事情。 – bowheart

0

嗯,首先。如果您想對每個元素執行相同的操作,請嘗試使用類。 (同樣的動作我的意思是:打開和關閉)

所以,你仍然可以使用ID作爲一個非常具體的原因,併爲通用的類。

我試過做更簡單,沒有很多修改的HTML部分。但我建議您將鏈接和細節放入容器中。

下面是一個解決方案,我知道它似乎很複雜...也許與fadeToggle會更好。但是需要修改DOM。

這是你的HTML(使用類和數據屬性):

<div class="nav"> 
<a href="#" id="project-1-link" class="project" data-id="1"> 
    One  
</a> 


<a href="#" id="project-2-link" class="project" data-id="2"> 
    Two  
</a> 


<a href="#" id="project-3-link" class="project" data-id="3"> 
    Three 
</a> 

<a href="#" id="project-4-link" class="project" data-id="4"> 
    Four 
</a> 




<div id="project-1-details" class="details"> 
    <a class="close">x</a> 
    <strong>One.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 
</div> 

<div id="project-2-details" class="details"> 
    <a class="close">x</a> 
    <strong>Two.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 
</div> 

<div id="project-3-details" class="details"> 
    <a class="close">x</a> 
    <strong>Three.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 
</div> 


<div id="project-4-details" class="details"> 
    <a class="close">x</a> 
    <strong>Four.</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 
</div> 

數據-ID是要知道這個數字我打電話,我們要使用它的腳本,所以您不必爲每個元素編寫一行代碼。

這裏的腳本:

$(function(){ 
    $(".project").on("click", function(){ 

    // HERE WE TAKE THE NUMBER OF THE ID FROM THE CLICKED OBJECT 

    var dataid = $(this).attr("data-id"); 

    // THEN WE FADE OUT EVERYTHING IS VISIBLE 
    $(".details").fadeOut(); 

    //HERE, WE'LL TAKE THE ONE WE'RE GONNA FADE IN 
    setTimeout(function(){ 
     $("#project-"+dataid+"-details").fadeIn(); 
    },400) 
    }); 
}) 

而這裏的的jsfiddle:

http://jsfiddle.net/mpcguc4k/

對不起,長的答案。