Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

html - How can i make infinite flowing background with only CSS?

I'm just started to web programming, cuz many cooooool pages on awwwards.com - definitely caught my mind.

anyway, the first page what i aim for make is the pinterest (www.pinterest.com); slowly moving background with blur effect, floating modal and bottom fixed footer.

with some kinda O'Reilly books, the blur, modal and footer are no more problem. but i couldn't made the background even with them yet.

so, how can i make horizontally infinite flowing background with only CSS??? (without JS)

*conditions

  1. the page is responsive, background-image's height should fit to screen

  2. width follow the height's size, as original image's ratio.

and here's my code.

  <head>
    <style type="text/css">
    * {
    margin: 0;
    padding: 0;
    }
    html {
    height: 100%;
    }
    body {
    overflow: hidden;
    }
    #animatedBackground {
    position: absolute;
    height: 100%;
    width: 100%;
    background: url("http://placehold.it/1600x800");
    background-repeat: repeat;
    background-position: 0 0;
    background-size: auto 100%;

border: 1px solid red;

    animation: animatedBackground 5s linear infinite;
    }
    @keyframes animatedBackground {
    from {
    left: -50%;
    }
    to {
    left: 50%;
    }
    }
    </style>
    </head>
    
    <body>
    <div id="animatedBackground">animatedBackground</div>
    </body>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should fit your slowly moving+infinite flowing+responsively fit to height background criteria.

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#animatedBackground {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background: url("http://twibbon.s3.amazonaws.com/238/63bb30c8-2649-465e-9df1-ab2f8e5f7ecc.jpg");
  background-repeat: repeat;
  background-position: 0 0;
  background-size: auto 100%;
/*adjust s value for speed*/
  animation: animatedBackground 500s linear infinite;
}

@keyframes animatedBackground {
  from {
    background-position: 0 0;
  }
/*use negative width if you want it to flow right to left else and positive for left to right*/
  to {
    background-position: -10000px 0;
  }
}
<div id="animatedBackground">
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...