Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                h1 Magnetic Button
main
  .divisor
    a.magnetic-wrap.size1(href="#")
      .js-magnetic-area.magnetic-size
      .js-magnetic-content
        .my-burger
    p Perfect example.
  .divisor
    a.magnetic-wrap.size2(href="#")
      .js-magnetic-area.magnetic-area.magnetic-size.wrong
      .js-magnetic-content
        .my-burger
    p Uneven and square areas don't work.
footer

              
            
!

CSS

              
                $magnetic-size: 3rem;
$magnetic-size2: 5rem;

.magnetic-wrap {
  display: inline-block;
  position: relative;
}

.my-burger {
  width: 4rem;
  height: 4rem;
  border-radius: 2rem;
  background: #ffb31a;
}

.magnetic-size {
  position: absolute;
  bottom: -$magnetic-size;
  top: -$magnetic-size;
  left: -$magnetic-size;
  right: -$magnetic-size;
  z-index: 1; // The area has to be on top
  border-radius: 50%;
  border: 2px dashed #333; // Guideline to visualize the area
}

.size1 {
  margin: $magnetic-size;
}
.size2 {
  margin: $magnetic-size2;
}

.wrong {
  background: rgba(red, 0.2);
  bottom: -$magnetic-size2;
  top: -$magnetic-size2;
  left: -$magnetic-size2;
  right: 0;
  border-radius: 0;
}

///////////////////////////
// Made for this example //
///////////////////////////

body {
  font-family: Roboto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: rgb(15, 20, 30);
  color: white;
  min-height: 100vh;
  margin: 0;
}
main {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
p {
  text-align: center;
  color: rgba(white, 0.8);
}
.divisor {
  margin: $magnetic-size;
}

              
            
!

JS

              
                var mWrap = document.querySelectorAll(".magnetic-wrap");

function parallaxIt(e, wrap, movement = 1) {
  var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  var boundingRect = wrap.mArea.getBoundingClientRect();
  var relX = e.pageX - boundingRect.left;
  var relY = e.pageY - boundingRect.top;

  gsap.to(wrap.mContent, {
    x: (relX - boundingRect.width / 2) * movement,
    y: (relY - boundingRect.height / 2 - scrollTop) * movement,
    ease: "power1",
    duration: 0.6
  });
}

mWrap.forEach(function (wrap) {
  wrap.mContent = wrap.querySelector(".js-magnetic-content");
  wrap.mArea = wrap.querySelector(".js-magnetic-area");
  
  wrap.mArea.addEventListener("mousemove", function(e) {
    parallaxIt(e, wrap);
  });
  
  wrap.mArea.addEventListener("mouseleave", function (e) {
    gsap.to(wrap.mContent, {
      scale: 1,
      x: 0,
      y: 0,
      ease: "power3",
      duration: 0.6
    });
  });
});
              
            
!
999px

Console