Comments (10)
The main concern is that if users do not click on the preSlide and nextSlide quick enough after each page auto switch, they will get a quick flash to 'the next and next slide' (one is trigged from the autoplay and another is coming from the clicking. That's why I'm wondering maybe reset/ restart the autoplay timer might solve this problem without disabling the autoplay function. or is there any other easier way to solve this issue?
from react-hooks-image-slider.
Yup. You are right about that undesired effect happening. I'm going to check into it today.
from react-hooks-image-slider.
So I pushed up my initial solution for this to the autoplay branch.
I have two questions for you:
- What do you think about the solution below?
- I'm unsure if there are any cases where we will still get a weird transition. Right now, I found that if you click an arrow mid transition, there is a flash of white. Can you find any other edge cases/is the edge case you described above solved as well?
Here are the important pieces:
-
Handle whether
autoPlay
is active in a component higher in the hierarchy than Slider. I created anautoPlay
variable, as well as a function to turn it off in my example parent in index.js -
Set
autoPlayRef.current
to null ifautoPlay
is not active.
useEffect(() => {
transitionRef.current = smoothTransition
resizeRef.current = handleResize
autoPlayRef.current = props.autoPlay ? nextSlide : null
})
This comes in handy further on in the explanation.
- I separated setting the interval for autoPlay into it's own useEffect call from the transition and browser listeners to avoid conditionals, which made the code less easier to reason about.
// AutoPlay
useEffect(() => {
if (props.autoPlay) {
const play = () => {
autoPlayRef.current()
}
const interval = setInterval(play, props.autoPlay * 1000)
return () => {
clearInterval(interval)
}
}
}, [props.autoPlay])
- Last thing was to refactor
nextSlide
andprevSlide
to stop autoplay upon click (if autoplay is currently active).
const hasAutoPlayBeenStopped = e => {
if (e && autoPlayRef.current && e.target.className.includes('Arrow')) {
props.stopAutoPlay()
}
}
const nextSlide = e => {
hasAutoPlayBeenStopped(e)
setState({
...state,
translate: translate + getWidth(),
activeSlide: activeSlide === slides.length - 1 ? 0 : activeSlide + 1
})
}
const prevSlide = e => {
hasAutoPlayBeenStopped(e)
setState({
...state,
translate: 0,
activeSlide: activeSlide === 0 ? slides.length - 1 : activeSlide - 1
})
}
When we turn off autoPlay
, that's where our first useEffect
call that sets all of the ref values on each render comes in. When the component re-renders after stopping autoPlay
in the parent, that useEffect
call runs again and sets the autoPlayRef.current
value to null.
This allows us to have a solid conditional that only executes stopAutoPlay
only when it's active.
from react-hooks-image-slider.
@DZuz14
Thanks for the explanation! I've cloned the autoplay branch but the interval seems didn't restart after clicking the arrows. There was weirdly a few times the autoplay completely stopped and never resumed anymore. Is it the same on your computer?
I did encounter another issue while clicking the arrows quickly and continually. That was followed by a few blank slides. I guess it might because the smoothTransition couldn't catch up with the speed of quick clicking. For now I just temporally blocked nextSlide & prevSlide for like 500ms and that is sufficient for me. Just let you know and you might have more elegant solution since I am very new to the react world.
from react-hooks-image-slider.
You're right...lol. It doesn't work for me either. I could have sworn I tested it pretty good before pushing it up. I might have messed something up.
from react-hooks-image-slider.
omgggg... I think its the svg icon preventing clicks from working sometimes lol....i will fix this
from react-hooks-image-slider.
@MinHuang-TW Fixed it. This was a tricky one to track down. If you go to the Arrow component, I set a pointer-events property to none on the img element inside the css.
What was happening is, that if you clicked directly on the svg img element, nothing would happen, because I had set the onClick handler on the div, not the img element.
Setting that new property automatically bubbles up the click event to the div now, which catches it
from react-hooks-image-slider.
from react-hooks-image-slider.
Now the autoplay does stop! Is it possible to automatically resume it after the preset interval (3 seconds) instead of stopping forever?
from react-hooks-image-slider.
That's entirely up to you. You could create some type of interaction outside the form to start it back up, or inside the form.
from react-hooks-image-slider.
Related Issues (12)
- Requesting for AutoPlay feature HOT 3
- Is it possible to change slide transition to any animation on previous and next.
- Multiple sliders on the same page HOT 5
- Overlaping component
- Should be able to click on radio button to change to corresponding image. HOT 1
- Responsive translate HOT 3
- Images end up not appearing after one cycle on mobile (iOS and Chrome) HOT 3
- autoplay
- Reset the Interval HOT 2
- Problem with node-sass, old version has been removed. Gotta update to latest (at least 4.12.0);)
- Multiple images per slide HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-hooks-image-slider.