4

I'm trying to use this code I found online to make a moving image banner, but my problem is that I can't seem to get it to scroll vertically. Right now, only the first picture is moving, and I'm not sure why. Here is the link to the original code.

Here's what I have so far.

CSS:

<style>
  body {
    background: url('images/dark_geometric.png');
  }
  #container {
    width: 800px;
    height: 705px; 
    overflow: hidden;
  }
  .photobanner {
    height: 233px;
    width: 3550px;
    margin-bottom: 80px;
  }
  /*keyframe animations*/
  .first {
    -webkit-animation: bannermove 30s linear infinite;
    -moz-animation: bannermove 30s linear infinite;
    -ms-animation: bannermove 30s linear infinite;
    -o-animation: bannermove 30s linear infinite;
    animation: bannermove 30s linear infinite;
  }
  @keyframes "bannermove" {
    0% {
      margin-top: 0px;
    }
    100% {
      margin-top: -2125px;
    }
  }
  @-moz-keyframes bannermove {
    0% {
      margin-top: 0px;
    }
    100% {
      margin-top: -2125px;
    }
  }
  @-webkit-keyframes "bannermove" {
    0% {
      margin-top: 0px;
    }
    100% {
      margin-top: -2125px;
    }
  }
  @-ms-keyframes "bannermove" {
    0% {
      margin-top: 0px;
    }
    100% {
      margin-top: -2125px;
    }
  }
  @-o-keyframes "bannermove" {
    0% {
      margin-top: 0px;
    }
    100% {
      margin-top: -2125px;
    }
  }
</style>

HTML:

<body>
  <div id="container">
    <div class="photobanner">
      <img class="first" src="images/rsz_event1.png" alt="">
      <img src="images/rsz_event2.png" alt="">
      <img src="images/rsz_event3.png" alt="">
      <img src="images/rsz_event4.png" alt="">
      <img src="images/rsz_event5.png" alt="">
      <img src="images/rsz_event6.png" alt="">
      <img src="images/rsz_event7.png" alt="">
      <img src="images/rsz_event8.png" alt="">
      <img src="images/rsz_event1.png" alt="">
      <img src="images/rsz_event2.png" alt="">
      <img src="images/rsz_event3.png" alt="">
      <img src="images/rsz_event4.png" alt="">
    </div>
  </div>
</body>

If someone could help point me in the right direction, I would really appreciate it. Just started learning.

Harry
  • 87,580
  • 25
  • 202
  • 214
Thomas Phan
  • 249
  • 2
  • 17

1 Answers1

3

Images are not block level elements and so if you need to get the images to display one below another and animate it using margin-top property then you should add display: block setting to it.

The linked example doesn't need this because that is a horizontal scroll and all these image elements by default get lined up one next to the other.

body {
  background: url('http://lorempixel.com/600/400/abstract/1');
}
#container {
  width: 800px;
  height: 600px; /* modify as per your need, would be better to set it to multiples of image height */
  overflow: hidden;
}
.photobanner {
  width: 3550px;
  height: 200px; /* modify as per your need, this is the height of each image */
  margin-bottom: 80px;
}
.first {
  animation: bannermove 30s linear infinite;
}
img {
  display: block;
}
@keyframes bannermove {
  0% {
    margin-top: 0px;
  }
  100% {
    margin-top: -1600px; /* container height - (no. of images - 1) * image height */
  }
}
<div id="container">
  <div class="photobanner">
    <img class="first" src="http://lorempixel.com/800/200/nature/1" alt="">
    <img src="http://lorempixel.com/800/200/nature/2" alt="">
    <img src="http://lorempixel.com/800/200/nature/3" alt="">
    <img src="http://lorempixel.com/800/200/nature/4" alt="">
    <img src="http://lorempixel.com/800/200/nature/5" alt="">
    <img src="http://lorempixel.com/800/200/nature/6" alt="">
    <img src="http://lorempixel.com/800/200/nature/7" alt="">
    <img src="http://lorempixel.com/800/200/nature/8" alt="">
    <img src="http://lorempixel.com/800/200/nature/1" alt="">
    <img src="http://lorempixel.com/800/200/nature/2" alt="">
    <img src="http://lorempixel.com/800/200/nature/3" alt="">
    <img src="http://lorempixel.com/800/200/nature/4" alt="">
  </div>
</div>
Harry
  • 87,580
  • 25
  • 202
  • 214
  • Ohh wow I did not know that you needed to explicitly say display: block. I thought you could just arrange them in a vertical alignment. Thanks a lot! – Thomas Phan Nov 15 '15 at 14:56