Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Transition Width Right to Left

I am have made a heading (the word Welcome) that reveals itself once the page has loaded (onload="").

Fiddle in case the code below doesn't work.

function animate() {
  document.getElementById("mainText").style.width = "100%";

}
#mainText {
  margin: 0px;
  display: inline-block;
  font-size: 100px;
  width: 0%;
  transition: width 2s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}
<body onload="animate()">
  <h1 id="mainText">Welcome</h1>
</body>

The CSS and Plain JS work fine but I want the word "Welcome" to be revealed right side first and then moving along, so from the e to the W, instead of how it currently is, which opens left to right.

I have tried text align: right;, but this doesn't change anything.

I preferably don't want to use any jQuery, if the solution is a JS one.

An example of what the desired look should be, half way though the transition:

like image 417
Oliver Murfett Avatar asked Jun 28 '17 10:06

Oliver Murfett


People also ask

Can you transition position in CSS?

CSS transitions provide a way to control animation speed when changing CSS properties. Instead of having property changes take effect immediately, you can cause the changes in a property to take place over a period of time.

How do you transition multiple properties in CSS?

Transitioning two or more properties You can transition two (or more) CSS properties by separating them with a comma in your transition or transition-property property. You can do the same with duration, timing-functions and delays as well. If the values are the same, you only need to specify one of them.

How do I use WebKit transition in CSS?

Use the @supports (transition) feature query instead. The -webkit-transition Boolean non-standardCSS media feature is a WebKit extension whose value is true if the browsing context supports CSS transitions. Apple has a description in Safari CSS Reference; this is now called transition there.


2 Answers

You can use the clip-path property to clip parts of the element so they are not visible. This property can also be animated to reveal the element again, using the forwards keyword in the animation so it stays in it's 'revealed' end state.

The inset takes values that are in order: from-top, from-right, from-bottom, from-left.

#text {
  margin: 0;
  font-size: 100px;
  animation: reveal 2s forwards;
}

@keyframes reveal {
  from {
    clip-path: inset(0 0 0 100%);
  }
  to {
    clip-path: inset(0 0 0 0);
  }
}
<h1 id="text">Welcome</h1>
like image 69
Jon Koops Avatar answered Sep 21 '22 13:09

Jon Koops


Yes, it is possible using Transitions and Positions:

window.onload = function () {
  document.querySelector("h1").classList.add("active");
};
h1 {
  overflow: hidden;
  display: inline-block;
  position: relative;
}
h1 .mask {
  position: absolute;
  transition: all 0.5s linear;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background-color: #fff;
}
h1.active .mask {
  right: 100%;
}
<h1><span class="mask"></span>Welcome</h1>

I just wrote an article about this - CSS Transitions & JavaScript for Animated Entry Effects. Hope it is useful... :)

like image 45
Praveen Kumar Purushothaman Avatar answered Sep 19 '22 13:09

Praveen Kumar Purushothaman