can you add a scroll train animation where the skills are moving from left to riht and…
- User
can you add a scroll train animation where the skills are moving from left to riht and below from right to left and so on , it must in the same theme "import React, { useEffect, useRef } from 'react'; import { StaticImage } from 'gatsby-plugin-image'; import styled from 'styled-components'; import { srConfig } from '@config'; import sr from '@utils/sr'; import { usePrefersReducedMotion } from '@hooks';
const StyledAboutSection = styled.section` max-width: 900px;
.inner { display: grid; grid-template-columns: 3fr 2fr; grid-gap: 50px;
@media (max-width: 768px) { display: block; }}
; const StyledText = styled.divul.skills-list { display: grid; grid-template-columns: repeat(7, minmax(140px, 200px)); grid-gap: 0 5px; padding: 0; margin: 10px 0 0 0; overflow: hidden; list-style: none;li { position: relative; margin-bottom: 10px; padding-left: 20px; font-family: var(--font-mono); font-size: var(--fz-xs); &:before { content: '▹'; position: absolute; left: 0; color: var(--green); font-size: var(--fz-sm); line-height: 12px; } }}
; const StyledPic = styled.divposition: relative; max-width: 300px;@media (max-width: 768px) { margin: 50px auto 0; width: 70%; }
.wrapper { ${({ theme }) => theme.mixins.boxShadow}; display: block; position: relative; width: 100%; border-radius: var(--border-radius); background-color: var(--green);
&:hover, &:focus { outline: 0; transform: translate(-4px, -4px); &:after { transform: translate(8px, 8px); } .img { filter: none; mix-blend-mode: normal; } } .img { position: relative; border-radius: var(--border-radius); mix-blend-mode: multiply; filter: grayscale(100%) contrast(1); transition: var(--transition); } &:before, &:after { content: ''; display: block; position: absolute; width: 100%; height: 100%; border-radius: var(--border-radius); transition: var(--transition); } &:before { top: 0; left: 0; background-color: var(--navy); mix-blend-mode: screen; } &:after { border: 2px solid var(--green); top: 14px; left: 14px; z-index: -1; }} `;
const About = () => { const revealContainer = useRef(null); const prefersReducedMotion = usePrefersReducedMotion();
useEffect(() => { if (prefersReducedMotion) { return; }
sr.reveal(revealContainer.current, srConfig());}, []);
const skills = { Languages: ['C/C++', 'Java', 'Python', 'JavaScript', 'TypeScript', 'Solidity', 'Rust', 'Move'], Databases: ['SQLite', 'PostgreSQL', 'MongoDB', 'Firebase'], DevOps: ['Docker', 'Jenkins'], Others: ['Computer Networks', 'Operating Systems', 'System Design', 'Microservices', 'Cloud Computing'], Frameworks: ['Node.js', 'React.js', 'Express.js', 'Django', 'Flask'], Tools: ['Git', 'Linux', 'Docker', 'GCP', 'AWS', 'Cisco Packet Tracer'] }; return ( About Me
<div className="inner"> <StyledText> <div> <p> I Started my Journey by participating in hackathons, Events and Buildathons, Did some Freelancing and Intenships as a Full Stack Developer. </p> <p>Here are a few technologies I’ve worked on</p> <br></br> </div> {Object.keys(skills).map((category, index) => ( <div key={index}> <h5>{category}</h5> <ul className="skills-list"> {skills[category].map((skill, i) => ( <li key={i}>{skill}</li> ))} </ul> </div> ))} </StyledText> </div> </StyledAboutSection>); };
export default About; "
- Assistant
Certainly! To add a scroll train animation to your skills lists, where each category scrolls alternatively from left to right and right to left, you can use CSS animations within your styled-components. Below is the modified code with the necessary additions and explanations.
Overview of Changes
-
Animation Keyframes: Created two keyframe animations,
scrollLeftToRightandscrollRightToLeft, to handle the scrolling directions. -
Styled Components Adjustments:
StyledSkillsList: A new styled component that applies the scrolling animation based on props.StyledText: Modified to useStyledSkillsListinstead of the regularul.skills-list.
-
Alternating Directions: In the React component, passed a
directionprop toStyledSkillsListto alternate the scrolling direction for each skill category. -
Responsive and Accessibility Considerations: Ensured that animations respect user preferences for reduced motion.
Updated Code
import React, { useEffect, useRef } from 'react'; import { StaticImage } from 'gatsby-plugin-image'; import styled, { keyframes } from 'styled-components'; import { srConfig } from '@config'; import sr from '@utils/sr'; import { usePrefersReducedMotion } from '@hooks'; // Define keyframe animations for scrolling left to right and right to left const scrollLeftToRight = keyframes` 0% { transform: translateX(-100%); } 100% { transform: translateX(100%); } `; const scrollRightToLeft = keyframes` 0% { transform: translateX(100%); } 100% { transform: translateX(-100%); } `; const StyledAboutSection = styled.section` max-width: 900px; .inner { display: grid; grid-template-columns: 3fr 2fr; grid-gap: 50px; @media (max-width: 768px) { display: block; } } `; const StyledText = styled.div` padding: 1rem; div { margin-bottom: 2rem; } h5 { margin-bottom: 0.5rem; font-size: 1.2rem; color: var(--green); } `; // New styled component for the scrolling skills list const StyledSkillsList = styled.ul` display: flex; white-space: nowrap; overflow: hidden; list-style: none; padding: 0; margin: 0; /* Apply animation if not reduced motion */ ${({ prefersReducedMotion, direction }) => !prefersReducedMotion && ` animation: ${ direction === 'left-to-right' ? scrollLeftToRight : scrollRightToLeft } 10s linear infinite; `} li { display: inline-block; margin-right: 2rem; font-family: var(--font-mono); font-size: var(--fz-xs); position: relative; &:before { content: '▹'; position: absolute; left: -1.5rem; color: var(--green); font-size: var(--fz-sm); line-height: 12px; } } `; const StyledPic = styled.div` position: relative; max-width: 300px; @media (max-width: 768px) { margin: 50px auto 0; width: 70%; } .wrapper { ${({ theme }) => theme.mixins.boxShadow}; display: block; position: relative; width: 100%; border-radius: var(--border-radius); background-color: var(--green); &:hover, &:focus { outline: 0; transform: translate(-4px, -4px); &:after { transform: translate(8px, 8px); } .img { filter: none; mix-blend-mode: normal; } } .img { position: relative; border-radius: var(--border-radius); mix-blend-mode: multiply; filter: grayscale(100%) contrast(1); transition: var(--transition); } &:before, &:after { content: ''; display: block; position: absolute; width: 100%; height: 100%; border-radius: var(--border-radius); transition: var(--transition); } &:before { top: 0; left: 0; background-color: var(--navy); mix-blend-mode: screen; } &:after { border: 2px solid var(--green); top: 14px; left: 14px; z-index: -1; } } `; const About = () => { const revealContainer = useRef(null); const prefersReducedMotion = usePrefersReducedMotion(); useEffect(() => { if (prefersReducedMotion) { return; } sr.reveal(revealContainer.current, srConfig()); }, []); const skills = { Languages: ['C/C++', 'Java', 'Python', 'JavaScript', 'TypeScript', 'Solidity', 'Rust', 'Move'], Databases: ['SQLite', 'PostgreSQL', 'MongoDB', 'Firebase'], DevOps: ['Docker', 'Jenkins'], Others: ['Computer Networks', 'Operating Systems', 'System Design', 'Microservices', 'Cloud Computing'], Frameworks: ['Node.js', 'React.js', 'Express.js', 'Django', 'Flask'], Tools: ['Git', 'Linux', 'Docker', 'GCP', 'AWS', 'Cisco Packet Tracer'] }; return ( <StyledAboutSection id="about" ref={revealContainer}> <h2 className="numbered-heading">About Me</h2> <div className="inner"> <StyledText> <div> <p> I started my journey by participating in hackathons, events, and buildathons. I have also undertaken freelancing and internships as a Full Stack Developer. </p> <p>Here are a few technologies I’ve worked on:</p> <br /> </div> {Object.keys(skills).map((category, index) => ( <div key={index}> <h5>{category}</h5> <StyledSkillsList prefersReducedMotion={prefersReducedMotion} direction={index % 2 === 0 ? 'left-to-right' : 'right-to-left'} > {skills[category].map((skill, i) => ( <li key={i}>{skill}</li> ))} </StyledSkillsList> </div> ))} </StyledText> {/* If you have an image or additional content, include it here */} {/* <StyledPic> <div className="wrapper"> <StaticImage className="img" src="../../images/profile-pic.png" alt="Profile Picture" quality={95} formats={['AUTO', 'WEBP', 'AVIF']} /> </div> </StyledPic> */} </div> </StyledAboutSection> ); }; export default About;Detailed Explanation
-
Keyframe Animations:
scrollLeftToRight: Moves the list from left (-100%) to right (100%).scrollRightToLeft: Moves the list from right (100%) to left (-100%).
-
StyledSkillsList:- Display and Layout: Uses
flexandwhite-space: nowrapto keep the list items in a single line for smooth scrolling. - Overflow: Hidden to ensure that overflowing items are not visible.
- Animation: Applies either
scrollLeftToRightorscrollRightToLeftbased on thedirectionprop. The animation duration is set to10sand loops infinitely with a linear timing function. - Reduced Motion: If the user prefers reduced motion, the animation is disabled.
- Display and Layout: Uses
-
Alternating Directions:
- In the
Aboutcomponent, when mapping over the skill categories, thedirectionprop is determined by the index of the category. Even-indexed categories scroll from left to right, and odd-indexed categories scroll from right to left.
- In the
-
Accessibility:
- The
usePrefersReducedMotionhook checks if the user has enabled reduced motion in their system settings. If so, animations are disabled to respect the user's preference.
- The
-
Styling Adjustments:
- Removed the grid layout for
.skills-listand replaced it with a flex layout to facilitate smooth scrolling. - Adjusted the margin and padding to ensure that the scrolling items are spaced appropriately.
- Removed the grid layout for
-
Responsive Considerations:
- The scrolling animation is designed to work well on various screen sizes. However, feel free to adjust the
animation-durationor other properties to better suit your design needs.
- The scrolling animation is designed to work well on various screen sizes. However, feel free to adjust the
Additional Notes
-
Performance: CSS animations are generally performant, but ensure that the animation duration (
10sin this case) and properties are optimized to prevent any jank, especially on lower-end devices. -
Customization: You can adjust the
animation-durationto make the scrolling faster or slower based on your preference. -
Looping Seamlessly: For a more seamless infinite scroll, you might consider duplicating the list items and adjusting the animation to create a continuous loop without visible jumps. This requires a more complex setup and might involve additional keyframes or JavaScript.
-
Browser Compatibility: Ensure that the CSS animations used are supported across the browsers you intend to support. The current setup uses standard CSS properties that are widely supported.
Conclusion
With these modifications, your skills lists will now have a continuous scrolling animation, alternating directions for each category. This adds a dynamic and engaging visual element to your "About Me" section while maintaining the existing theme and respecting user accessibility preferences.
-