我在一个页面中使用了两个进度条carousel。第一个旋转木马会破坏第二个进度条。
这是我的第一个旋转木马:
<div id="owl-demo" class="owl-carousel owl-theme">
<div class="item"><img src="images/fullimage1.jpg" alt="The Last of us"></div>
<div class="item"><img src="images/fullimage2.jpg" alt="GTA V"></div>
<div class="item"><img src="images/fullimage3.jpg" alt="Mirror Edge"></div>
</div>
第二个:
<div id="owl-demo2" class="owl-carousel owl-theme">
<div class="item"><img src="images/fullimage1.jpg" alt="The Last of us"></div>
<div class="item"><img src="images/fullimage2.jpg" alt="GTA V"></div>
<div class="item"><img src="images/fullimage3.jpg" alt="Mirror Edge"></div>
</div>
我已将id更改为owl-demo2。 这是我的css文件:
#owl-demo .item img{display: block; width: 100%; height: auto;}
#bar{ width: 0%; max-width: 100%; height: 4px; background: #990000;}
#progressBar{ width: 100%; background: #8A6D3B;}
#owl-demo2 .item img{display: block; width: 100%; height: auto;}
这是js文件
var time = 7; // time in seconds
var $progressBar,
$bar,
$elem,
isPause,
tick,
percentTime;
//Init the carousel
$("#owl-demo").owlCarousel({
slideSpeed : 500,
paginationSpeed : 500,
singleItem : true,
afterInit : progressBar,
afterMove : moved,
startDragging : pauseOnDragging
});
$("#owl-demo2").owlCarousel({
slideSpeed : 500,
paginationSpeed : 500,
singleItem : true,
afterInit : progressBar,
afterMove : moved,
startDragging : pauseOnDragging
});
//Init progressBar where elem is $("#owl-demo")
function progressBar(elem){
$elem = elem;
//build progress bar elements
buildProgressBar();
//start counting
start();
}
//create div#progressBar and div#bar then prepend to $("#owl-demo")
function buildProgressBar(){
$progressBar = $("<div>",{
id:"progressBar"
});
$bar = $("<div>",{
id:"bar"
});
$progressBar.append($bar).prependTo($elem);
}
function start() {
//reset timer
percentTime = 0;
isPause = false;
//run interval every 0.01 second
tick = setInterval(interval, 10);
};
function interval() {
if(isPause === false){
percentTime += 1 / time;
$bar.css({
width: percentTime+"%"
});
//if percentTime is equal or greater than 100
if(percentTime >= 100){
//slide to next item
$elem.trigger('owl.next')
}
}
}
//pause while dragging
function pauseOnDragging(){
isPause = true;
}
//moved callback
function moved(){
//clear interval
clearTimeout(tick);
//start again
start();
}
我的第一个问题是,我哪里弄错了? 第二个问题是,是否可以将进度条从上到下? 提前谢谢
答案 0 :(得分:2)
<强> Working fiddle 强>
之所以发生这种情况,是因为id
应该是唯一的,并且您为两个进度条提供相同的ID。
所以按类更改ids
,例如:
function buildProgressBar(){
$progressBar = $("<div>",{
class:"progressBar"
});
$bar = $("<div>",{
class:"bar"
});
$progressBar.append($bar).prependTo($elem);
}
然后在bar
函数中使用类interval()
来更新此类的所有元素:
$('.bar').css({
width: percentTime+"%"
});
还将css规则从id更改为类:
.bar{ width: 0%; max-width: 100%; height: 4px; background: #990000;}
.progressBar{ width: 100%; background: #8A6D3B;}
最后替换:{/ p>中的$elem
$elem.trigger('owl.next');
按$(".owl-carousel")
,owl.next
将适用于两个轮播:
$(".owl-carousel").trigger('owl.next');
希望这有帮助。
$(document).ready(function() {
var time = 7; // time in seconds
var $progressBar,
$bar,
$elem,
isPause,
tick,
percentTime;
//Init the carousel
$("#owl-demo,#owl-demo2").owlCarousel({
slideSpeed : 500,
paginationSpeed : 500,
singleItem : true,
afterInit : progressBar,
afterMove : moved,
startDragging : pauseOnDragging
});
//Init progressBar where elem is $("#owl-demo")
function progressBar(elem){
$elem = elem;
//build progress bar elements
buildProgressBar();
//start counting
start();
}
//create div#progressBar and div#bar then prepend to $("#owl-demo")
function buildProgressBar(){
$progressBar = $("<div>",{
class:"progressBar"
});
$bar = $("<div>",{
class:"bar"
});
$progressBar.append($bar).prependTo($elem);
}
function start() {
//reset timer
percentTime = 0;
isPause = false;
//run interval every 0.01 second
tick = setInterval(interval, 10);
};
function interval() {
if(isPause === false){
percentTime += 1 / time;
$('.bar').css({
width: percentTime+"%"
});
//if percentTime is equal or greater than 100
if(percentTime >= 100){
//slide to next item
$elem.trigger('owl.next')
}
}
}
//pause while dragging
function pauseOnDragging(){
isPause = true;
}
//moved callback
function moved(){
//clear interval
clearTimeout(tick);
//start again
start();
}
});
#owl-demo .item img{display: block; width: 100%; height: auto;}
.bar{ width: 0%; max-width: 100%; height: 4px; background: #990000;}
.progressBar{ width: 100%; background: #8A6D3B;}
#owl-demo2 .item img{display: block; width: 100%; height: auto;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://owlgraphic.com/owlcarousel/owl-carousel/owl.carousel.min.js"></script>
<link href="http://owlgraphic.com/owlcarousel/owl-carousel/owl.theme.css" rel="stylesheet"/>
<link href="http://owlgraphic.com/owlcarousel/owl-carousel/owl.carousel.css" rel="stylesheet"/>
<div id="owl-demo" class="owl-carousel">
<div> ---- Example1 (Your Content 1)</div>
<div> ---- Example1 (Your Content 2)</div>
<div> ---- Example1 (Your Content 3)</div>
</div>
<div id="owl-demo2" class="owl-carousel">
<div> ---- Example2 (Your Content 1)</div>
<div> ---- Example2 (Your Content 2)</div>
<div> ---- Example2 (Your Content 3)</div>
</div>