Merge pull request #23 from vadimkorr/feature/#22_Pause-autoplay-on-mouseenter
Feature/#22 Pause autoplay on mouseenter
This commit is contained in:
@@ -42,6 +42,7 @@ Import component and styles in App component
|
||||
| `autoplay` | `boolean` | `false` | Enables auto play of pages |
|
||||
| `autoplayDuration` | `number` | `3000` | Auto play change interval (ms) |
|
||||
| `autoplayDirection` | `string` | `'next'` | Auto play change direction (`next` or `prev`) |
|
||||
| `pauseOnFocus` | `boolean` | `false` | Pause autoplay on focus |
|
||||
| `dots` | `boolean` | `true` | Current page indicator dots |
|
||||
| `timingFunction` | `string` | `'ease-in-out'` | CSS animation timing function |
|
||||
|
||||
|
||||
21
src/actions/focusable/event.js
Normal file
21
src/actions/focusable/event.js
Normal file
@@ -0,0 +1,21 @@
|
||||
// focusin event
|
||||
export function addFocusinEventListener(source, cb) {
|
||||
source.addEventListener('mouseenter', cb)
|
||||
source.addEventListener('touchstart', cb)
|
||||
}
|
||||
export function removeFocusinEventListener(source, cb) {
|
||||
source.removeEventListener('mouseenter', cb)
|
||||
source.removeEventListener('touchstart', cb)
|
||||
}
|
||||
|
||||
// focusout event
|
||||
export function addFocusoutEventListener(source, cb) {
|
||||
source.addEventListener('mouseleave', cb)
|
||||
source.addEventListener('touchend', cb)
|
||||
source.addEventListener('touchcancel', cb)
|
||||
}
|
||||
export function removeFocusoutEventListener(source, cb) {
|
||||
source.removeEventListener('mouseleave', cb)
|
||||
source.removeEventListener('touchend', cb)
|
||||
source.removeEventListener('touchcancel', cb)
|
||||
}
|
||||
29
src/actions/focusable/focusable.js
Normal file
29
src/actions/focusable/focusable.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { createDispatcher } from '../../utils/event'
|
||||
import {
|
||||
addFocusinEventListener,
|
||||
removeFocusinEventListener,
|
||||
addFocusoutEventListener,
|
||||
removeFocusoutEventListener,
|
||||
} from './event'
|
||||
|
||||
export function focusable(node) {
|
||||
const dispatch = createDispatcher(node)
|
||||
|
||||
function handleFocusin() {
|
||||
dispatch('focused', { value: true })
|
||||
}
|
||||
|
||||
function handleFocusout() {
|
||||
dispatch('focused', { value: false })
|
||||
}
|
||||
|
||||
addFocusinEventListener(node, handleFocusin)
|
||||
addFocusoutEventListener(node, handleFocusout)
|
||||
|
||||
return {
|
||||
destroy() {
|
||||
removeFocusinEventListener(node, handleFocusin)
|
||||
removeFocusoutEventListener(node, handleFocusout)
|
||||
},
|
||||
}
|
||||
}
|
||||
1
src/actions/focusable/index.js
Normal file
1
src/actions/focusable/index.js
Normal file
@@ -0,0 +1 @@
|
||||
export * from './focusable'
|
||||
@@ -5,6 +5,7 @@
|
||||
import Arrow from '../Arrow/Arrow.svelte'
|
||||
import { NEXT, PREV } from '../../direction'
|
||||
import { swipeable } from '../../actions/swipeable'
|
||||
import { focusable } from '../../actions/focusable'
|
||||
import {
|
||||
addResizeEventListener,
|
||||
removeResizeEventListener
|
||||
@@ -60,6 +61,11 @@
|
||||
*/
|
||||
export let autoplayDirection = NEXT
|
||||
|
||||
/**
|
||||
* Pause autoplay on focus
|
||||
*/
|
||||
export let pauseOnFocus = false
|
||||
|
||||
/**
|
||||
* Current page indicator dots
|
||||
*/
|
||||
@@ -76,6 +82,18 @@
|
||||
let offset = 0
|
||||
let pageWindowElement
|
||||
let pagesElement
|
||||
let focused = false
|
||||
|
||||
let autoplayInterval = null
|
||||
$: {
|
||||
if (pauseOnFocus) {
|
||||
if (focused) {
|
||||
clearAutoplay()
|
||||
} else {
|
||||
applyAutoplay()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// used for lazy loading images, preloaded only current, adjacent and cloanable images
|
||||
$: loaded = getAdjacentIndexes(originalCurrentPageIndex, originalPagesCount, infinite)
|
||||
@@ -96,15 +114,16 @@
|
||||
}
|
||||
|
||||
function applyAutoplay() {
|
||||
let interval
|
||||
if (autoplay) {
|
||||
interval = setInterval(() => {
|
||||
if (autoplay && !autoplayInterval) {
|
||||
autoplayInterval = setInterval(() => {
|
||||
directionFnDescription[autoplayDirection]()
|
||||
}, autoplayDuration)
|
||||
}
|
||||
return () => {
|
||||
interval && clearInterval(interval)
|
||||
}
|
||||
}
|
||||
|
||||
function clearAutoplay() {
|
||||
clearInterval(autoplayInterval)
|
||||
autoplayInterval = null
|
||||
}
|
||||
|
||||
function addClones() {
|
||||
@@ -128,12 +147,13 @@
|
||||
infinite && addClones()
|
||||
applyPageSizes()
|
||||
}
|
||||
cleanupFns.push(applyAutoplay())
|
||||
applyAutoplay()
|
||||
addResizeEventListener(applyPageSizes)
|
||||
})()
|
||||
})
|
||||
|
||||
onDestroy(() => {
|
||||
clearAutoplay()
|
||||
removeResizeEventListener(applyPageSizes)
|
||||
cleanupFns.filter(fn => fn && typeof fn === 'function').forEach(fn => fn())
|
||||
})
|
||||
@@ -198,6 +218,9 @@
|
||||
function handleSwipeEnd() {
|
||||
showPage(currentPageIndex, { offsetDelay: 0, animated: true })
|
||||
}
|
||||
function handleFocused(event) {
|
||||
focused = event.detail.value
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="sc-carousel__carousel-container">
|
||||
@@ -216,6 +239,8 @@
|
||||
<div
|
||||
class="sc-carousel__pages-window"
|
||||
bind:this={pageWindowElement}
|
||||
use:focusable
|
||||
on:focused={handleFocused}
|
||||
>
|
||||
<div
|
||||
class="sc-carousel__pages-container"
|
||||
|
||||
@@ -41,6 +41,11 @@
|
||||
* Auto play change direction ('next', 'prev')
|
||||
*/
|
||||
export let autoplayDirection = NEXT
|
||||
|
||||
/**
|
||||
* Pause autoplay on focus
|
||||
*/
|
||||
export let pauseOnFocus = false
|
||||
|
||||
/**
|
||||
* Current page indicator dots
|
||||
@@ -77,6 +82,7 @@
|
||||
{autoplay}
|
||||
{autoplayDuration}
|
||||
{autoplayDirection}
|
||||
{pauseOnFocus}
|
||||
{dots}
|
||||
on:pageChange={
|
||||
event => console.log(`Current page index: ${event.detail}`)
|
||||
@@ -101,6 +107,7 @@
|
||||
{autoplay}
|
||||
{autoplayDuration}
|
||||
{autoplayDirection}
|
||||
{pauseOnFocus}
|
||||
{dots}
|
||||
>
|
||||
{#each colors2 as { color, text } (color)}
|
||||
|
||||
@@ -42,6 +42,11 @@
|
||||
*/
|
||||
export let autoplayDirection = NEXT
|
||||
|
||||
/**
|
||||
* Pause autoplay on focus
|
||||
*/
|
||||
export let pauseOnFocus = false
|
||||
|
||||
/**
|
||||
* Current page indicator dots
|
||||
*/
|
||||
@@ -71,6 +76,7 @@
|
||||
{autoplay}
|
||||
{autoplayDuration}
|
||||
{autoplayDirection}
|
||||
{pauseOnFocus}
|
||||
{dots}
|
||||
let:showPrevPage
|
||||
let:showNextPage
|
||||
|
||||
@@ -42,6 +42,11 @@
|
||||
*/
|
||||
export let autoplayDirection = NEXT
|
||||
|
||||
/**
|
||||
* Pause autoplay on focus
|
||||
*/
|
||||
export let pauseOnFocus = false
|
||||
|
||||
/**
|
||||
* Current page indicator dots
|
||||
*/
|
||||
@@ -75,6 +80,7 @@
|
||||
{autoplay}
|
||||
{autoplayDuration}
|
||||
{autoplayDirection}
|
||||
{pauseOnFocus}
|
||||
{dots}
|
||||
let:currentPageIndex
|
||||
let:pagesCount
|
||||
|
||||
@@ -214,6 +214,7 @@ Import component and styles in App component
|
||||
| `autoplayDuration` | `number` | `3000` | Auto play change interval (ms) |
|
||||
| `autoplayDirection` | `string` | `'next'` | Auto play change direction (`next` or `prev`) |
|
||||
| `dots` | `boolean` | `true` | Current page indicator dots |
|
||||
| `pauseOnFocus` | `boolean` | `false` | Pause autoplay on focus |
|
||||
| `timingFunction` | `string` | `'ease-in-out'` | CSS animation timing function |
|
||||
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user