2012-11-13 94 views
0

我使用jQuery和Ajax創建一個抽屜(#DrawerContainer)和加載內容到它,如果我在畫廊點擊縮略圖事件的作品。我的功能已接近完成,但如果再次單擊打開按鈕(現在#current),我希望能夠關閉該抽屜。兩個div點擊只在一個DIV

這是我的代碼jsfiddle:http://jsfiddle.net/RF6df/54/
如果您點擊一個方形/縮略圖,它是藍色的矩形,出現抽屜元素。 當前縮略圖變爲綠色。

我加入我的抽屜裏一個按鈕(的jsfiddle不可見)將其關閉。爲了這個目的,我使用這部分代碼,它的功能就像一個魅力。

 // Close the drawer 
     $(".CloseDrawer").click(function() { 
      $('#DrawerContainer').slideUp() 
       setTimeout(function(){ // then remove it... 
        $('#DrawerContainer').remove(); 
       }, 300); // after 500ms. 
      return false; 
     }); 

現在我需要我的#current股利能夠以上關閉#DrawerContainer代碼以同樣的方式.CloseDrawer一樣。
不幸的是添加第二個觸發這樣$("#current,.CloseDrawer").click(function()到我的功能無法正常工作......當我點擊「當前」的縮略圖,它只是重新打開抽屜,而不是關閉它...

如何修改我的代碼用「當前」縮略圖關閉我的#DrawerContainer?

請記住,我正在學習jQuery的,所以如果你可以發表評論它可能是一個很大的幫助。並且請不要修改我的標記或CSS,因爲一切都在關閉部分旁邊工作。

+0

您可以鏈接到該網站您在本實施中,或者將按鈕添加到的jsfiddle,好嗎? – SeinopSys

+0

我在本地工作,但正方形是jsfiddle上的按鈕。 – wyem

+0

我看到的是'$( 「#當前,.CloseDrawer」)內的代碼點擊(函數(){...});'根本不能運行。添加一個'alert()'並親自查看。 – SeinopSys

回答

0

按我的理解,你可以使用「切換()」函數,它不完全一樣(即,切換可視性)。

$('#DrawerContainer').toggle(); 

編輯: 更新了腳本工作。

$(document).ready(function() { 

    $.ajaxSetup({cache: false}); 

    $('#portfolio-list>div:not(#DrawerContainer)').click(function() { 
     if ($(this).attr("id") != "current") 
     { 
    // modify hash for sharing purpose (remove the first part of the href) 
    var pathname = $(this).find('a')[0].href.split('/'), 
      l = pathname.length; 
     pathname = pathname[l-1] || pathname[l-2]; 
     window.location.hash = "#!" + pathname; 

    $('#current').removeAttr('id'); 
    $(this).attr('id', 'current'); 

    // find first item in next row 
    var LastInRow = -1; 
    var top = $(this).offset().top; 
    if ($(this).next().length == 0 || $(this).next().offset().top != top) { 
     LastInRow = $(this); 
    } 
    else { 
     $(this).nextAll().each(function() { 
      if ($(this).offset().top != top) { 
       return false; // == break from .each()    
      } 
      LastInRow = $(this); 
     }); 
    } 
    if (LastInRow === -1) { 
     LastInRow = $(this).parent().children().last(); 
    } 

    // Ajout du drawer 
    var post_link = $(this).find('.mosaic-backdrop').attr("href"); 
     $('#DrawerContainer').remove(); // remove existing, if any 
     $('<div/>').attr('id', 'DrawerContainer').css({display: 'none'}).data('citem', this).html("loading...").load(post_link + " #container > * ").insertAfter(LastInRow).slideDown(300); 
     return false; // stops the browser when content is loaded 
    } 
    else { 
      $('#DrawerContainer').slideUp(300); 
     $(this).removeAttr("id"); 
    } 

    }); 

    $(document).ajaxSuccess(function() { 

     Cufon('h1'); //refresh cufon 

     // Toggle/close the drawer 
     $("#current,.CloseDrawer").click(function() { 
      $('#DrawerContainer').slideToggle() 
       setTimeout(function(){ // then remove it... 
        $('#DrawerContainer').remove(); 
       }, 300); // after 500ms. 
      return false; 
     }); 

    }); 

    //updated Ene's version 
    var hash = window.location.hash; 
    if (hash.length > 0) { 
     hash = hash.replace('#!' , '' , hash); 
     $('a[href$="'+hash+'/"]').trigger('click'); 
    } 

}); 

而且,在這裏更新它:Updated JS Fiddle

編輯-2:Updated Link

希望這有助於!

+0

謝謝Praveen,你是最接近的!我修改了一下你的代碼,否則如果我們加載一個特定的URL,抽屜不會被打開。我現在只有一個問題。我必須點擊兩次才能關閉抽屜,當它關閉時,我無法再打開它了......在我學習的過程中,我很難找到它的來源......請問您有看? http://jsfiddle.net/RF6df/45/ – wyem

+0

用新腳本更新了答案並提供了鏈接。希望這能解決您的問題 – Praveen

+0

感謝您抽出時間,但現在我甚至無法打開抽屜。:(不知道它來自哪裏,我稍後會看看。順便說一句,這裏是我的實際代碼http://jsfiddle.net/RF6df/54/仍是最後的問題,但我懷疑jsfiddle沒有考慮的ajaxSuccess ... – wyem