-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcfimg2.php
86 lines (83 loc) · 2.6 KB
/
cfimg2.php
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
<h2 id="cfimg2">Demo 2 - One image to another, when a button is pressed (transitions)</h2>
<h3>Plan</h3>
<p>Same as before, but instead of using the :hover pseudo class, we are going to use javascript to add a toggle a class. I'm using jQuery here because it's easy to understand, though you could just use plain old JS.</p>
<h3>Demo</h3>
<style>
#cf2 {
position:relative;
height:281px;
width:450px;
margin:0 auto;
}
#cf2 img {
position:absolute;
left:0;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#cf2 img.transparent {
opacity:0;
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: alpha(opacity=0);
}
#cf_onclick {
cursor:pointer;
}
</style>
<script>
$(document).ready(function() {
$("#cf_onclick, #cf2 img.top").click(function() {
$("#cf2 img.top").toggleClass("transparent");
});
});
</script>
<div id="cf2" class="shadow">
<img class="bottom" src="/images/Stones.jpg" />
<img class="top" src="/images/Summit.jpg" />
</div>
<p class="center" id="cf_onclick">Click me to toggle</p>
<h3>Code</h3>
<p>First up, the HTML markup. Again, with no CSS enabled, you just get two images.</p>
<pre class="prettyprint lang-html">
<div id="cf2" class="shadow">
<img class="bottom" src="/tests/images/Stones.jpg" />
<img class="top" src="/tests/images/Summit.jpg" />
</div>
<p id="cf_onclick">Click me to toggle</p>
</pre>
<p>Then the CSS. I've added a class with the opacity value.</p>
<pre class="prettyprint lang-css">
#cf2 {
position:relative;
height:281px;
width:450px;
margin:0 auto;
}
#cf2 img {
position:absolute;
left:0;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#cf2 img.transparent {
opacity:0;
}
#cf_onclick {
cursor:pointer;
}
</pre>
<p>Then the extremely short JS. Note that the browser is smart enough to realise that it can animate to the new properties, I didn't have to set them in javascript (thought that works too).</p>
<pre class="prettyprint lang-js">
$(document).ready(function() {
$("#cf_onclick").click(function() {
$("#cf2 img.top").toggleClass("transparent");
});
});
</pre>
<p>Have a look at <a href="/demos/multiple_images.php">the multiple image demo</a> to see how to extend this idea to more than two images.</p>