用CSS实现一个简单的幻灯片效果页面

又是一个常见面试题,不搜不知道,一搜吓一跳,有很多很有意思的写法。

第一反应是利用CSS3的animation。不过为了兼容浏览器,记得加各浏览器前缀(“chrome和safira:-webkit-”、“firefox:-moz-”、“opera:-o-”)
下面说说用到的animation各属性。
animation-name(动画名字,需用引号包裹)
animation-duration(动画持续时间,如:20s)
animaiton-iteration-count(循环次数,“infinite”为无线循环)
还有一个很重要的“keyframes(关键帧)”,书写格式为:@keyframes “动画名字”{}
“{}”内根据设置内容从“0%”到“100%”依次编写,注意“0%”一定不能把百分号省略


<html>
<head>
    <title>title>
    <style type="text/css">
        .myDiv {
            width:300px;
            height: 300px;
            margin: 20px auto;
            -webkit-animation-name: 'loop';
            -webkit-animation-duration: 10s;
            -webkit-animation-iteration-count: infinite;
        }
        @-webkit-keyframes "loop"{
            0% { background: url("images/m15.jpg") no-repeat;}
            25% { background: url("images/m10.jpg") no-repeat;}
            50% { background: url("images/m11.jpg") no-repeat;}
            75% { background: url("images/m12.jpg") no-repeat;}
            100% {background:  url("images/m15.jpg") no-repeat;}
        }
    style>
head>
<body>
<div class="myDiv">div>
body>
html>

下面这个写法也很有意思。这个是改变背景的透明度


<head>
    <meta charset="utf-8" />
    <title>css3实现幻灯片效果title>
    <meta content="" name="description" />
    <meta content="" name="author" />
    <style type="text/css">
        .cb-slideshow{
            width:400px;
            height:400px;
            margin:0 auto;
            z-index:0;
        }
        .cb-slideshow li{
            position:absolute;
            width:400px;
            height:400px;
            background-size:cover;
            background-repeat: none;
            opacity:0;
            z-index:0;
            -webkit-animation: loops 36s linear infinite 0s;
        }
        .cb-slideshow li:nth-child(1){
            background-image: url(images/m10.jpg);
        }
        .cb-slideshow li:nth-child(2){
            background-image: url(images/m11.jpg);
            -webkit-animation-delay: 6s;
        }
        .cb-slideshow li:nth-child(3){
            background-image: url(images/m12.jpg);
            -webkit-animation-delay: 12s;
        }
        .cb-slideshow li:nth-child(4){
            background-image: url(images/m15.jpg);
            -webkit-animation-delay: 18s;
        }
        .cb-slideshow li:nth-child(5){
            background-image: url(images/m16.jpg);
            -webkit-animation-delay: 24s;
        }
        .cb-slideshow li:nth-child(6){
            background-image: url(images/m17.jpg);
            -webkit-animation-delay: 30s;
        }
        @-webkit-keyframes "loops" {
            0% {
                opacity: 0;

            }
            8% {
                opacity:1;
            }
            17% {
                opacity:1;
            }
            25% {
                opacity:0.5;
            }
            100% {
                opacity: 0;
            }
        }


    style>
head>
<body>
<ul class="cb-slideshow">
    <li>li>
    <li>li>
    <li>li>
    <li>li>
    <li>li>
    <li>li>
ul>
body>

还有两种写法,利用了label和a的特性。不完全算是幻灯片,先贴上来看看.


<html>
  <head>
    <style>
    img {
      display: none;
      width: 100px;
      height: 100px;
    }

    input:checked + img {
      display: block;
    }

    input {
      position: absolute;
      left: -9999px;
    }

    label {
      cursor: pointer;
    }
    style>
  head>
  <body>
    <div id="cont">
      <input id="img1" name="img" type="radio" checked="checked">
      <img src="a.png">
      <input id="img2" name="img" type="radio">
      <img src="b.png">
    div>
    <div id="nav">
      <label for="img1">第一张label>
      <label for="img2">第二张label>
    div>
  body>
html>

<html>
  <head>
  <style>

  #cont {
  position: relative;
  height: 100px;
  }
img {
      position: absolute;
      width: 100px;
      height: 100px;
      z-index: 1;
    }
img:first-child,
img:target {
      z-index: 2;
    }


  style>
  head>
  <body>
  <div id="cont">
      <img id="img1" src="a.jpg">
      <img id="img2" src="b.jpg">
    div>
    <div>
      <a href="#img1">onea>
      <a href="#img2">twoa>
    div>
  body>
html>

你可能感兴趣的:(css)