claudios
claudios

Reputation: 6656

Animate image like it's moving from right to left

I am trying to animate a floor which should move from right to left like the clouds and it work but the problem is it overflows outside the container. How can I prevent this to happen? Sorry I am new to css animation. Thanks in advance.

Here is the fiddle I made: Click here

.container {
    width: 50%;
    margin: 0 auto;
    text-align: center;
}

#clouds{
    padding: 100px 0;
    background: #3598dc;
    background: -webkit-linear-gradient(top, #3598dc 0%, #fff 100%);
    background: -linear-gradient(top, #3598dc 0%, #fff 100%);
    background: -moz-linear-gradient(top, #3598dc 0%, #fff 100%);
}

.cloud {
    width: 200px; height: 60px;
    background: #fff;
    border-radius: 200px;
    -moz-border-radius: 200px;
    -webkit-border-radius: 200px;
    position: relative;
}

.cloud:before, .cloud:after {
    content: '';
    position: absolute;
    background: #fff;
    width: 100px; height: 80px;
    position: absolute; top: -15px; left: 10px;

    border-radius: 100px;
    -moz-border-radius: 100px;
    -webkit-border-radius: 100px;
    -webkit-transform: rotate(30deg);
    transform: rotate(30deg);
    -moz-transform: rotate(30deg);
}

.cloud:after {
    width: 120px; height: 120px;
    top: -55px; left: auto; right: 15px;
}

#character{
    margin-top: -120px;
    background:url(http://i.imgur.com/Zjsbcni.png);
    width: 84px;
    height: 126px;
    animation: walk 1.0s steps(6) infinite;
    margin-left: 425px;
}

#flooring {
    height: 39px;
    background-image: url(http://i.imgur.com/usFekrp.png);
    animation: movefloor 20s linear infinite;
    overflow: hidden;
}

@keyframes movefloor {
    0% {margin-left: 1000px;}
    100% {margin-left: -1000px;}
}

@keyframes walk {
    from { background-position: 0px; }
    to { background-position: -500px; }
}

.x1 {
    -webkit-animation: moveclouds 15s linear infinite;
    -moz-animation: moveclouds 15s linear infinite;
    -o-animation: moveclouds 15s linear infinite;
}

.x2 {
    left: 200px;
    -webkit-transform: scale(0.6);
    -moz-transform: scale(0.6);
    transform: scale(0.6);
    opacity: 0.6; /*opacity proportional to the size*/

    -webkit-animation: moveclouds 25s linear infinite;
    -moz-animation: moveclouds 25s linear infinite;
    -o-animation: moveclouds 25s linear infinite;
}

.x3 {
    left: 252px;
    top: -200px;
    -webkit-transform: scale(0.8);
    -moz-transform: scale(0.8);
    transform: scale(0.8);
    opacity: 0.8; /*opacity proportional to the size*/

    -webkit-animation: moveclouds 20s linear infinite;
    -moz-animation: moveclouds 20s linear infinite;
    -o-animation: moveclouds 20s linear infinite;
}


@-webkit-keyframes moveclouds {
    0% {margin-left: 1000px;}
    100% {margin-left: -1000px;}
}
@-moz-keyframes moveclouds {
    0% {margin-left: 1000px;}
    100% {margin-left: -1000px;}
}
@-o-keyframes moveclouds {
    0% {margin-left: 1000px;}
    100% {margin-left: -1000px;}
}
<div class="container">
    <div id="clouds">
        <div class="cloud x1"></div>
        <div class="cloud x2"></div>
        <div class="cloud x3"></div>
        <div class="cloud x4"></div>
        <div class="cloud x5"></div>
    </div>
    <div id="character"></div>
    <div id="flooring"></div>
</div>

Upvotes: 2

Views: 1053

Answers (4)

Kokovin Vladislav
Kokovin Vladislav

Reputation: 10391

add overflow: hidden; to id "clouds"

   #clouds{
      overflow: hidden;
    }

https://jsfiddle.net/p7noxpx1/1/

example with flooring https://jsfiddle.net/ggb62cux/4/

Upvotes: 3

Hkidd
Hkidd

Reputation: 912

Solution

I think you should add an overflow: hidden; to .container and #clouds because every element is contained in .container. The clouds are positioned absolute, so they may overflow inside .container thus overflow: hidden; is applied on #clouds. Also change the margin of the #character if you want it to be inside the container.

CSS

 .container {           
  margin: 0 auto;
  overflow: hidden;
  text-align: center;
  width: 50%;
}

#clouds {           
  background: #3598dc;
  background: -webkit-linear-gradient(top, #3598dc 0%, #fff 100%);
  background: -linear-gradient(top, #3598dc 0%, #fff 100%);
  background: -moz-linear-gradient(top, #3598dc 0%, #fff 100%);
  overflow: hidden;
  padding: 100px 0;      
}

#character{
  animation: walk 1.0s steps(6) infinite;
  background:url(http://i.imgur.com/Zjsbcni.png);      
  height: 126px;  
  margin-left: 320px;
  margin-top: -120px;
  width: 84px;
}

Check this fiddle

Upvotes: 1

Pugazh
Pugazh

Reputation: 9561

Updated fiddle

Fix 1: Clouds overflowing the container:

overflow: hidden on the body will only hide the elements overflowing the body of page.So you would need to add overflow: hidden to the div #clouds.

Modify #clouds as below:

#clouds {
  padding: 100px 0;
  background: #3598dc;
  background: -webkit-linear-gradient(top, #3598dc 0%, #fff 100%);
  background: -linear-gradient(top, #3598dc 0%, #fff 100%);
  background: -moz-linear-gradient(top, #3598dc 0%, #fff 100%);
overflow: hidden;
}

Fix 2: Floor animation is hidden Floor animation is hidden because it's overflowing the body. Change overlfow: hidden in body to overflow-x: hidden

Modify body as below:

body {
  font-family: 'Candal', sans-serif;
  overflow-x: hidden;
}

Upvotes: 1

Randy
Randy

Reputation: 9809

Add a overflow:hidden to #clouds:

body {
    font-family: 'Candal', sans-serif;
    overflow: hidden;
    background-color:black;

    /* remove this line, this is to prove the cloud is invisible ^^^ */
}

.container {
    width: 50%;
    margin: 0 auto;
    text-align: center;
}

#clouds{
    padding: 100px 0;
    background: #3598dc;
    background: -webkit-linear-gradient(top, #3598dc 0%, #fff 100%);
    background: -linear-gradient(top, #3598dc 0%, #fff 100%);
    background: -moz-linear-gradient(top, #3598dc 0%, #fff 100%);
    overflow:hidden;

    /* add this line ^^^ */
}

.cloud {
    width: 200px; height: 60px;
    background: #fff;
    border-radius: 200px;
    -moz-border-radius: 200px;
    -webkit-border-radius: 200px;

    position: relative;
}

.cloud:before, .cloud:after {
    content: '';
    position: absolute;
    background: #fff;
    width: 100px; height: 80px;
    position: absolute; top: -15px; left: 10px;

    border-radius: 100px;
    -moz-border-radius: 100px;
    -webkit-border-radius: 100px;

    -webkit-transform: rotate(30deg);
    transform: rotate(30deg);
    -moz-transform: rotate(30deg);
}

.cloud:after {
    width: 120px; height: 120px;
    top: -55px; left: auto; right: 15px;
}

#character{
    margin-top: -120px;
    background:url(http://i.imgur.com/Zjsbcni.png);
    width: 84px;
    height: 126px;
    animation: walk 1.0s steps(6) infinite;
    margin-left: 425px;
}

#flooring {
    height: 39px;
    background-image: url(http://i.imgur.com/usFekrp.png);
    animation: movefloor 20s linear infinite;
    overflow: hidden;
}

@keyframes movefloor {
    0% {margin-left: 1000px;}
    100% {margin-left: -1000px;}
}

@keyframes walk {
    from { background-position: 0px; }
    to { background-position: -500px; }
}

.x1 {
    -webkit-animation: moveclouds 15s linear infinite;
    -moz-animation: moveclouds 15s linear infinite;
    -o-animation: moveclouds 15s linear infinite;
}

/*variable speed, opacity, and position of clouds for realistic effect*/
.x2 {
    left: 200px;

    -webkit-transform: scale(0.6);
    -moz-transform: scale(0.6);
    transform: scale(0.6);
    opacity: 0.6; /*opacity proportional to the size*/

    /*Speed will also be proportional to the size and opacity*/
    /*More the speed. Less the time in 's' = seconds*/
    -webkit-animation: moveclouds 25s linear infinite;
    -moz-animation: moveclouds 25s linear infinite;
    -o-animation: moveclouds 25s linear infinite;
}

.x3 {
    left: 252px;
    top: -200px;
    -webkit-transform: scale(0.8);
    -moz-transform: scale(0.8);
    transform: scale(0.8);
    opacity: 0.8; /*opacity proportional to the size*/

    -webkit-animation: moveclouds 20s linear infinite;
    -moz-animation: moveclouds 20s linear infinite;
    -o-animation: moveclouds 20s linear infinite;
}

.x4 {
    left: 470px;
    top: -250px;

    -webkit-transform: scale(0.75);
    -moz-transform: scale(0.75);
    transform: scale(0.75);
    opacity: 0.75; /*opacity proportional to the size*/

    -webkit-animation: moveclouds 18s linear infinite;
    -moz-animation: moveclouds 18s linear infinite;
    -o-animation: moveclouds 18s linear infinite;
}

.x5 {
    left: -150px;
    top: -150px;

    -webkit-transform: scale(0.8);
    -moz-transform: scale(0.8);
    transform: scale(0.8);
    opacity: 0.8; /*opacity proportional to the size*/

    -webkit-animation: moveclouds 20s linear infinite;
    -moz-animation: moveclouds 20s linear infinite;
    -o-animation: moveclouds 20s linear infinite;
}

@-webkit-keyframes moveclouds {
    0% {margin-left: 1000px;}
    100% {margin-left: -1000px;}
}
@-moz-keyframes moveclouds {
    0% {margin-left: 1000px;}
    100% {margin-left: -1000px;}
}
@-o-keyframes moveclouds {
    0% {margin-left: 1000px;}
    100% {margin-left: -1000px;}
}
<div class="container">
  <h1>CSS3 Animation</h1>
  <div id="clouds">
		<div class="cloud x1"></div>
		<div class="cloud x2"></div>
		<div class="cloud x3"></div>
		<div class="cloud x4"></div>
		<div class="cloud x5"></div>
	</div>
  <div id="character"></div>
  <div id="flooring"></div>
</div>

Upvotes: 2

Related Questions