Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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

              
                <body>
  <section class="start">
    <p class="overskrift">I am shamelessly copying a <a href="https://codepen.io/Mamboleoo/pen/XgBvrJ" target="_blank">pen</a> and trying to make it work on a demo page.</p>
    <br>
    <br>
    <p>In this case of button animation are two "challenges" for me:</p>
    <br>
    <p style="color:#e53c36">First:</p>
    <p>Why the heck doesn't the animations work as expected? There's no movement at all. What am I missing here? </p>
    <br>
    <p style="color:#e53c36">
      Second (this is the more important question): </p>
    <p class="question">On <a href="http://lambacher.info/landheim/referanser.html" target="_blank">this page</a> the code is already implemented and it "works". But I have to reload the page after getting to it via link :/ <br>
      But unfortunately it gets messy when a page-user is scrolling after the first time of animation. You'll see that the pointer is no longer on the spot it was before and the animations are triggered when the cursor isn't on the area like it used to be before. <br>Why is that and what can I do to prevent this?</p>
  </section>
  <section class="sceneOne">
    Vi jobber tett i lag med:
  </section>
  <section class="sceneTwo">
    <div class="button-field">
      <ul>
        <li>
      <button class="hunton">Example-Link 1</button>
        </li>
        <li>
      <button class="byggmakker">Example-Link 2</button>
        </li>
        <li>
      <button class="byggmax">Example-Link 3</button>
        </li>
        <li>
      <button class="norgeshus">Example-Link 4</button>
        </li>
        <li>
      <button class="boligpartner">Example-Link 5</button>
        </li>
        <li>
      <button class="moelven">Example-Link 6</button>
        </li>
      </ul>
    </div>
  </section>
  <section class="end">
    Thank you for your very much appreciated help in my case!
  </section>
</body>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Arsenal&family=Montserrat&display=swap");


* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  height: 100%;
}

body {
  background: #333;
}
section {
  display: flex;
  justify-content: center;
  align-items: center;
}
.start {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  width: 100%;
  height: 500px;
  font-family: "Montserrat", sans-serif;
  font-size: 1.1rem;
  font-weight: 100;
  color: rgba(251,251,251,1);
}
.overskrift {
  font-style: oblique;
  text-decoration: none;
  
  a {
    color: #e53c36;
  }
}
.question {
  text-decoration: none;
  width: 80%;
  
  a {
    color: #e53c36;
  }
}
.sceneOne {
  width: 100%;
  font-family: "Montserrat", sans-serif;
  font-size: 1.5rem;
  font-weight: 300;
  color: #e53c36;
}
.sceneTwo {
  width: 100%;
}
.button-field {
  margin-top: 50px;
  width: 90%;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
ul{
  padding: 0;
}
li{
  display: inline-block;
  list-style-type: none;
  margin: 0 20px;
}
button {
  position: relative;
  width: 200px;
  height: 200px;
  border-radius: 50%;
  border: 6px solid rgba(251,251,251,1);
  outline: 2px solid #e53c36;
  cursor: pointer;
  font-family: "Arsenal", sans-serif;
  font-size: 1.2rem;
  font-weight: 100;
  color: rgba(251,251,251,1);
  padding: 0;
}
button.hunton {
  background: url(https://lambacher.info/landheim/media/logo_hunton.svg), linear-gradient(rgba(251,251,251,0), rgba(251,251,251,0.3), rgba(251,251,251,0));
  background-size: 40%, 100%;
  background-repeat: no-repeat;
  background-position: center center;
}
button.byggmakker {
  background: url(https://lambacher.info/landheim/media/logo_byggmakker.svg), linear-gradient(rgba(251,251,251,0), rgba(251,251,251,0.3), rgba(251,251,251,0));
  background-size: 90%, 100%;
  background-repeat: no-repeat;
  background-position: center center;
}
button.byggmax {
  background: url(https://lambacher.info/landheim/media/logo_byggmax.svg), linear-gradient(rgba(251,251,251,0), rgba(251,251,251,0.3), rgba(251,251,251,0));
  background-size: 90%, 100%;
  background-repeat: no-repeat;
  background-position: center center;
}
button.norgeshus {
  background: url(https://lambacher.info/landheim/media/logo_norgeshus.svg), linear-gradient(rgba(251,251,251,0), rgba(251,251,251,0.3), rgba(251,251,251,0));
  background-size: 90%, 100%;
  background-repeat: no-repeat;
  background-position: center center;
}
button.boligpartner {
  background: url(https://lambacher.info/landheim/media/logo_boligpartner.svg), linear-gradient(rgba(251,251,251,0), rgba(251,251,251,0.3), rgba(251,251,251,0));
  background-size: 90%, 100%;
  background-repeat: no-repeat;
  background-position: center center;
}
button.moelven {
  background: url(https://lambacher.info/landheim/media/logo_moelven.svg), linear-gradient(rgba(251,251,251,0), rgba(251,251,251,0.3), rgba(251,251,251,0));
  background-size: 90%, 100%;
  background-repeat: no-repeat;
  background-position: center center;
}
.end {
  font-family: "Montserrat", sans-serif;
  font-size: 1.1rem;
  font-weight: 100;
  color: #e53c36;
  width: 100%;
  height: 500px;
}
              
            
!

JS

              
                class HoverButton {
  constructor(el) {
    this.el = el;
    this.hover = false;
    this.calculatePosition();
    this.attachEventsListener();
  }

  attachEventsListener() {
    window.addEventListener("mousemove", (e) => this.onMouseMove(e));
    window.addEventListener("resize", (e) => this.calculatePosition(e));
  }

  calculatePosition() {
    gsap.set(this.el, {
      x: 0,
      y: 0,
      scale: 1,
    });

    //const box = this.el.getBoundingClientRect();
    //this.x = box.left + box.width * 0.5;
    //this.y = box.top + box.height * 0.5;
    //this.width = box.width;
    //this.height = box.height;
    
    
    const box = this.el.getBoundingClientRect();
    this.x = this.el.offsetLeft + box.width * 0.5;
    this.y = this.el.offsetTop + box.height * 0.5;
    this.width = box.width;
    this.height = box.height;
    
  }

  onMouseMove(e) {
    let hover = false;
    let hoverArea = this.hover ? 0.7 : 0.5;
    let x = e.pageX - this.x;
    let y = e.pageY - this.y;
    let distance = Math.sqrt(x * x + y * y);
    if (distance < this.width * hoverArea) {
      hover = true;
      if (!this.hover) {
        this.hover = true;
      }
      this.onHover(e.pageX, e.pageY);
    }

    if (!hover && this.hover) {
      this.onLeave();
      this.hover = false;
    }
  }

  onHover(x, y) {
    gsap.to(this.el, {
      x: (x - this.x) * 0.4,
      y: (y - this.y) * 0.4,
      scale: 1.15,
      ease: "power2.out",
      duration: 0.4,
    });

    this.el.style.zIndex = 10;
  }
  onLeave() {
    gsap.to(this.el, {
      x: 0,
      y: 0,
      scale: 1,
      ease: "elastic.out(1.2, 0.4)",
      duration: 0.7,
    });

    this.el.style.zIndex = 1;
  }
}

const btn1 = document.querySelector("button.hunton");
new HoverButton(btn1);

const btn2 = document.querySelector("button.byggmakker");
new HoverButton(btn2);

const btn3 = document.querySelector("button.byggmax");
new HoverButton(btn3);

const btn4 = document.querySelector("button.norgeshus");
new HoverButton(btn4);

const btn5 = document.querySelector("button.boligpartner");
new HoverButton(btn5);

const btn6 = document.querySelector("button.moelven");
new HoverButton(btn6);

              
            
!
999px

Console