Bitte warten...

JavaScript: Diashow

Hier ein Beispiel einer einfachen Diashow mit verschiedenen Übergangseffekten:

Und hier der dazugehörige Quelltext:

Code auswählen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="de">
  <head>
    <title>Diashow</title>
    <meta charset="UTF-8">
    <style>
      #slides { border:2px solid white; position:relative; overflow:hidden; }
      #slides img { position:absolute; top:0; left:0; transition-property:opacity,left,top,width,height,transform; transition-timing-function:linear; transform:rotate(0deg); }
    </style>
  </head>
  <body>
    <div id="slides">
      <img id="img1" style="z-index:0;" alt="">
      <img id="img2" style="z-index:1;" alt="">
    </div>
    <script>
      data = ["img/show1.jpg", "img/show2.jpg", "img/show3.jpg", "img/show4.jpg", "img/show5.jpg"];
      w = 300; h = 400;  // image width and height
      s = 3000;  // speed
      d = 1000;  // transition duration in ms
      
      SLIDES = document.getElementById("slides");
      SLIDES.style.width  = w + "px";
      SLIDES.style.height = h + "px";
      I1 = document.getElementById("img1");
      I2 = document.getElementById("img2");
      I1.style.width  = w + "px";
      I1.style.height = h + "px";
      I2.style.width  = w + "px";
      I2.style.height = h + "px";
      I1.style.transitionDuration  = d + "ms";
      I2.style.transitionDuration  = d + "ms";
      n = 0;
      I2.src = data[n];
      function slideShow() {
        if (I1.style.zIndex == 0) { L1 = I1; L2 = I2; } else { L1 = I2; L2 = I1; }
        S1 = L1.style;
        S2 = L2.style;
        // load next image
        n++; if (n == data.length) n = 0;
        L1.src = data[n];
        // apply transition effect
        switch (Math.floor(Math.random() * 9)) {
        //switch (8) {
          case 0:  // blend into
            S2.opacity = 0;
            break;
          case 1:  // wipe right
            S2.left = h + "px";
            break;
          case 2:  // wipe left
            S2.left = -h + "px";
            break;
          case 3:  // wipe down
            S2.top = h + "px";
            break;
          case 4:  // wipe up
            S2.top = -h + "px";
            break;
          case 5:  // collapse horizontal
            S2.left = (w / 2) + "px";
            S2.width = "0px";
            break;
          case 6:  // collapse vertical
            S2.top = (h / 2) + "px";
            S2.height = "0px";
            break;
          case 7:  // zoom out
            S2.left = (w / 2) + "px";
            S2.top = (h / 2) + "px";
            S2.width = "0px";
            S2.height = "0px";
            break;
          case 8:  // rotate out
            S2.transform = "rotate(90deg)";
            S2.transformOrigin = "bottom right";
            break;
        }
        setTimeout(function(){
          // flip layers
          S1.zIndex = 1;
          S2.zIndex = 0;
          // reset styles
          S2.opacity = 1;
          S2.left = S2.top = 0;
          S2.width  = w + "px";
          S2.height = h + "px";
          S2.transform = "rotate(0deg)";
          S2.transformOrigin = "";
        }, d);
      }
      setInterval(slideShow, s);
    </script>
  </body>
</html>