mirror of
https://github.com/twbs/bootstrap.git
synced 2025-01-29 21:52:22 +01:00
Use Page Visibility API in Carousel; fixes #17706
Avoids cycling carousels when the page isn't visible. Closes #17710 Refs #15566
This commit is contained in:
parent
9b12e5f495
commit
817d997b75
@ -119,6 +119,13 @@ const Carousel = (($) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nextWhenVisible() {
|
||||||
|
// Don't call next when the page isn't visible
|
||||||
|
if (!document.hidden) {
|
||||||
|
this.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
prev() {
|
prev() {
|
||||||
if (!this._isSliding) {
|
if (!this._isSliding) {
|
||||||
this._slide(Direction.PREVIOUS)
|
this._slide(Direction.PREVIOUS)
|
||||||
@ -152,7 +159,7 @@ const Carousel = (($) => {
|
|||||||
|
|
||||||
if (this._config.interval && !this._isPaused) {
|
if (this._config.interval && !this._isPaused) {
|
||||||
this._interval = setInterval(
|
this._interval = setInterval(
|
||||||
$.proxy(this.next, this), this._config.interval
|
$.proxy(document.visibilityState ? this.nextWhenVisible : this.next, this), this._config.interval
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
<div class="page-header">
|
<div class="page-header">
|
||||||
<h1>Carousel <small>Bootstrap Visual Test</small></h1>
|
<h1>Carousel <small>Bootstrap Visual Test</small></h1>
|
||||||
</div>
|
</div>
|
||||||
|
<p>Also, the carousel shouldn't slide when its window/tab is hidden. Check the console log.</p>
|
||||||
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
|
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
|
||||||
<ol class="carousel-indicators">
|
<ol class="carousel-indicators">
|
||||||
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
|
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
|
||||||
@ -53,6 +53,14 @@
|
|||||||
<script src="../vendor/jquery.min.js"></script>
|
<script src="../vendor/jquery.min.js"></script>
|
||||||
<script src="../../dist/util.js"></script>
|
<script src="../../dist/util.js"></script>
|
||||||
<script src="../../dist/carousel.js"></script>
|
<script src="../../dist/carousel.js"></script>
|
||||||
|
<script>
|
||||||
|
$(function () {
|
||||||
|
// Test to show that the carousel doesn't slide when the current tab isn't visible
|
||||||
|
$('#carousel-example-generic').on('slid.bs.carousel', function (event) {
|
||||||
|
console.log('slid at ', event.timeStamp);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user