我在创建图库页面时遇到问题。我使用Bootstrap作为基础,使用它。我查看了各种滑块和旋转木马,并选择了Flexslider2 - 它效果很好,看起来很棒,并且作为单个滑块工作得很好。
但是我现在想要使用4个Flexsliders,每个选项卡上有一个用于图库页面。好的,我可以有4页,但只有1页看起来会更好。
我对Javascript很新,但知道html(但不是DOM)。下面的示例仅为2个Flexsliders设置简单。
当我的页面触发时,初始的Flexslider很好但是当我切换标签时,第二个没有显示,没有错误,但是当我在屏幕上发出警报时,第二个Flexslider被初始化,然后显示。这指的是DOM问题,但我似乎无法找到解决方案。
HTML;
<div class="row-fluid">
<div class="span9">
<section id="tabs">
<div class="bs-docs-example">
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a href="#home">Home</a></li>
<li><a href="#profile">Profile</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="home">
<p>this is tab1 (0)</p>
<section class="slider">
<div id="slider1" class="flexslider">
<ul class="slides">
<li> <img src="assets/gallery/slide1.jpg" /> </li>
<li> <img src="assets/gallery/slide2.jpg" /> </li>
<li> <img src="assets/gallery/slide3.jpg" /> </li>
<li> <img src="assets/gallery/slide4.jpg" /> </li>
</ul>
</div>
<div id="carousel1" class="flexslider">
<ul class="slides">
<li> <img src="assets/gallery/slide1.jpg" /> </li>
<li> <img src="assets/gallery/slide2.jpg" /> </li>
<li> <img src="assets/gallery/slide3.jpg" /> </li>
<li> <img src="assets/gallery/slide4.jpg" /> </li>
</ul>
</div>
</section>
</div>
<div class="tab-pane" id="profile">
<p>this is tab2 (1)</p>
<section class="slider">
<div id="slider2" class="flexslider">
<ul class="slides">
<li> <img src="assets/gallery/slide2.jpg" /> </li>
<li> <img src="assets/gallery/slide3.jpg" /> </li>
<li> <img src="assets/gallery/slide4.jpg" /> </li>
<li> <img src="assets/gallery/slide1.jpg" /> </li>
</ul>
</div>
<div id="carousel2" class="flexslider">
<ul class="slides">
<li> <img src="assets/gallery/slide2.jpg" /> </li>
<li> <img src="assets/gallery/slide3.jpg" /> </li>
<li> <img src="assets/gallery/slide4.jpg" /> </li>
<li> <img src="assets/gallery/slide1.jpg" /> </li>
</ul>
</div>
</section>
</div>
</div>
</div>
<hr class="bs-docs-separator">
</section>
</div>
</div>
javascript是;
<script type="text/javascript">
$('#myTab a').click(function (e) {
e.preventDefault();
alert("just about to call initflex");
initflexsliders();
$(this).tab('show');
})
</script>
<script type="text/javascript">
$(window).load(function(){
initflexsliders();
});
</script>
<script type="text/javascript">
function initflexsliders(){
$('#carousel1').flexslider({
animation: "slide",
controlNav: false,
animationLoop: false,
slideshow: false,
itemWidth: 100,
itemMargin: 5,
asNavFor: '#slider1'
});
$('#slider1').flexslider({
animation: "slide",
controlNav: false,
animationLoop: false,
slideshow: false,
sync: "#carousel1",
});
$('#carousel2').flexslider({
animation: "slide",
controlNav: false,
animationLoop: false,
slideshow: false,
itemWidth: 100,
itemMargin: 5,
asNavFor: '#slider2'
});
$('#slider2').flexslider({
animation: "slide",
controlNav: false,
animationLoop: false,
slideshow: false,
sync: "#carousel2",
});
}
</script>
有没有人有任何想法?我希望它很简单,但是它让我把头发拉出来了?
答案 0 :(得分:1)
我也有解决方案,这适用于flexslider 2.5
当tab已经打开然后初始化flexslider时我们捕获事件:
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
var selectedTab = $(e.target).attr("href") // activated tab
if( $(selectedTab + ' .flexslider' ).length ){
$(window).trigger('resize'); //this should fix for you but you can also try this
var carousel_sliders = $(selectedTab + ' .flexslider');
for (var i = 0; i < carousel_sliders.length; i++) {
var element = carousel_sliders.eq(i);
element.flexslider({
animation: "slide",
controlsContainer: $(selectedTab + " .custom-navigation-m .custom-controls-container"),
customDirectionNav: $(selectedTab + " .custom-navigation-m a")
});
}
}
});
答案 1 :(得分:0)
玩了一下,似乎你不应该在页面加载时初始化每个滑块,而是分别在每个标签上调用它。
小提琴:http://jsfiddle.net/qSqpD/709/
$('#myTab a:first-child').click(function (e) {
e.preventDefault();
$(this).tab('show');
$('#carousel1').flexslider({
animation: "slide",
controlNav: false,
animationLoop: false,
slideshow: false,
itemWidth: 400,
itemMargin: 5,
asNavFor: '#slider1'
});
});
希望有所帮助
答案 2 :(得分:0)
我有一个解决方案。
请转到
https://github.com/liangrongze/flexslider-compatible-bootstrap-tab
并下载flexslider文件以替换旧的js文件。
它适用于我的项目。
答案 3 :(得分:0)
一个简单的解决方案是:
在HTML中:
<div class="tab-pane" id="videos" align="center">
在JS中:
$('#video').click( function (e) {
e.preventDefault();
$('#slider2').flexslider({
animation: "slide",
animationLoop: false,
slideshow: false,
itemMargin: 5,
video: true
});