2015-04-23 68 views
0

Heey大家,中心DIV水平scolling

我建立這個水平滾動的網站,但是當我點擊從導航欄的鏈接上,在div出現在屏幕的右側,而不是在中心。有誰知道如何解決這一問題?

這裏是jQuery代碼我用:

$(function() { 
      $('.fixed-nav-bar li, .fixed-nav-bar a').bind('click',function(event){ 
       var $anchor = $(this); 

       $('html, body').stop().animate({ 
        scrollLeft: $($anchor.attr('href')).offset().left 
       }, 1500,'easeInOutExpo'); 

       $('html, body').stop().animate({ 
        scrollLeft: $($anchor.attr('href')).offset().left 
       }, 1000); 
       event.preventDefault(); 

      });    

提前感謝!

+2

我們可以通過jsFiddle和HTML以及CSS看到問題嗎? – Nitesh

+1

此外,jQuery建議使用.on(),因爲jQuery 1.7+以上的.bind()http://api.jquery.com/on/ – Mortaza

回答

1

scrollLeft$($anchor.attr('href')).offset().left + ($(window).width() - $($anchor.attr('href')).width()) * 0.5

的部分的加計算後的窗口寬度中減去所述元件的寬度,並將該差scrollLeft的一半,導致靶在中心之中。

你可能應該清理一下我的示例代碼,使它更具可讀性,但這個概念應該清楚。

+0

謝謝!有用! – user4796074