score:2
Accepted answer
import React from "react";
import "./style.css";
import { useRef } from "react";
import styled from "styled-components";
const Wrapper = styled.div`
margin: 5% 40%;
.test {
height: 300px;
width: 100px;
overflow: scroll;
.inner {
background-image: linear-gradient(red, blue);
height: 800px;
width: 100%;
}
}
`;
const Scroll = () => {
const step = 10;
const scrollRef = useRef();
const isScrollRef = useRef();
const setMove = (state) => isScrollRef.current = state;
const move = () => {
if (isScrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollTop + step;
requestAnimationFrame(move);
}
};
return (
<Wrapper>
<div ref={scrollRef} className="test">
<div className="inner"></div>
</div>
<button
onMouseDown={() => { setMove(true); move();}}
onMouseUp={() => setMove(false)}
>
HOLD DOWN TO SCROLL
</button>
</Wrapper>
);
};
export default Scroll;
export default function App() {
return (
<div>
<Scroll />
</div>
);
}
score:2
import { useRef, useState, useLayoutEffect, useEffect } from "react";
import styled from "styled-components";
const Wrapper = styled.div`
margin: 5% 40%;
.test {
height: 300px;
width: 100px;
overflow: scroll;
.inner {
background-image: linear-gradient(red, blue);
height: 800px;
width: 100%;
}
}
`;
function useInterval(callback, active, speed, speedRange = 10) {
const savedCallback = useRef(callback);
const intervalIdRef = useRef(null);
useLayoutEffect(() => {
savedCallback.current = callback;
}, [callback]);
useEffect(() => {
clearInterval(intervalIdRef.current);
if (speedRange < 1) {
console.error(`Speed range must be >= 1`);
return;
}
if (!(speed >= 1 && speed <= speedRange) || speed < 1) {
console.error(`Speed must be in range [1...${speedRange}]`);
return;
}
if (!active) {
return;
}
intervalIdRef.current = setInterval(
() => savedCallback.current(),
speedRange / speed
);
return () => clearInterval(intervalIdRef.current);
}, [active, speed, speedRange]);
}
const Scroll = () => {
const scrollRef = useRef();
const [speed, setSpeed] = useState(1);
//Default speedRange is 10 = 10 different speeds [1..10]
// (The higher this number the less smooth are the low speeds,
// but there will be more speeds to chose from)
const [speedRange, setSpeedRange] = useState(10);
const [scrolling, setScrolling] = useState(false);
useInterval(
() => {
scrollRef.current.scrollTop += 1;
},
scrolling,
speed,
speedRange
);
return (
<Wrapper>
<div ref={scrollRef} className="test">
<div className="inner"></div>
</div>
<button
onMouseDown={() => setScrolling(true)}
onMouseUp={() => setScrolling(false)}
>
HOLD DOWN TO SCROLL
</button>
</Wrapper>
);
};
export default Scroll;
Source: stackoverflow.com
Related Query
- Continuous scroll animation while mouse down or button hovered in ReactJs
- ReactJS how to render a component only when scroll down and reach it on the page?
- Make scroll to top button with animation in React
- Scroll down the page to another React component on a button click
- react trading view widget how to stop scroll on mouse wheel up down
- Use animation to smoothly display hidden button when hitting bottom of scroll
- 'this' issue while mapping a List with button reactjs
- Show a certain element in a component on scroll down and hide on scroll Up Reactjs
- while building infinite loading via API in REACT , the page number is not updating in console even when i scroll down a lot
- Certain views loading while others do not on back button press ReactJS
- ReactJS hooks not directing to other component while clicking login button
- How do I trigger a callback continuously while mouse is down in Rect?
- reactjs scroll to bottom with user defined speed when user click start button
- How to place a scroll down arrow at the bottom of a button card
- React scroll to top animation button does not work
- ReactJS delay onChange while typing
- Get scroll position with Reactjs
- Handling scroll Animation in React
- Facing issue while adding radio button in react - input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`
- react show button on mouse enter
- ReactJS change color of element on scroll and when certain position is reached
- Mouse wheel events in Reactjs
- Submit form in ReactJS using BUTTON element
- Syntax Error "Expecting expression, got '<'" while trying to run reactjs tutorial
- ReactJS + Redux: How to structure action creators down to each component?
- How to change button text in ReactJS
- Scroll page to the nested React component on a button click
- How to replay a CSS3 animation in Reactjs
- how to add row to a table using ReactJS on button click
- How to read and upload a file in reactjs using custom button
More Query from same tag
- What is wrong in the below code of input validation?
- getting error : function is not defined in react
- Not able to access store on non react component
- redux-form custom dynamic validation inside FieldArray
- Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'
- Javascript (React + Flow) - Destructuring with nullable nested objects
- How to do form validation in react?
- Best way to slice a string that is stored in an object
- How to not call any api endpoints if firebase token is expired in React app?
- What is wrong with my set state on a React.JS component?
- How do I create trapezoid div with centered text?
- ReactJs - Insert a new css class on element when a especific NavLink is active
- How to conditionally import file in Reactjs
- Reactjs state update after localstorage data changes
- how to protect routes in react? Need to authenticate if admin logged in
- redux thunk wait data from a dispatch before to do an another dispatch
- Attempted import error: 'Container' is not exported from 'react-bootstrap'
- How can I create two <div>, but one of them has to be fixed in the center
- Change the element wrapping its children based on a condition in JSX
- onClick function does not remove item in an array
- Change currency state from a default const object
- PDF is blank when opened
- React js get value of component's state from container
- ERROR in ./src/index.js Module parse failed:Unexpected token (13:16) You may need an appropriate loader to handle this file type
- React Route causes to class change
- ReactJS router links not working when clicking them
- Why is my UI not refreshing after setState()? (using React and Rails)
- Using webpack aliases in mocha tests
- Sorting React table by clicking table headers for columns with numerical and string values
- Recharts is not working for the React with typescript