使用CSS3

时间:2017-05-04 17:53:11

标签: html css css3 animation position

所以我知道那里有很多横向幻灯片,但我已经尝试过跟随它们并且每次都出现问题。每次它真的很复杂或者对我来说根本不起作用。所以我尝试创建自己的(对于凌乱的代码抱歉):



.wrapper {
	width: 100%;
	height: 35vw;
	margin: auto;
	background: #fff url("https://s-media-cache-ak0.pinimg.com/originals/a4/f2/cb/a4f2cb80ff2ae2772e80bf30e9d78d4c.gif") no-repeat center;
	background-size: 200px 200px;
}

.slides {
	position: absolute;
	height: 35vw;
	width: 100%;
}

#slide1{
	background: transparent url("https://www.planwallpaper.com/static/images/hd-wallpaper-1080p-widescreen-1080p-backgrounds.jpg") no-repeat center;
	background-size: cover;
	animation: left1 15s infinite;
}

#slide2 {
	background: transparent url("https://www.planwallpaper.com/static/images/hd-beach-wallpapers-1080p.jpg") no-repeat center;
	background-size: cover;
	animation: left2 15s infinite;
	left: 100%;
	
}
#slide3 {
	background: transparent url("https://www.planwallpaper.com/static/images/01819_birdonabranch_1920x1080.jpg") no-repeat center;
	background-size: cover;
	animation: left3 15s infinite;
	left: 200%;
}

@keyframes left1 {
0% {left: 0%; right:0%;}
26.8% {left: 0%; right:0%;}
33.5% {left: -100%; right: 100%;}
93.8% {left: -200%; right: 200%;}
100% {left: 0%; right: 0%;}
}

@keyframes left2 {
0% {left: 100%; right:-100%;}
26.8% {left: 100%; right:-100%;}
33.5% {left: 0%; right: 0%;}
60.3% {left: 0%; right: 0%;}
67% {left: -100%; right: 100%;}
93.8% {left: -100%; right: 100%;}
100% {left: 100%; right: -100%;}
}

@keyframes left3 {
0% {left: 200%; right:-200%;}
26.8% {left: 200%; right:-200%;}
33.5% {left: 100%; right: -100%;}
60.3% {left: 100%; right: -100%;}
67% {left: 0%; right: 0%;}
93.8% {left: 0%; right: 0%;}
100% {left: 200%; right: -200%;}
}

<body>

<div class="wrapper">
	<div class="slides" id="slide1"></div>
	<div class="slides" id="slide2"></div>
	<div class="slides" id="slide3"></div>
</div>
</body>
&#13;
&#13;
&#13;

起初,我以为我没事。然后我注意到你能够水平滚动。我尝试通过使用溢出来修复它:以不同的方式隐藏并与display:和position:组合。什么都行不通。要么图像没有正确定位,要么动画不能正常工作。我从没想过离页的定位元素会如此荒谬。
附:我没有学过任何网页设计,我自学成才,并试图创建一个像样的网站。

2 个答案:

答案 0 :(得分:1)

position:relativeoverflow:hidden添加到.wrapper似乎可以解决问题。

.wrapper {
	width: 100%;
	height: 35vw;
	margin: auto;
	background: #fff url("https://s-media-cache-ak0.pinimg.com/originals/a4/f2/cb/a4f2cb80ff2ae2772e80bf30e9d78d4c.gif") no-repeat center;
	background-size: 200px 200px;
        position: relative;
        overflow: hidden;
}

.slides {
	position: absolute;
	height: 35vw;
	width: 100%;
}

#slide1{
	background: transparent url("https://www.planwallpaper.com/static/images/hd-wallpaper-1080p-widescreen-1080p-backgrounds.jpg") no-repeat center;
	background-size: cover;
	animation: left1 15s infinite;
}

#slide2 {
	background: transparent url("https://www.planwallpaper.com/static/images/hd-beach-wallpapers-1080p.jpg") no-repeat center;
	background-size: cover;
	animation: left2 15s infinite;
	left: 100%;
	
}
#slide3 {
	background: transparent url("https://www.planwallpaper.com/static/images/01819_birdonabranch_1920x1080.jpg") no-repeat center;
	background-size: cover;
	animation: left3 15s infinite;
	left: 200%;
}

@keyframes left1 {
0% {left: 0%; right:0%;}
26.8% {left: 0%; right:0%;}
33.5% {left: -100%; right: 100%;}
93.8% {left: -200%; right: 200%;}
100% {left: 0%; right: 0%;}
}

@keyframes left2 {
0% {left: 100%; right:-100%;}
26.8% {left: 100%; right:-100%;}
33.5% {left: 0%; right: 0%;}
60.3% {left: 0%; right: 0%;}
67% {left: -100%; right: 100%;}
93.8% {left: -100%; right: 100%;}
100% {left: 100%; right: -100%;}
}

@keyframes left3 {
0% {left: 200%; right:-200%;}
26.8% {left: 200%; right:-200%;}
33.5% {left: 100%; right: -100%;}
60.3% {left: 100%; right: -100%;}
67% {left: 0%; right: 0%;}
93.8% {left: 0%; right: 0%;}
100% {left: 200%; right: -200%;}
}
<body>

<div class="wrapper">
	<div class="slides" id="slide1"></div>
	<div class="slides" id="slide2"></div>
	<div class="slides" id="slide3"></div>
</div>
</body>

答案 1 :(得分:0)

您应该使用Bootstrap's Carousel而不是自定义逻辑。您可以任意方式自定义它。

这是一个有效的演示:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  .carousel-inner > .item > img,
  .carousel-inner > .item > a > img {
      width: 100%;
      margin: auto;
  }
  </style>
</head>
<body>

<div class="container">
  <div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="2000">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <img src="https://www.planwallpaper.com/static/images/hd-wallpaper-1080p-widescreen-1080p-backgrounds.jpg" alt="slide 1" width="460" height="345">
      </div>

      <div class="item">
        <img src="https://www.planwallpaper.com/static/images/hd-beach-wallpapers-1080p.jpg" alt="slide 2" width="460" height="345">
      </div>
    
      <div class="item">
        <img src="https://www.planwallpaper.com/static/images/01819_birdonabranch_1920x1080.jpg" alt="slide 3" width="460" height="345">
      </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

</body>
</html>