2013-07-04 139 views
0

我有一個網站,100%的高度有一個隱藏的頁腳,需要向上滑動並顯示它,當點擊一個按鈕時,再次點擊該按鈕時,它應該滑下並隱藏它。slideToggle動畫「顯示」而不是「隱藏」

問題是滑動動畫只有在頁腳向上滑動時以及向下滑動時滑動纔會起作用,而不會動畫。

通過單擊頁腳中的「更多」按鈕,您可以看到問題正確here。用來操縱該按鈕 的JS代碼如下:提前

$(document).ready(function(){ 

    $(".footer_container").hide(); 
    $(".show_hide").show();  

    $('.show_hide').click(function(){ 

     var speed = "500"; 
     $(".footer_container").slideToggle(speed); 

     $('html, body').animate({ 
      scrollTop: $(document).height() 
     }, speed); 

    }); 
}); 

謝謝!

更新:我只是嘗試這樣的代碼:

$('.show_hide').click(function(){ 

    var speed = "500"; 
    $(".footer_container").toggle(speed); 

    $('html, body').animate({ 
     scrollTop: $(".footer_container").offset().top + $('window').height() 
    }, speed); 

}); 

而且aparently有一個動畫回事,我不知道存在的頁腳。也許這是造成這個問題的原因?

+0

你給出的鏈接工作正常,再次 –

+0

躲在它的工作對我罰款在Chrome。 –

+0

@KeesSonnema,你有隱藏動畫嗎? – Sergio

回答

1

好了,所以我這給一個鏡頭:

$('.show_hide').unbind() 
$('.show_hide').click(function() { 
    var speed = "500"; 
    $(".footer_container").toggle(speed); 

    if ($(".footer_container").data('can-see')) { 
     var displaced = $('.footer_container').height(); 
     $('.twitter_footer').animate({ 
      marginTop: "600px", 
     }, { 
      duration: speed, 
      complete: function() { 
       $('.twitter_footer').css('margin-top', "0"); 
      } 
     }); 
    } 

    $('html, body').animate({ 
     scrollTop: $(".footer_container").offset().top + $('window').height() 
    }, speed); 

    $(".footer_container").data('can-see', !$(".footer_container").data('can-see')) 

}); 

示範在http://jsfiddle.net/DPq5Z/

相同的結果,另一種方式(使用絕對位置爲了ioning保持原狀以上元素):

$('.show_hide').unbind() 
$('.show_hide').click(function() { 
    var speed = "500"; 
    $(".footer_container").fadeToggle(speed); 

    if ($(".footer_container").data('can-see')) { 
     slide_down('.twitter_footer', speed); 
     slide_down('.button_bg', speed); 

    } 

    $('html, body').animate({ 
     scrollTop: $(".footer_container").offset().top + $('window').height() 
    }, speed); 

    $(".footer_container").data('can-see', !$(".footer_container").data('can-see')) 

}); 

function slide_down(c, speed){ 
    var tp = $(c).offset().top; 
     $(c).css({ 
      'position': 'absolute', 
       'top': tp + "px" 
     }); 
     $(c).animate({ 
      top: tp + 170 + "px", 
     }, { 
      duration: speed, 
      complete: function() { 
       $(c).css({ 
        'position': "relative", 
        'top': '0' 
       }); 
      } 
     }); 
    } 

示範在http://jsfiddle.net/9R6L4/

+0

這太棒了!從來沒有想過它會需要這麼多簡單的效果代碼!我改變了'$(「。footer_container」)。toggle(速度);'用'$(「。footer_container」)。fadeToggle(速度);'它運行的非常漂亮。 – Tiago

+0

我剛剛注意到一個小問題:在[食品](http://dev.digicooltest.com/apoa/food/)頁面上,當頁腳隱藏時,內容也會滾動。你有機會看看這個嗎? – Tiago

+0

結束了使內容位固定的容器,現在會做:)謝謝。 – Tiago

0

它可以像jQuery中的默認動畫一樣工作。如果你想定製這個。您需要使用jQuery緩動插件。它需要緩動作用的參數,比如easeIn,easeOut,Bounce等等,它控制流量。默認情況下,它是線性的,這就是你所看到的。

緩解插件:https://github.com/gdsmith/jquery.easing

$('.show_hide').click(function(){ 

    var speed = "500"; 
    $(".footer_container").fadeToggle(speed); 

    $('html, body').animate({ 
     scrollTop: $(".footer_container").offset().top + $('window').height() 
    }, speed); 

}); 

的jsfiddle:http://jsfiddle.net/vvmYH/4/

+0

但它在slideUp上進行動畫處理,而不是在slideDown上。我用一個不同的代碼更新了我的問題,發現在頁腳上還有另一個動畫,這也許是導致問題的原因。你有沒有檢查出來? – Tiago

+0

檢查我的更新代碼和小提琴..我只是用fadeToggle來代替。 – defau1t

+0

好的,我只注意到你的jsFiddle。你所做的是增加衰落,但那不是我想要完成的。也許我沒有正確解釋自己,對此表示歉意。 單擊「更多」一次後,它向上滑動(帶動畫)。但是,如果您再次單擊滑動,則它沒有幻燈片動畫。 – Tiago