CSS transition width right to left

I made a heading (the word Welcome), which opens after loading the page ( onload="").

Fiddle if the code below does not 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>
Run codeHide result

CSS and Plain JS work fine, but I want the word “Welcome” to be shown on the right side first, and then move forward, so from e to W, instead of finding it now, it opens from left to right.

I tried text align: right;, but that doesn’t change anything.

I prefer not to use jQuery if this is a JS solution.

An example of what should look halfway though the transition:

+4
source share
6

, :

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>
Hide result

- CSS JavaScript . , ... :)

+4

transform: translate , .

function animate() {
  document.getElementById("mainText").classList.add('show');
}
#mainText {
  position: relative;
  margin: 0px;
  display: inline-block;
  font-size: 100px;
  white-space: nowrap;
  text-overflow: clip;
  overflow: hidden;
}
#mainText::after {
  content: '';
  position: absolute;
  left: 0; top: 0;
  width: 100%; height: 100%;
  background: white;
  transition: transform 2s;
}
#mainText.show::after {
  transform: translateX(-100%);
}
<body onload="animate()">

    <h1 id="mainText">Welcome</h1>
    
</body>
Hide result

, , direction left/width.

, clip-path , , , , .

function animate() {
  document.getElementById("mainText").classList.add('show');
}
body {
  background: black;
}
#mainText {
  position: relative;
  margin: 0px;
  display: inline-block;
  font-size: 100px;
  white-space: nowrap;
  color: rgba(0, 0, 0, 0);
}
#mainText::before {
  content: attr(data-text);
  position: absolute;
  left: 100%;
  top: 0;
  width: 0;
  height: 100%;
  color: white;
  direction: rtl;
  overflow: hidden;
  transition: left 2s, width 2s;
}
#mainText.show::before {
  left: 0;
  width: 100%;
}
<body onload="animate()">

  <h1 id="mainText" data-text="Welcome">Welcome</h1>

</body>
Hide result
+3

-

function animate() {
  document.getElementById("overlay").style.width = "0%";

}
#mainText {
  margin: 0px;
  display: inline-block;
  font-size: 100px;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}
#overlay{
  width: 100%;
  position:absolute;
  top:0;
  left:0;
  background:#fff;
  transition: width 2s;
  height:100%;
}
<body onload="animate()">
  <h1 id="mainText">Welcome</h1>
  <div id="overlay"></div>
</body>
Hide result
+2

clip-path , . , , forwards , "" .

inset , : -, , , .

#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>
Hide result
+1

, . is-active. , CSS.

function animate() {
  document.getElementById("mainText").className = "is-active";
}
#mainText {
  margin: 0px;
  display: inline-block;
  position: relative;
  font-size: 100px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}

#mainText:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 100%;
  background: #FFF;
  transition: width 2s;
}

#mainText.is-active:before {
  width: 0%;
}
<body onload="animate()">
  <h1 id="mainText">Welcome</h1>
</body>
Hide result
0

Transform: translateX .

X.

.message {
  color: darkred;
  font-size: 30px;
  display: inline-block;
  font-weight: 100;
  font-family: sans-serif;
}

.sliding-text-1,
.sliding-text-2,
.sliding-text-3 {
  animation-name: slide;
  animation-duration: 1s;
  animation-timing-function: ease-out;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
  opacity: 0;
}

.sliding-text-2 {
  animation-delay: 2s;
  color: darkblue;
}

.sliding-text-3 {
  animation-delay: 4s;
  color: darkgreen;
}

@keyframes slide {
  from {
    transform: translateX(200px);
  }
  to {
    transform: translateX(0px);
    opacity: 1;
  }
}
<h1 class="message sliding-text-1">Hello!</h1>
<h1 class="message sliding-text-2">Thanks for visiting!</h1>
<h1 class="message sliding-text-3">Have a nice day!</h1>
Hide result
0

Source: https://habr.com/ru/post/1680296/


All Articles