2012-05-11 221 views
10

我正在使用TW Bootstrap的選項卡來標記我的內容clients' site我設置了HTML標記以刪除「數據切換」,因爲我需要初始化jScrollpane庫單擊。Twitter Bootstrap Tabs href =「#」錨定標記​​跳躍

我已經得到這個工作,但是當你點擊其中一個導航圖標時,頁面跳下去。

如何避免發生這種情況?

我的標記如下:

HTML

<ul class="nav nav-tabs"> 
      <li class="home_tab active"><a href="#home"></a></li> 
      <li class="about_tab"><a href="#about"></a></li> 
      <li class="services_tab"><a href="#services"></a></li> 
      <li class="cases_tab"><a href="#case_studies"></a></li> 
      <li class="contact_tab"><a href="#contact_us"></a></li> 
      <li class="news_tab"><a href="#news"></a></li> 
</ul> 

<div class="tab-content"> 
      <div id="home" class="tab-pane active scroll_tab"> 
      <?php if (have_posts()) while (have_posts()) : the_post(); ?> 
      <h2> 
       <?php the_title(); ?> 
      </h2> 
      <?php the_content(); ?> 
      <?php endwhile; ?> 
      </div> 
      <div id="about" class="tab-pane"> 
      <?php 
       $page_id = 9; 
       $page_data = get_page($page_id); 
       echo '<h2>'. $page_data->post_title .'</h2>';// echo the title 
       echo apply_filters('the_content', $page_data->post_content); // echo the content and retain Wordpress filters such as paragraph tags. 
      ?> 
      </div> 
      <div id="services" class="tab-pane"> 
      <div class="signs"> 
      <ul class="nav-tabs sub_tabs"> 
       <li class="roll_labels"><a data-toggle="tab" href="#roll_labels"></a></li> 
       <li class="sheeted_labels"><a data-toggle="tab" href="#page1"></a></li> 
       <li class="fanfold_labels"><a data-toggle="tab" href="#page1"></a></li> 
       <li class="printers"><a data-toggle="tab" href="#page1"></a></li> 
      </ul> 
      </div> 
      <?php 
       $page_id = 11; 
       $page_data = get_page($page_id); 
       echo '<h2>'. $page_data->post_title .'</h2>';// echo the title 
      echo apply_filters('the_content', $page_data->post_content); // echo the content and retain Wordpress filters such as paragraph tags. 
     ?> 
     </div> 
     <div id="case_studies" class="tab-pane"> 
     <?php 
      $page_id = 13; 
      $page_data = get_page($page_id); 
      echo '<h2>'. $page_data->post_title .'</h2>';// echo the title 
      echo apply_filters('the_content', $page_data->post_content); // echo the content and retain Wordpress filters such as paragraph tags. 
     ?> 
     </div> 
     <div id="contact_us" class="tab-pane"> 
     <?php 
      $page_id = 15; 
      $page_data = get_page($page_id); 
      echo '<h2>'. $page_data->post_title .'</h2>';// echo the title 
      echo apply_filters('the_content', $page_data->post_content); // echo the content and retain Wordpress filters such as paragraph tags. 
     ?> 
     </div> 
     <div id="news" class="tab-pane"> 
     <?php 
      $page_id = 144; 
      $page_data = get_page($page_id); 
      echo '<h2>'. $page_data->post_title .'</h2>';// echo the title 
     ?> 
     <?php 
      // Load Latest Blog - Limited to 2 items           
      $recent = new WP_Query("tags=blog&showposts=2"); while($recent->have_posts()) : $recent->the_post();?> 
     <div class="news_excerpt"> 
      <h3><?php the_title(); ?></h3> 
      <p><?php echo limit_words(get_the_excerpt(), '40'); ?> ...</p> 
      <a data-toggle="modal" href="#newsModal-<? the_ID(); ?>" id="newspopup"> 
        <img src="<?php bloginfo('template_url'); ?>/assets/img/content/team_read_more.png" alt="Read More" style="border:none;"> 
      </a> 
      <div class="modal hide fade" id="newsModal-<? the_ID(); ?>"> 
      <div class="modal-header"> 
       <button data-dismiss="modal" class="close">×</button> 
       <h3><?php the_title(); ?></h3> 
      </div> 
      <div class="modal-body"> 
       <p><?php the_post_thumbnail('news_image'); ?></p> 
       <p><?php the_content(); ?></p> 
      </div> 
      </div> 
      <?php endwhile; ?> 
     </div>   
     </div> 
     <div id="roll_labels" class="tab-pane"> 
     <?php 
      $page_id = 109; 
      $page_data = get_page($page_id); 
      echo '<h2>'. $page_data->post_title .'</h2>';// echo the title 
      echo apply_filters('the_content', $page_data->post_content); // echo the content and retain Wordpress filters such as paragraph tags. 
     ?> 
     </div> 
    </div> 

jQuery的

$('.nav-tabs li a').click(function (e) { 
     $(this).tab('show'); 
     $('.tab-content > .tab-pane.active').jScrollPane(); 
    }); 

就像我說的,我如何防止頁面內容從 「跳樓」 到錨?非常感謝..

回答

21
$('.nav-tabs li a').click(function (e) { 
    e.preventDefault(); 
    $(this).tab('show'); 
    $('.tab-content > .tab-pane.active').jScrollPane(); 
}); 

使用e.preventDefault()。它可以防止默認操作(在這種情況下,「導航」到#)

+1

啊哈!想知道e.preventDefault()做了什麼。不應該從文檔中將其從我的副本中刪除。很多謝謝 – StuBlackett

+0

你是我的英雄 – backman

+0

這個答案在Chrome上完美運行。但是,它不適用於Firefox。 – Aryo

1

從@ paulslater19選擇的答案不適用於我。以下代碼訣竅:

<script> 
    $(document).ready(function() { 
    var hash = window.location.hash; 
    var link = $('a'); 
    $('.nav-tabs > li > a').click(function (e) { 
     e.preventDefault(); 
     hash = link.attr("href"); 
     window.location = hash; 
    }); 
    }) 
</script> 

另請參閱SO question 14185974

1

@ paulslater19的回答對我不起作用。但是,下面的代碼的確如此:

$('.nav-tabs li a').click(function(e) { 
    history.pushState(null, null, $(this).attr('href')); 
}); 

在Bootstrap 3.2.0上測試。我有這個代碼Twitter Bootstrap: How To Prevent Jump When Tabs Are Clicked

UPDATE

完整的功能,用於引導3.2.0:

$().ready(function() { 
     // store the currently selected tab in the hash value 
     $("ul.nav-tabs > li > a").click(function (e) { 
      $(this).tab('show'); 
      history.pushState(null, null, $(e.target).attr("href")); 
     }); 

     // on load of the page: switch to the currently selected tab 
     $('ul.nav-tabs a[href="' + window.location.hash + '"]').tab('show'); 
    }); 
+0

我會想象你的答案在較新版本的Bootstrap上工作。認爲檢查答案的日期是關鍵。 – StuBlackett

+0

這對我來說不適用於boostrap 3.0和jquery 2.0.3。跳躍停止,但不顯示選項卡。嘗試在history.pushState之後添加.tab('show'),但它只是重新創建行爲者。 – Tofuwarrior

+1

@Tofuwarrior我編輯了bootstrap 3.2.0的完整函數的答案。 –

11

使用數據目標,而不是HREF似乎與引導3丸和標籤的工作。如果鼠標光標沒有因爲沒有href屬性而改變,那麼你可以添加一點css

編輯: 根據下面的請求添加代碼,注意缺少href上的目標和中,加數據目標=「#的...

 <ul class="nav nav-tabs" > 
    <li class="active"><a href="#" data-target="#tab_id_1" data-toggle="tab"> Tab Name 1</a></li> 
    <li><a href="#" data-target="#tab_id_2" data-toggle="tab"> Tab Name 2 </a></li> 
    </ul> 
    <div class="tab-content"> 
     <div id="tab_id_1" class="tab-pane active"> 
       CONTENT 1 
      </div> 
      <div id="tab_id_2" class="tab-pane"> 
       CONTENT 2 
      </div> 
    </div> 
+2

添加示例代碼。 –

+0

這對我來說很好,使用e.preventDefault()不起作用。謝謝! – user752746

6

我發現了一個非常簡單的解決辦法,我只是添加其他#在標籤的href:

<ul class="nav nav-tabs"> 
      <li class="home_tab active"><a href="##home"></a></li> 
      <li class="about_tab"><a href="##about"></a></li> 
      <li class="services_tab"><a href="##services"></a></li> 
      ... 
</ul> 

我不知道這是最好的解決方案,但肯定是快速和容易的。 在我的情況下適用於Chrome和IE 10,並滿足我的要求就足夠了。

+0

只爲我工作的解決方案。在Chrome,FF和IE上工作。 – jeesus

+0

這在頁面內很好地工作,但當我試圖鏈接到一個標籤通過添加'## tab-name'到URL我有問題..但它可能是與我的代碼 - 我想我可能會重寫默認選項卡的行爲某處 - 不知道 – ryan2johnson9

0

所選答案對我來說確實有用,但我也發現了另一種解決方案。只需刪除href-attribute,如下例所示...

<ul class="nav nav-tabs"> 
    <li class="home_tab active"><a></a></li> 
    <li class="about_tab"><a></a></li> 
    <li class="services_tab"><a></a></li> 
    ... 

因爲這個問題似乎是在<一個> - 標記有「優先」了jQuery的動作,只移除在href屬性就足夠了。 (<一個>標籤都有效,而不HREF屬性被允許的方式)。

在我的情況下,原來的問題是令人沮喪的發現,因爲它只有在生產環境中顯示,而不是在開發(相同的代碼)。無法找到html或鏈接資源中的頁面之間的任何區別...無論如何,這解決了它。

2

定義您的選項卡鏈接是這樣的:

<a data-target="#step1" data-toggle="tab">

和你的標籤本身是這樣的:

<div class="tab-pane" id="step1">

+1

我沒有得到downvotes。這個解決方案對我來說很好。 –

+0

這對Bootstrap來說是最好的答案。沒有控制檯錯誤 - 從我投票。 – DaniB

0

可以使用<a data-target="#home">代替<a href="#home">

在這裏看到一個例子:jsfiddle DEMO

0

我發現如果選項卡窗格高度不同,選項卡將跳轉。

我結束了標準化的高度,出於某種原因,即使在Flexbox的網格垂直定位有小鬼

function tabs_normalize() { 
    var tabs = $('.tab-content .tab-pane'), 
     heights = [], 
     tallest; 
    // check for tabs and create heights array 
    if (tabs.length) { 
     function set_heights() { 
      tabs.each(function() { 
       heights.push($(this).height()); 
      }); 
      tallest = Math.max.apply(null, heights); 
      tabs.each(function() { 
       $(this).css('min-height',tallest + 'px'); 
      }); 
     }; 
     set_heights();  
     // detect resize and rerun etc.. 
     $(window).on('resize orientationchange', function() { 
      tallest = 0, heights.length = 0; 
      tabs.each(function() { 
       $(this).css('min-height','0'); 
      }); 
      set_heights(); 
     }); 
    } 
} 
tabs_normalize();