-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
224 lines (221 loc) · 6.47 KB
/
test.html
File metadata and controls
224 lines (221 loc) · 6.47 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<html>
<head>
<style type="text/css">
body {
font-size: 12px;
font-family: sans-serif
}
#pic1
{
width:250px;
height: auto;
}
</style>
<script src="jquery.js"></script>
<script>
// Function to execute a callback when an image has been loaded,
// either from the network or from the browser cache.
var loadImage = function ($image, src, callback) {
// Bind the load event BEFORE setting the src.
$image.bind("load", function (evt) {
// Image has loaded, so unbind event and call callback.
$image.unbind("load");
callback($image);
}).each(function () {
// For Gecko-based browsers, check the complete property,
// and trigger the event manually if image loaded.
if ($image[0].complete) {
$image.trigger("load");
}
});
// For Webkit browsers, the following line ensures load event fires if
// image src is the same as last image src. This is done by setting
// the src to an empty string initially.
if ($.browser.webkit) {
$image.attr('src', '');
}
$image.attr('src', src);
};
// Create a single carousel item.
var createItem = function ($image, angle, options) {
var loaded = false, // Flag to indicate image has loaded.
orgWidth, // Original, unscaled width of image.
orgHeight, // Original, unscaled height of image.
$originDiv, // Image is attached to this div.
// A range used in the scale calculation to ensure
// the frontmost item has a scale of 1,
// and the farthest item has a scale as defined
// in options.minScale.
sizeRange = (1 - options.minScale) * 0.5,
// An object to store the public update function.
that;
// Make image invisible and
// set its positioning to absolute.
$image.css({
opacity: 0,
position: 'absolute'
});
// Create a div element ($originDiv). The image
// will be attached to it.
$originDiv = $image.wrap('<div style="position:absolute;">').parent();
that = {
update: function (ang) {
var sinVal, scale, x, y;
// Rotate the item.
ang += angle;
// Calculate scale.
sinVal = Math.sin(ang);
scale = ((sinVal + 1) * sizeRange) + options.minScale;
// Calculate position and zIndex of origin div.
x = ((Math.cos(ang) * options.radiusX) * scale) + options.width / 2;
y = ((sinVal * options.radiusY) * scale) + options.height / 2;
$originDiv.css({
left: (x >> 0) + 'px',
top: (y >> 0) + 'px',
zIndex: (scale * 100) >> 0
});
// If image has loaded, update its dimensions according to
// the calculated scale.
// Position it relative to the origin div, so the
// origin div is in the center.
if (loaded) {
$image.css({
width: (orgWidth * scale) + 'px',
height: (orgHeight * scale) + 'px',
top: ((-orgHeight * scale) / 2) + 'px',
left: ((-orgWidth * scale) / 2) + 'px'
});
}
}
};
loadImage($image, $image.attr('src'), function ($image) {
loaded = true;
// Save the image width and height for the scaling calculations.
orgWidth = $image.width();
orgHeight = $image.height();
// Make the item fade-in.
$image.animate({
opacity: 1
}, 1000);
});
return that;
};
// Create a carousel.
var createCarousel = function ($wrap, options) {
var items = [],
rot = 0,
pause = false,
unpauseTimeout = 0,
// Now calculate the amount to rotate per frameRate tick.
rotAmount = ( Math.PI * 2) * (options.frameRate/options.rotRate),
$images = $('img', $wrap),
// Calculate the angular spacing between items.
spacing = (Math.PI / $images.length) * 2,
// This is the angle of the first item at
// the front of the carousel.
angle = Math.PI / 2,
i;
// Create a function that is called when the mouse moves over
// or out of an item.
$wrap.bind('mouseover mouseout', function (evt) {
// Has the event been triggered on an image? Return if not.
if (!$(evt.target).is('img')) {
return;
}
// If mouseover, then pause the carousel.
if (evt.type === 'mouseover') {
// Stop the unpause timeout if it's running.
clearTimeout(unpauseTimeout);
// Indicate carousel is paused.
pause = true;
} else {
// If mouseout, restart carousel, but after a small
// delay to avoid jerking movements as the mouse moves
// between items.
unpauseTimeout = setTimeout(function () {
pause = false;
}, 200);
}
});
// This loop runs through the list of images and creates
// a carousel item for each one.
for (i = 0; i < $images.length; i++) {
var image = $images[i];
var item = createItem($(image), angle, options);
items.push(item);
angle += spacing;
}
// The setInterval will rotate all items in the carousel
// every 30ms, unless the carousel is paused.
setInterval(function () {
if (!pause) {
rot += rotAmount;
}
for (i = 0; i < items.length; i++) {
items[i].update(rot);
}
}, options.frameRate);
};
// This is the jQuery plug-in part. It iterates through
// the list of DOM elements that wrap groups of images.
// These groups of images are turned into carousels.
(function ($) {
$.fn.Carousel = function(options) {
this.each( function() {
// User options are merged with default options.
options = $.extend({}, $.fn.Carousel.defaults, options);
// Each wrapping element is given relative positioning
// (so the absolute positioning of the carousel items works),
// and the width and height are set as specified in the options.
$(this).css({
position:'relative',
width: options.width+'px',
height: options.height +'px'
});
createCarousel($(this),options);
});
};
// These are the default options.
$.fn.Carousel.defaults = {
radiusX:230, // Horizontal radius.
radiusY:80, // Vertical radius.
width:512, // Width of wrapping element.
height:300, // Height of wrapping element.
frameRate: 30, // Frame rate in milliseconds.
rotRate: 5000, // Time it takes for carousel to make one complete rotation.
minScale:0.60 // This is the smallest scale applied to the farthest item.
};})(jQuery);
$(function(){
// Create a carousel on all wrapping elements
// with a class of .carousel.
$('.carousel').Carousel({
width:511, height:600, // Set wrapping element size.
radiusX:220,radiusY:70, // Set carousel radii.
minScale:0.6 // Set min scale of rearmost item.
});
// Bind a click event to one of the pictures (Mona Lisa)
// to show events are preserved after images become
// carousel items.
$('#pic2').bind('click', function() {
alert('Pic 2 clicked!');
});
});
</script>
</head>
<body>
<div class="carousel" ><!-- This is the wrapping element -->
<a href="http://en.wikipedia.org/wiki/Self-portrait_(Leonardo_da_Vinci)"
target="_blank">
<img src="hill.png" alt="Pic 1"/>
</a>
<img id="pic2" src="cloud.png" alt="Pic 2"/>
<img src="grass.png" alt="Pic 3"/>
<img src="cloud.png" alt="Pic 4"/>
<img src="grass.png" alt="Pic 5"/>
<img src="cloud.png" alt="Pic 6"/>
<img src="cloud.png" alt="Pic 7"/>
<img src="grass.png" alt="Pic 8"/>
<img src="cloud.png" alt="Pic 9"/>
</div>
</body>
</html>