Masonry js正在搞乱Bootstrap选项卡功能

时间:2014-11-04 19:37:31

标签: javascript twitter-bootstrap tabs jquery-masonry masonry

我正在使用Desandro的Masonry的Bootstrap标签功能,问题是当我加载页面时,它会在第一个标签上开始半秒钟,然后跳转到带有Masonry的标签。 我猜我可以通过一些脚本来解决这个问题,但我对js / jquery来说太新了,无法解决这个问题。

我尝试更改脚本代码的顺序,因为它看起来像页面加载,然后Masonry脚本发生并强制其选项卡处于活动状态。但无论代码的顺序如何,它仍然会发生。

具体而言,我需要的是有人指出我忽略的错误,或者给我一些关于如何在加载时使页面停留在第一个标签上的指示。

我在这里包含了我的html和js控件,但我也在http://jsfiddle.net/jccarter1990/o15e98ts/3/

完成了整个代码。

提前致谢



<ul class="nav nav-pills" role="tablist" id="myTab">
		<li role="presentation" class="active"><a href="#page-one" role="tab" data-toggle="tab">Cover Letter</a></li>
		<li role="presentation"><a href="#page-two" role="tab" data-toggle="tab">Features</a></li>
		<li role="presentation"><a href="#page-three" role="tab" data-toggle="tab">Gallery</a></li>
	</ul>

<div class="tab-content">

<div role="tabpanel" class="tab-pane active"  id="page-one">Cover Letter</div>
  
<div role="tabpanel" class="tab-pane"  id="page-two">Features</div>
  
<div role="tabpanel" class="tab-pane"  id="page-three">
  <div class="masonry js-masonry" >
  <div class="item"><img class="thumbnail" src="img/paigeJohnLasVegas.jpg"></div>
  <div class="item"><img class="thumbnail" src="img/engagementsCloseUp.jpg"></div>
  <div class="item"><img class="thumbnail" src="img/jimmyJohns.jpg"></div>
  <div class="item"><img class="thumbnail" src="img/paigeNinjaTurtle.jpg"></div>
  <div class="item"><img class="thumbnail" src="img/sprayPaintSid.jpg"></div>
    </div>  
  </div>
&#13;
&#13;
&#13;

&#13;
&#13;
<script>
	var container = document.querySelector('.masonry');
		var msnry;
		// initialize Masonry after all images have loaded
		imagesLoaded( container, function() {
		msnry = new Masonry( container );
		});
	var container = document.querySelector('.masonry');
		var msnry = new Masonry( container, {
		  // options
		  columnWidth: 200,
		  itemSelector: '.masonry.item'
		});
	
	$('#myTab a').click(function (e) {
		e.preventDefault()
		$(this).tab('show')
})

	$(function () {
		$('#myTab a[href="#page-one"]').tab('show')
		$('#myTab a[href="#page-two"]').tab('show')
		$('#myTab a[href="#page-three"]').tab('show')
	})

	
</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

更新的答案:将您的第一个标签和面板设置为活动标签:

Fiddle

 <div class="container navigation"> <span class="nt-name">John Carter</span>

 <br>   <span class="nt-title">Professional Portfolio</span>

 <ul class="nav nav-pills" role="tablist" id="myTab">
    <li role="presentation" class="active"><a href="#page-one" role="tab" data-toggle="tab">Cover Letter</a>

    </li>
    <li role="presentation"><a href="#page-two" role="tab" data-toggle="tab">Features</a>

    </li>
    <li role="presentation"><a href="#page-three" class="active" role="tab" data-toggle="tab">Gallery</a>

    </li>
</ul>
 </div>
 <div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="page-one">
    <div class="container">
            <h1>Cover Letter</h1>

    </div>
</div>
<div role="tabpanel" class="tab-pane" id="page-two">
    <div class="container">
         <h1>Features</h1>

    </div>
 </div>
  <div role="tabpanel" class="tab-pane" id="page-three">
    <div class="container">
            <h1>Gallery</h1>

    </div>
    <div class="masonry js-masonry">
        <div class="item">
            <img class="thumbnail" src="img/paigeJohnLasVegas.jpg">
        </div>
        <div class="item">
            <img class="thumbnail" src="img/engagementsCloseUp.jpg">
        </div>
        <div class="item">
            <img class="thumbnail" src="img/jimmyJohns.jpg">
        </div>
        <div class="item">
            <img class="thumbnail" src="img/paigeNinjaTurtle.jpg">
        </div>
        <div class="item">
            <img class="thumbnail" src="img/sprayPaintSid.jpg">
        </div>
    </div>
</div>
</div>

将您的脚本更改为:

//bootstrap tab function
$('#myTab a').click(function (e) {
e.preventDefault()
$(this).tab('show')
})

遗漏:

顺便说一句,如果选中包含Bootstrap的框,则不必在jsfiddle中包含bootstrap.js,它包含js。如果您要包含其他js,例如masonry.js,请使用外部资源添加它们,而不是将它们粘贴到脚本面板中。这将使fior人更容易看到相关代码。