Home > Mobile >  How to turn around hover effect?
How to turn around hover effect?

Time:10-05

I need to turn around the hover effect of an element. Right now, the border disappears when you hover over it, but I want it to be hidden by default and only appears when you hover over it?

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #fcf3ec;
}

.button {
  --offset: 10px;
  --border-size: 2px;
  display: block;
  position: relative;
  padding: 1.5em 3em;
  appearance: none;
  border: 0;
  background: transparent;
  color: #e55743;
  text-transform: uppercase;
  letter-spacing: .25em;
  outline: none;
  cursor: pointer;
  font-weight: bold;
  border-radius: 0;
  box-shadow: inset 0 0 0 var(--border-size) currentcolor;
  transition: background .8s ease;
}

.button:hover {
  background: rgba(100, 0, 0, .03);
}

.button__horizontal,
.button__vertical {
  position: absolute;
  top: var(--horizontal-offset, 0);
  right: var(--vertical-offset, 0);
  bottom: var(--horizontal-offset, 0);
  left: var(--vertical-offset, 0);
  transition: transform .8s ease;
  will-change: transform;
}

.button__horizontal::before .button__vertical::before {
  content: '';
  position: absolute;
  border: inherit;
}

.button__horizontal {
  --vertical-offset: calc(var(--offset) * -1);
  border-top: var(--border-size) solid currentcolor;
  border-bottom: var(--border-size) solid currentcolor;
}

.button__horizontal::before {
  top: calc(var(--vertical-offset) - var(--border-size));
  bottom: calc(var(--vertical-offset) - var(--border-size));
  left: calc(var(--vertical-offset) * -1);
  right: calc(var(--vertical-offset) * -1);
}

.button:hover .button__horizontal {
  transform: scaleX(0);
}

.button__vertical {
  --horizontal-offset: calc(var(--offset) * -1);
  border-left: var(--border-size) solid currentcolor;
  border-right: var(--border-size) solid currentcolor;
}

.button__vertical::before {
  top: calc(var(--horizontal-offset) * -1);
  bottom: calc(var(--horizontal-offset) * -1);
  left: calc(var(--horizontal-offset) - var(--border-size));
  right: calc(var(--horizontal-offset) - var(--border-size));
}

.button:hover .button__vertical {
  transform: scaleY(0);
}
<button >
  Fancy Button
  <div ></div>
  <div ></div>
</button>

Here you can see how the effect works and also the original code:

https://codepen.io/electerious/details/qPjbGm

How can I make this effect the other way round?

CodePudding user response:

All you need to do is change where you use the transform.

.button__vertical {
  transform: scaleY(0);
  --horizontal-offset: calc(var(--offset) * -1);
  border-left: var(--border-size) solid currentcolor;
  border-right: var(--border-size) solid currentcolor;
}

.button:hover .button__vertical {
  transform: scaleY(1);
}

With that its not visible and once you hover you scale it. you will obvs need to do the same for the button__horizontal class but for the scaleX

CodePudding user response:

Here is an easier idea with less of code and only the button tag:

.button {
  --offset: 10px;
  --border-size: 2px;
  
  padding: 1.5em 3em;
  border: var(--offset) solid transparent;
  appearance: none;
  color: #e55743;
  text-transform: uppercase;
  letter-spacing: .25em;
  cursor: pointer;
  font-weight: bold;
  --_g: currentColor var(--border-size),#0000 0 calc(100% - var(--border-size)),currentColor 0;
  background: 
    linear-gradient(90deg,var(--_g)) 50%/calc(100% - 2*var(--offset)) 100% border-box,
    linear-gradient(      var(--_g)) 50%/100% calc(100% - 2*var(--offset)) border-box,
    padding-box;
  background-repeat: no-repeat;
  clip-path: inset(var(--offset));
  transition: .8s ease;
}
.button:hover {
  background-color: rgba(100, 0, 0, .03);
  clip-path: inset(0);
}

body {
  display: grid;
  place-content: center;
  height: 100vh;
  margin: 0;
  background: #fcf3ec;
}
<button >Fancy Button</button>

  • Related