content logo

React Buttons:

React Circular Magnetic Button Follows Mouse Cursor

By using the magnet buttons, you will both create entertainment for the user and encourage the user to click on the button you want. In this post, we have prepared a models of magnetic buttons for you. This button follow the mouse a little after the mouse is removed from it, and for this reason, the magnetic mood is felt. This button have gray themes.

#

Circular Magnetic Button

#

Dynamic React Button

#

HTML Magnetic Button

#

Button Following Cursor Effect

<!-- This script got from frontendfreecode.com -->
<main></main><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
* {
	box-sizing: border-box;
}
html, body {
	height: 100%;
	margin: 0;
}
body {
	display: flex;
	justify-content: center;
	align-items: center;
}
button {
	border: none;
	margin: 0;
	padding: 0;
	width: auto;
	overflow: visible;
	background: transparent;
	color: inherit;
	font: inherit;
	line-height: normal;
	text-align: center;
	text-decoration: none;
	outline: none;
	cursor: pointer;
	white-space: normal;
	-webkit-font-smoothing: inherit;
	-moz-osx-font-smoothing: inherit;
	-webkit-appearance: none;
}
.u-fit {
	width: 100%;
	height: 100%;
}
.u-relative {
	position: relative;
}
.u-absolute {
	position: absolute;
}
.u-absolute.u-pos-tl {
	top: 0;
	left: 0;
}
.u-flex {
	display: flex;
}
.u-flex.u-center-xy {
	justify-content: center;
	align-items: center;
}
.c-magnetic-btn {
	width: 120px;
	height: 120px;
	border-radius: 50%;
}
.o-circle {
	display: inline-block;
	background-color: rgba(0, 0, 0, 0.015);
	border: 2px solid rgba(0, 0, 0, 0.5);
	border-radius: 50%;
	z-index: -1;
	opacity: 0.2;
	transition: opacity 0.6s cubic-bezier(0.23, 1, 0.32, 1);
	will-change: opacity;
}
.t-btn-label {
	display: inline-block;
	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
	font-family: "Karla", sans-serif;
	font-weight: 700;
	letter-spacing: 1px;
	line-height: 1;
	color: rgba(0, 0, 0, 0.5);
	font-size: 13px;
}
.is-hover .o-circle {
	opacity: 0.4;
}
a {
	position: absolute;
	bottom: 0;
	right: 0;
	padding: 8px 16px;
	transform-origin: bottom right;
	transform: rotate(-90deg) translateX(100%);
	font-family: "Karla", sans-serif;
	text-decoration: none;
	color: rgba(0, 0, 0, 0.75);
	font-size: 13px;
	text-transform: uppercase;
	letter-spacing: 1px;
	opacity: 0.5;
	transition: opacity 0.4s cubic-bezier(0.23, 1, 0.32, 1);
}
a:hover {
	opacity: 1;
}
function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}const { Component } = React;
const { render } = ReactDOM;
const { mapValues, toNumber } = _;
const settings = {
  threshold: 80,
  ratio: 15,
  max: 100,
  scale: 1.2,
  ease: 0.14,
  label: 'Hover me.' };
class MagneticButton extends Component {constructor(...args) {super(...args);_defineProperty(this, "state",
    {
      window: { width: window.innerWidth, height: window.innerHeight },
      mouse: { x: 0, y: 0 },
      isMagnetic: false,
      ease: { x: 0, y: 0, scale: 1 },
      bounds: {} });_defineProperty(this, "resize",
    () => {
      const btn = this.btn.getBoundingClientRect();
      const state = {
        window: {
          width: window.innerWidth,
          height: window.innerHeight },
        bounds: {
          width: btn.width,
          height: btn.height,
          top: btn.top,
          left: btn.left } };
      this.setState({ ...state });
    });_defineProperty(this, "mouseMove",
    ({ pageX: x, pageY: y }) => {
      this.setState({
        mouse: { x, y },
        isMagnetic: this.isMagnetic(x, y) });
    });_defineProperty(this, "run",
    () => {
      const ease = { ...this.state.ease };
      const transform = this.getTransforms();
      Object.keys(transform).forEach(key => this.getEase(key, transform, ease));
      this.setState({ ease: this.fixValues(ease) });
      requestAnimationFrame(this.run);
    });}componentDidMount() {this.addEvents();this.resize();this.run();}addEvents() {window.addEventListener('resize', this.resize);document.addEventListener('mousemove', this.mouseMove);}isMagnetic(x, y) {const { bounds } = this.state;const centerX = bounds.left + bounds.width / 2;const centerY = bounds.top + bounds.height / 2;const a = Math.abs(centerX - x);const b = Math.abs(centerY - y);const c = Math.sqrt(a * a + b * b);return c < bounds.width / 2 + this.getThreshold();}getThreshold() {const { isMagnetic } = this.state;const { threshold, ratio } = this.props;return isMagnetic ? threshold * ratio : threshold;}
  getTransforms() {
    const { isMagnetic, mouse, window } = this.state;
    const { max, scale } = this.props;
    return {
      x: isMagnetic ? (mouse.x - window.width / 2) / window.width * max : 0,
      y: isMagnetic ? (mouse.y - window.height / 2) / window.height * max : 0,
      scale: isMagnetic ? scale : 1 };
  }
  getEase(key, target, value) {
    return value[key] += (target[key] - value[key]) * this.props.ease;
  }
  fixValues(obj) {
    return mapValues(obj, x => toNumber(x.toFixed(2)));
  }
  render() {
    const { ratio } = this.props;
    const isHoverClass = this.state.isMagnetic ? 'is-hover' : '';
    const { x, y, scale } = this.state.ease;
    const style = {
      btn: { transform: `translate3d(${x}px, ${y}px, 0) scale(${scale})` },
      label: { transform: `translate3d(${-x / ratio}px, ${-y / ratio}px, 0) scale(${1 / scale})` } };
    return /*#__PURE__*/(
      React.createElement("button", { style: style.btn, className: `c-magnetic-btn u-relative ${isHoverClass}`, ref: btn => this.btn = btn }, /*#__PURE__*/
      React.createElement("span", { className: "t-btn-label u-flex u-center-xy", style: style.label }, this.props.label), /*#__PURE__*/
      React.createElement("span", { className: "o-circle u-fit u-absolute u-pos-tl" })));
  }}
render( /*#__PURE__*/React.createElement(MagneticButton, settings), document.querySelector('main'));
<link href="https://fonts.googleapis.com/css?family=Karla:400,700" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<link href="https://fonts.googleapis.com/css?family=Karla:400,700" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js'></script>
<style>
* {
	box-sizing: border-box;
}
html, body {
	height: 100%;
	margin: 0;
}
body {
	display: flex;
	justify-content: center;
	align-items: center;
}
button {
	border: none;
	margin: 0;
	padding: 0;
	width: auto;
	overflow: visible;
	background: transparent;
	color: inherit;
	font: inherit;
	line-height: normal;
	text-align: center;
	text-decoration: none;
	outline: none;
	cursor: pointer;
	white-space: normal;
	-webkit-font-smoothing: inherit;
	-moz-osx-font-smoothing: inherit;
	-webkit-appearance: none;
}
.u-fit {
	width: 100%;
	height: 100%;
}
.u-relative {
	position: relative;
}
.u-absolute {
	position: absolute;
}
.u-absolute.u-pos-tl {
	top: 0;
	left: 0;
}
.u-flex {
	display: flex;
}
.u-flex.u-center-xy {
	justify-content: center;
	align-items: center;
}
.c-magnetic-btn {
	width: 120px;
	height: 120px;
	border-radius: 50%;
}
.o-circle {
	display: inline-block;
	background-color: rgba(0, 0, 0, 0.015);
	border: 2px solid rgba(0, 0, 0, 0.5);
	border-radius: 50%;
	z-index: -1;
	opacity: 0.2;
	transition: opacity 0.6s cubic-bezier(0.23, 1, 0.32, 1);
	will-change: opacity;
}
.t-btn-label {
	display: inline-block;
	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
	font-family: "Karla", sans-serif;
	font-weight: 700;
	letter-spacing: 1px;
	line-height: 1;
	color: rgba(0, 0, 0, 0.5);
	font-size: 13px;
}
.is-hover .o-circle {
	opacity: 0.4;
}
a {
	position: absolute;
	bottom: 0;
	right: 0;
	padding: 8px 16px;
	transform-origin: bottom right;
	transform: rotate(-90deg) translateX(100%);
	font-family: "Karla", sans-serif;
	text-decoration: none;
	color: rgba(0, 0, 0, 0.75);
	font-size: 13px;
	text-transform: uppercase;
	letter-spacing: 1px;
	opacity: 0.5;
	transition: opacity 0.4s cubic-bezier(0.23, 1, 0.32, 1);
}
a:hover {
	opacity: 1;
}
</style>

</head>
<body>
<main></main><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}const { Component } = React;
const { render } = ReactDOM;
const { mapValues, toNumber } = _;
const settings = {
  threshold: 80,
  ratio: 15,
  max: 100,
  scale: 1.2,
  ease: 0.14,
  label: 'Hover me.' };
class MagneticButton extends Component {constructor(...args) {super(...args);_defineProperty(this, "state",
    {
      window: { width: window.innerWidth, height: window.innerHeight },
      mouse: { x: 0, y: 0 },
      isMagnetic: false,
      ease: { x: 0, y: 0, scale: 1 },
      bounds: {} });_defineProperty(this, "resize",
    () => {
      const btn = this.btn.getBoundingClientRect();
      const state = {
        window: {
          width: window.innerWidth,
          height: window.innerHeight },
        bounds: {
          width: btn.width,
          height: btn.height,
          top: btn.top,
          left: btn.left } };
      this.setState({ ...state });
    });_defineProperty(this, "mouseMove",
    ({ pageX: x, pageY: y }) => {
      this.setState({
        mouse: { x, y },
        isMagnetic: this.isMagnetic(x, y) });
    });_defineProperty(this, "run",
    () => {
      const ease = { ...this.state.ease };
      const transform = this.getTransforms();
      Object.keys(transform).forEach(key => this.getEase(key, transform, ease));
      this.setState({ ease: this.fixValues(ease) });
      requestAnimationFrame(this.run);
    });}componentDidMount() {this.addEvents();this.resize();this.run();}addEvents() {window.addEventListener('resize', this.resize);document.addEventListener('mousemove', this.mouseMove);}isMagnetic(x, y) {const { bounds } = this.state;const centerX = bounds.left + bounds.width / 2;const centerY = bounds.top + bounds.height / 2;const a = Math.abs(centerX - x);const b = Math.abs(centerY - y);const c = Math.sqrt(a * a + b * b);return c < bounds.width / 2 + this.getThreshold();}getThreshold() {const { isMagnetic } = this.state;const { threshold, ratio } = this.props;return isMagnetic ? threshold * ratio : threshold;}
  getTransforms() {
    const { isMagnetic, mouse, window } = this.state;
    const { max, scale } = this.props;
    return {
      x: isMagnetic ? (mouse.x - window.width / 2) / window.width * max : 0,
      y: isMagnetic ? (mouse.y - window.height / 2) / window.height * max : 0,
      scale: isMagnetic ? scale : 1 };
  }
  getEase(key, target, value) {
    return value[key] += (target[key] - value[key]) * this.props.ease;
  }
  fixValues(obj) {
    return mapValues(obj, x => toNumber(x.toFixed(2)));
  }
  render() {
    const { ratio } = this.props;
    const isHoverClass = this.state.isMagnetic ? 'is-hover' : '';
    const { x, y, scale } = this.state.ease;
    const style = {
      btn: { transform: `translate3d(${x}px, ${y}px, 0) scale(${scale})` },
      label: { transform: `translate3d(${-x / ratio}px, ${-y / ratio}px, 0) scale(${1 / scale})` } };
    return /*#__PURE__*/(
      React.createElement("button", { style: style.btn, className: `c-magnetic-btn u-relative ${isHoverClass}`, ref: btn => this.btn = btn }, /*#__PURE__*/
      React.createElement("span", { className: "t-btn-label u-flex u-center-xy", style: style.label }, this.props.label), /*#__PURE__*/
      React.createElement("span", { className: "o-circle u-fit u-absolute u-pos-tl" })));
  }}
render( /*#__PURE__*/React.createElement(MagneticButton, settings), document.querySelector('main'));
</script>

</body>
</html>
Preview