Skip to content

Commit

Permalink
support touch and drag event
Browse files Browse the repository at this point in the history
  • Loading branch information
Femi Oladeji committed Aug 20, 2020
1 parent 88d3fdc commit 5bd3a47
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 52 deletions.
18 changes: 13 additions & 5 deletions docs/views/codeStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ const Slideshow = () => {
return (
<div>
<Zoom {...zoomInProperties}>
{ images.map((each, index) => <img key={index} style={{width: "100%"}} src={each} />) }
{images.map((each, index) => (
<div key={index} style={{width: "100%"}}>
<img style={{ objectFit: "cover", width: "100%" }} src={each} />
</div>
))}
</Zoom>
</div>
)
Expand All @@ -198,7 +202,11 @@ const Slideshow = () => {
return (
<div>
<Zoom {...zoomOutProperties}>
{ images.map((each, index) => <img key={index} style={{width: "100%"}} src={each} />) }
{images.map((each, index) => (
<div key={index} style={{width: "100%"}}>
<img style={{ objectFit: "cover", width: "100%" }} src={each} />
</div>
))}
</Zoom>
</div>
)
Expand Down Expand Up @@ -417,7 +425,7 @@ const AutoplayExample = () => {
const style = {
textAlign: "center",
background: "teal",
padding: "200px",
padding: "200px 0",
fontSize: "30px"
}
Expand Down Expand Up @@ -461,7 +469,7 @@ const MethodsExample = () => {
const style = {
textAlign: "center",
background: "teal",
padding: "200px",
padding: "200px 0",
fontSize: "30px"
};
Expand Down Expand Up @@ -525,7 +533,7 @@ const CallbackExample = () => {
const style = {
textAlign: "center",
padding: "200px",
padding: "200px 0",
fontSize: "30px"
};
Expand Down
2 changes: 1 addition & 1 deletion docs/views/pages/Examples/Autoplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const AutoplayExample = () => {
const style = {
textAlign: "center",
background: "teal",
padding: "200px",
padding: "200px 0",
fontSize: "30px"
}

Expand Down
2 changes: 1 addition & 1 deletion docs/views/pages/Examples/Callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const CallbackExample = () => {

const style = {
textAlign: "center",
padding: "200px",
padding: "200px 0",
fontSize: "30px"
};

Expand Down
4 changes: 3 additions & 1 deletion docs/views/pages/Examples/Zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const ZoomExample = () => {
<div>
<Zoom {...zoomInProperties}>
{images.map((each, index) => (
<img key={index} style={{ width: "100%" }} src={each} />
<div key={index} style={{width: "100%"}}>
<img style={{ objectFit: "cover", width: "100%" }} src={each} />
</div>
))}
</Zoom>
</div>
Expand Down
4 changes: 3 additions & 1 deletion docs/views/pages/Examples/ZoomOut.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const ZoomExample = () => {
<div>
<Zoom {...zoomOutProperties}>
{images.map((each, index) => (
<img key={index} style={{ width: "100%" }} src={each} />
<div key={index} style={{width: "100%"}}>
<img style={{ objectFit: "cover", width: "100%" }} src={each} />
</div>
))}
</Zoom>
</div>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-slideshow-image",
"version": "3.0.2",
"version": "3.1.0",
"author": "Femi Oladeji",
"description": "An image slideshow with react",
"files": [
Expand Down
68 changes: 35 additions & 33 deletions src/css/styles.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
.react-slideshow-container {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}

.react-slideshow-container .nav {
Expand All @@ -16,23 +20,15 @@
cursor: pointer;
color: #fff;
border-radius: 50%;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
align-items: center;
justify-content: center;
}

.react-slideshow-container .default-nav span {
display: block;
width: 10px;
height: 10px;
border-color: #000;
border-style: solid;
box-sizing: border-box;
transform: rotate(45deg);
}

.react-slideshow-container .default-nav.disabled span {
border-color: #666;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}

.react-slideshow-container .default-nav:hover,
Expand All @@ -52,26 +48,21 @@
border-top: none;
}

.react-slideshow-container .default-nav:first-of-type span {
margin-left: 5px;
border-right: none;
border-top: none;
}

.react-slideshow-container .default-nav:last-of-type {
margin-left: -30px;
}

.react-slideshow-container .default-nav:last-of-type span {
margin-right: 5px;
border-left: none;
border-bottom: none;
}

.react-slideshow-container + div.indicators {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
flex-wrap: wrap;
justify-content: center;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
margin-top: 20px;
}

Expand Down Expand Up @@ -117,8 +108,11 @@
}

.react-slideshow-fade-wrapper .react-slideshow-fade-images-wrap {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}

.react-slideshow-fade-wrapper .react-slideshow-fade-images-wrap > div {
Expand All @@ -132,8 +126,11 @@
}

.react-slideshow-wrapper .images-wrap {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}

.react-slideshow-zoom-wrapper {
Expand All @@ -142,12 +139,17 @@
}

.react-slideshow-zoom-wrapper .zoom-wrapper {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
overflow: hidden;
}

.react-slideshow-zoom-wrapper .zoom-wrapper > div {
position: relative;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
}
1 change: 1 addition & 0 deletions src/fade.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class Fade extends Component {
<div
className="react-slideshow-container"
onMouseEnter={this.pauseSlides}
onMouseOver={this.pauseSlides}
onMouseLeave={this.startSlides}
ref={this.reactSlideshowWrapper}
>
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const showPreviousArrow = (
const className = `nav default-nav ${isDisabled ? 'disabled' : ''}`;
return (
<button className={className} {...props}>
<span />
<svg width="24" height="24" viewBox="0 0 24 24"><path d="M16.67 0l2.83 2.829-9.339 9.175 9.339 9.167-2.83 2.829-12.17-11.996z"/></svg>
</button>
);
};
Expand Down Expand Up @@ -59,7 +59,7 @@ const showNextArrow = (
const className = `nav default-nav ${isDisabled ? 'disabled' : ''}`;
return (
<button className={className} {...props}>
<span />
<svg width="24" height="24" viewBox="0 0 24 24"><path d="M5 3l3.057-3 11.943 12-11.943 12-3.057-3 9-9z"/></svg>
</button>
);
};
Expand Down
63 changes: 57 additions & 6 deletions src/slide.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Slideshow extends Component {
index:
props.defaultIndex && props.defaultIndex < props.children.length
? props.defaultIndex
: 0
: 0,
dragging: false
};
this.width = 0;
this.imageContainer = null;
Expand All @@ -30,6 +31,10 @@ class Slideshow extends Component {
this.reactSlideshowWrapper = createRef();
this.goToSlide = this.goToSlide.bind(this);
this.tweenGroup = new TWEEN.Group();
this.startSwipe = this.startSwipe.bind(this)
this.endSwipe = this.endSwipe.bind(this)
this.swipe = this.swipe.bind(this)
this.distanceSwiped = 0;
}

componentDidMount() {
Expand All @@ -55,6 +60,37 @@ class Slideshow extends Component {
this.removeResizeObserver();
}

startSwipe(e) {
this.startingClientX = e.touches ? e.touches[0].pageX : e.clientX
this.setState({ dragging: true })
clearTimeout(this.timeout)
}

endSwipe() {
this.setState({ dragging: false })
if ((Math.abs(this.distanceSwiped) / this.width) > 0.2) {
if (this.distanceSwiped < 0) {
this.goNext();
} else {
this.goBack();
}
} else {
if (Math.abs(this.distanceSwiped) > 0) {
this.slideImages(this.state.index, 300)
}
}
}

swipe(e) {
const clientX = e.touches ? e.touches[0].pageX : e.clientX;
if (this.state.dragging) {
let translateValue = this.width * (this.state.index + 1);
this.distanceSwiped = clientX - this.startingClientX;
translateValue -= this.distanceSwiped;
this.imageContainer.style.transform = `translate(-${translateValue}px)`;
}
}

removeResizeObserver() {
if (
this.resizeObserver &&
Expand Down Expand Up @@ -109,9 +145,13 @@ class Slideshow extends Component {
}

startSlides() {
const { pauseOnHover, autoplay } = this.props;
if (pauseOnHover && autoplay) {
this.timeout = setTimeout(() => this.goNext(), this.props.duration);
if (this.state.dragging) {
this.endSwipe()
} else {
const { pauseOnHover, autoplay } = this.props;
if (pauseOnHover && autoplay) {
this.timeout = setTimeout(() => this.goNext(), this.props.duration);
}
}
}

Expand Down Expand Up @@ -162,7 +202,15 @@ class Slideshow extends Component {
<div
className="react-slideshow-container"
onMouseEnter={this.pauseSlides}
onMouseOver={this.pauseSlides}
onMouseLeave={this.startSlides}
onMouseDown={this.startSwipe}
onMouseUp={this.endSwipe}
onMouseMove={this.swipe}
onTouchStart={this.startSwipe}
onTouchEnd={this.endSwipe}
onTouchCancel={this.endSwipe}
onTouchMove={this.swipe}
ref={this.reactSlideshowWrapper}
>
{arrows &&
Expand Down Expand Up @@ -212,7 +260,7 @@ class Slideshow extends Component {
);
}

slideImages(index) {
slideImages(index, animationDuration) {
let {
children,
transitionDuration,
Expand All @@ -221,10 +269,11 @@ class Slideshow extends Component {
duration,
onChange
} = this.props;
transitionDuration = animationDuration || transitionDuration;
const existingTweens = this.tweenGroup.getAll();
if (!existingTweens.length) {
clearTimeout(this.timeout);
const value = { margin: -this.width * (this.state.index + 1) };
const value = { margin: (-this.width * (this.state.index + 1)) + this.distanceSwiped };
const tween = new TWEEN.Tween(value, this.tweenGroup)
.to({ margin: -this.width * (index + 1) }, transitionDuration)
.onUpdate(value => {
Expand All @@ -244,6 +293,7 @@ class Slideshow extends Component {
animate();

tween.onComplete(() => {
this.distanceSwiped = 0;
const newIndex =
index < 0
? children.length - 1
Expand All @@ -262,6 +312,7 @@ class Slideshow extends Component {
},
() => {
if (autoplay && (infinite || this.state.index < children.length)) {
clearTimeout(this.timeout);
this.timeout = setTimeout(() => this.goNext(), duration);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class Zoom extends Component {
<div
className="react-slideshow-container"
onMouseEnter={this.pauseSlides}
onMouseOver={this.pauseSlides}
onMouseLeave={this.startSlides}
ref={this.reactSlideshowWrapper}
>
Expand Down

0 comments on commit 5bd3a47

Please sign in to comment.