mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
sliding with mouse
This commit is contained in:
parent
31093d42c3
commit
4236ef9824
1 changed files with 36 additions and 14 deletions
|
@ -8,6 +8,7 @@ import React, {
|
||||||
useRef,
|
useRef,
|
||||||
useCallback,
|
useCallback,
|
||||||
MouseEventHandler,
|
MouseEventHandler,
|
||||||
|
TouchEventHandler,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
import { ImageSwitcher } from '../ImageSwitcher';
|
import { ImageSwitcher } from '../ImageSwitcher';
|
||||||
import * as styles from './styles.scss';
|
import * as styles from './styles.scss';
|
||||||
|
@ -23,6 +24,8 @@ interface IProps {
|
||||||
updateLayout: () => void;
|
updateLayout: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getX = event => (event.touches ? event.touches[0].clientX : event.clientX);
|
||||||
|
|
||||||
const NodeImageSlideBlock: FC<IProps> = ({ node, is_loading, updateLayout }) => {
|
const NodeImageSlideBlock: FC<IProps> = ({ node, is_loading, updateLayout }) => {
|
||||||
const [is_animated, setIsAnimated] = useState(false);
|
const [is_animated, setIsAnimated] = useState(false);
|
||||||
const [current, setCurrent] = useState(0);
|
const [current, setCurrent] = useState(0);
|
||||||
|
@ -54,7 +57,6 @@ const NodeImageSlideBlock: FC<IProps> = ({ node, is_loading, updateLayout }) =>
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
setHeights(values);
|
setHeights(values);
|
||||||
console.log({ values });
|
|
||||||
}, [refs]);
|
}, [refs]);
|
||||||
|
|
||||||
const setRef = useCallback(
|
const setRef = useCallback(
|
||||||
|
@ -104,54 +106,74 @@ const NodeImageSlideBlock: FC<IProps> = ({ node, is_loading, updateLayout }) =>
|
||||||
|
|
||||||
const onDrag = useCallback(
|
const onDrag = useCallback(
|
||||||
event => {
|
event => {
|
||||||
if (!is_dragging || !slide.current || !wrap.current) return;
|
if (
|
||||||
|
!is_dragging ||
|
||||||
|
!slide.current ||
|
||||||
|
!wrap.current ||
|
||||||
|
(event.touches && event.clientY > event.clientX)
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
|
||||||
const { width: slide_width } = slide.current.getBoundingClientRect();
|
const { width: slide_width } = slide.current.getBoundingClientRect();
|
||||||
const { width: wrap_width } = wrap.current.getBoundingClientRect();
|
const { width: wrap_width } = wrap.current.getBoundingClientRect();
|
||||||
|
|
||||||
console.log(wrap_width - slide_width, initial_offset + event.clientX - initial_x);
|
|
||||||
|
|
||||||
setOffset(
|
setOffset(
|
||||||
Math.min(Math.max(initial_offset + event.clientX - initial_x, wrap_width - slide_width), 0)
|
Math.min(Math.max(initial_offset + getX(event) - initial_x, wrap_width - slide_width), 0)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
[is_dragging, initial_x, setOffset, initial_offset]
|
[is_dragging, initial_x, setOffset, initial_offset]
|
||||||
);
|
);
|
||||||
|
|
||||||
const stopDragging = useCallback(() => {
|
const stopDragging = useCallback(() => {
|
||||||
window.removeEventListener('mouseup', stopDragging);
|
if (!is_dragging) return;
|
||||||
setIsDragging(false);
|
|
||||||
}, [setIsDragging, onDrag]);
|
|
||||||
|
|
||||||
const startDragging: MouseEventHandler<HTMLDivElement> = useCallback(
|
const { width: wrap_width } = wrap.current.getBoundingClientRect();
|
||||||
|
|
||||||
|
setIsDragging(false);
|
||||||
|
setOffset(Math.round(offset / wrap_width) * wrap_width);
|
||||||
|
}, [setIsDragging, offset, setOffset, is_dragging, wrap]);
|
||||||
|
|
||||||
|
const startDragging = useCallback(
|
||||||
event => {
|
event => {
|
||||||
setIsDragging(true);
|
setIsDragging(true);
|
||||||
setInitialX(event.clientX);
|
setInitialX(getX(event));
|
||||||
setInitialOffset(offset);
|
setInitialOffset(offset);
|
||||||
|
|
||||||
window.addEventListener('mouseup', stopDragging);
|
|
||||||
},
|
},
|
||||||
[setIsDragging, stopDragging, setInitialX, offset, setInitialOffset, onDrag]
|
[setIsDragging, setInitialX, offset, setInitialOffset]
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
window.addEventListener('resize', updateSizes);
|
||||||
|
|
||||||
window.addEventListener('mousemove', onDrag);
|
window.addEventListener('mousemove', onDrag);
|
||||||
|
window.addEventListener('touchmove', onDrag);
|
||||||
|
|
||||||
|
window.addEventListener('mouseup', stopDragging);
|
||||||
|
window.addEventListener('touchend', stopDragging);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
window.removeEventListener('resize', updateSizes);
|
||||||
|
|
||||||
window.removeEventListener('mousemove', onDrag);
|
window.removeEventListener('mousemove', onDrag);
|
||||||
|
window.removeEventListener('touchmove', onDrag);
|
||||||
|
|
||||||
|
window.removeEventListener('mouseup', stopDragging);
|
||||||
|
window.removeEventListener('touchend', stopDragging);
|
||||||
};
|
};
|
||||||
}, [onDrag]);
|
}, [onDrag, stopDragging]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classNames(styles.wrap, { is_loading, is_animated })} ref={wrap}>
|
<div className={classNames(styles.wrap, { is_loading, is_animated })} ref={wrap}>
|
||||||
<div
|
<div
|
||||||
className={styles.image_container}
|
className={styles.image_container}
|
||||||
style={{
|
style={{
|
||||||
|
transition: is_dragging ? 'none' : 'transform 500ms',
|
||||||
height,
|
height,
|
||||||
transform: `translate(${offset}px, 0)`,
|
transform: `translate(${offset}px, 0)`,
|
||||||
width: `${images.length * 100}%`,
|
width: `${images.length * 100}%`,
|
||||||
}}
|
}}
|
||||||
onMouseDown={startDragging}
|
onMouseDown={startDragging}
|
||||||
|
onTouchStart={startDragging}
|
||||||
ref={slide}
|
ref={slide}
|
||||||
>
|
>
|
||||||
{(is_loading || !loaded[0] || !images.length) && <div className={styles.placeholder} />}
|
{(is_loading || !loaded[0] || !images.length) && <div className={styles.placeholder} />}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue