Smooth Scrolling in ReactJS
How do I make an smooth scrolling in reactjs?
I was working on a ReactJS project and didn't want to use jQuery to do the smooth scroll animations, so here is a simple example of how to accomplish the same with JS. The smoothScroll var would be required in module.. What is a smooth scrolling in reactjs? How do you make a smooth scrolling in reactjs? This script and codes were developed by Brian on 22 November 2022, Tuesday.
Smooth Scrolling in ReactJS - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Smooth Scrolling in ReactJS</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="app"></div> <script src='http://cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Smooth Scrolling in ReactJS - Script Codes CSS Codes
body { color: white; background: #1c1c1f;
}
.main { font: 16px / 1 Arial; position: relative;
}
.smooth-scroll { padding: 50px;
}
.smooth-scroll--spacer { border: 1px dashed #fff; height: 2000px;
}
.smooth-scroll button { color: #000; font: bold 18px/1 Arial; margin: 10px 0; padding: 10px; width: 100%;
}
Smooth Scrolling in ReactJS - Script Codes JS Codes
"use strict";
var App = React.createClass({ displayName: "App", render: function render() { return React.createElement( "div", null, React.createElement(SmoothScroll, null) ); }
});
var smoothScroll = { timer: null, stop: function stop() { clearTimeout(this.timer); }, scrollTo: function scrollTo(id, callback) { var settings = { duration: 1000, easing: { outQuint: function outQuint(x, t, b, c, d) { return c * ((t = t / d - 1) * t * t * t * t + 1) + b; } } }; var percentage; var startTime; var node = document.getElementById(id); var nodeTop = node.offsetTop; var nodeHeight = node.offsetHeight; var body = document.body; var html = document.documentElement; var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight); var windowHeight = window.innerHeight; var offset = window.pageYOffset; var delta = nodeTop - offset; var bottomScrollableY = height - windowHeight; var targetY = bottomScrollableY < delta ? bottomScrollableY - (height - nodeTop - nodeHeight + offset) : delta; startTime = Date.now(); percentage = 0; if (this.timer) { clearInterval(this.timer); } function step() { var yScroll; var elapsed = Date.now() - startTime; if (elapsed > settings.duration) { clearTimeout(this.timer); } percentage = elapsed / settings.duration; if (percentage > 1) { clearTimeout(this.timer); if (callback) { callback(); } } else { yScroll = settings.easing.outQuint(0, elapsed, offset, targetY, settings.duration); window.scrollTo(0, yScroll); this.timer = setTimeout(step, 10); } } this.timer = setTimeout(step, 10); }
};
var SmoothScroll = React.createClass({ displayName: "SmoothScroll", render: function render() { return React.createElement( "div", { className: "smooth-scroll" }, React.createElement( "button", { id: "top", onClick: this.handleTopClick }, "scroll to bottom" ), React.createElement("div", { className: "smooth-scroll--spacer" }), React.createElement( "button", { id: "bottom", onClick: this.handleBottomClick }, "scroll to top" ) ); }, handleTopClick: function handleTopClick() { smoothScroll.scrollTo('bottom'); }, handleBottomClick: function handleBottomClick() { smoothScroll.scrollTo('top'); }
});
React.render(React.createElement(App, null), document.getElementById('app'));
Developer | Brian |
Username | brian-baum |
Uploaded | November 22, 2022 |
Rating | 3 |
Size | 4,005 Kb |
Views | 38,456 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
JQuery countdown plugin | 4,460 Kb |
ReactJS Image Carousel | 5,932 Kb |
JQuery hash nav plugin | 5,306 Kb |
AngularJS contact list | 4,052 Kb |
CSS dropdowns | 3,146 Kb |
ReactJS Accessible Accordion Panel | 5,767 Kb |
Responsive demo page | 5,664 Kb |
JQuery exercise | 3,780 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Gradient-ui-button-mark-fx | Tabspace | 1,881 Kb |
Sticky notes with CSS3 | HaiNguyen007 | 2,146 Kb |
Main page display | BarryKe | 4,562 Kb |
Progressively reveal dots on a Bezier curve | GreenSock | 2,489 Kb |
Blockquote design | Sjmcpherson | 1,863 Kb |
RRC wrapSwitch | Pshrmn | 2,922 Kb |
A Pen by Michael Parenteau | Michaelparenteau | 2,133 Kb |
Improve | Gavra | 1,652 Kb |
Pictos font library from CodePen | Jstam | 3,790 Kb |
Segments mouse following | Nosir | 2,909 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!