Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
QiuYeDx committed Sep 15, 2023
1 parent db4704a commit cac2f2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/hooks/useScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
// export default useHorizontalScroll;

import { useState, useEffect, useCallback } from 'react';
import {throttle} from "@/utils/throttle";

/**
* Hook to get the horizontal scroll details of an element.
Expand Down Expand Up @@ -223,10 +224,10 @@ function useHorizontalScroll(ref, onlyOnMount = false) {
}
};

const handleResize = () => {
const handleResize = throttle(() => {
updateDimensions();
updateScrollState();
};
}, 150);

ref.current.addEventListener('scroll', handleScroll);
window.addEventListener('resize', handleResize);
Expand Down
22 changes: 18 additions & 4 deletions src/modules/XList/DiscreteProgress.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//
// export default DiscreteProgress;

import React, {useEffect, useRef, useState} from 'react';
import React, {useEffect, useLayoutEffect, useRef, useState} from 'react';
import tw, { styled } from 'twin.macro'
import gsap from "gsap";
import {randomNum} from "@/utils/utils";
Expand All @@ -56,7 +56,10 @@ import {randomNum} from "@/utils/utils";
*/
const DiscreteProgress = ({ clientWidth, scrollWidth, scrollLeft, numberOfSteps = 5, distancePerStep = 100, gsapClass = null, scrollRef }) => {
const activeStep = Math.min(numberOfSteps, Math.floor(scrollLeft / distancePerStep) + 1);
console.info(numberOfSteps, Math.floor(scrollLeft / distancePerStep) + 1);
const random_num = randomNum(0, 999);
const [DELAY, setDELAY] = useState(1.8);
const notFirst = useRef(false);
const [defaultGsapClass] = useState(`gsap_discrete_progress_${random_num}`);

const gsap_ref = useRef(null);
Expand All @@ -78,7 +81,10 @@ const DiscreteProgress = ({ clientWidth, scrollWidth, scrollLeft, numberOfSteps
duration: 1.2,
ease: 'power3.out',
stagger: 0.12,
delay: 1.8,
delay: DELAY,
callback: () => {
notFirst.current = true;
},
});
} else {
gsap_ref.current.kill();
Expand All @@ -96,11 +102,19 @@ const DiscreteProgress = ({ clientWidth, scrollWidth, scrollLeft, numberOfSteps
duration: 1.2,
ease: 'power3.out',
stagger: 0.12,
delay: 1.8,
delay: DELAY,
callback: () => {
notFirst.current = true;
},
});
}
}, 100);
}, []);
}, [numberOfSteps]);

useLayoutEffect(() => {
if(notFirst.current)
setDELAY(0);
}, [numberOfSteps]);

return (
<div tw="flex space-x-2 opacity-70 md:hover:opacity-100 active:opacity-100 duration-300">
Expand Down

0 comments on commit cac2f2c

Please sign in to comment.