Skip to content

Commit acf62c9

Browse files
committed
initAnimation & bug fixes
1 parent 139364c commit acf62c9

6 files changed

+94
-58
lines changed

README.markdown

+37
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,43 @@
1010

1111
(Only the most recent changes are shown below, see the [wiki page](https://github.com/chriscoyier/MovingBoxes/wiki/Change-Log) for a complete listing)
1212

13+
###Version 2.2.3 (1/7/2012)
14+
15+
* Added `initAnimation` option:
16+
* When `true` (default), MovingBoxes will show the initial animation starting from the first panel and sliding into the current panel (as determined by the hash or `startPanel` option).
17+
* If `false`, no animation will be seen and MovingBoxes will start on the appropriate panel.
18+
* The update method now has a flag to prevent callbacks from firing and also has it's own callback:
19+
* Set the flag to `false` to prevent the built-in callbacks (initChange, beforeAnimation & completed) from firing during the update. This flag is useful if you plan to call the update method a lot, like when the window resizes.
20+
* The callback for the update is used as follows:
21+
22+
```javascript
23+
// update(flag, callback);
24+
$('#slider').data('movingBoxes').update(false, function(slider){
25+
alert('update complete');
26+
});
27+
```
28+
29+
* Fixed clicking on links in the current panel would go to the next panel. Fix for [issue #60](https://github.com/chriscoyier/MovingBoxes/issues/60).
30+
* Updated method of plugin initialization to hopefully ensure that the `completed` callback will not fire until after initialization. Update for [issue #57](https://github.com/chriscoyier/MovingBoxes/issues/57).
31+
* Fixed a problem where the navigation was clearing the current panel after using the update method.
32+
* Hopefully fixed the problems brought up in [issue #49](https://github.com/chriscoyier/MovingBoxes/issues/49). So using this bit of code will allow you to set the MovingBoxes width as a **percentage value**.
33+
34+
```javascript
35+
$(function(){
36+
$(window).resize(function(){
37+
// get MovingBoxes plugin object
38+
var slider = $('.slider').data('movingBoxes');
39+
// set overall width to 50% of the browser width
40+
slider.options.width = $(window).width() * 0.5;
41+
// set panel Width to be 50% of MovingBoxes width (which ends up being 25% of browser width; 50% x 50%)
42+
// OR you can set the panelWidth to a px amount, say 300 instead of a fraction: "slider.options.panelWidth = 300"
43+
slider.options.panelWidth = 0.5;
44+
// update the slider; include false flag to prevent built-in callbacks from firing (optional)
45+
slider.update(false);
46+
}).resize(); // trigger window resize to do the initial resizing.
47+
});
48+
```
49+
1350
###Version 2.2.2 (1/3/2012)
1451

1552
* Removed the `width` and `panelWidth` options.

demo/demo.js

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ $(function(){
3232
// Add a slide
3333
var imageNumber = 0,
3434
// Set up demo external navigation links
35-
// could also set len = $('#slider-one').getMovingBoxes().totalPanels;
3635
navLinks = function(){
3736
var i, t = '', len = $('#slider-one').getMovingBoxes().totalPanels + 1;
3837
for ( i = 1; i < len; i++ ) {

index.html

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
<head>
44
<meta charset="utf-8">
55
<title>Moving Boxes</title>
6-
6+
77
<!-- Required CSS -->
88
<link href="css/movingboxes.css" media="screen" rel="stylesheet">
99
<!--[if lt IE 9]>
1010
<link href="css/movingboxes-ie.css" rel="stylesheet" media="screen" />
1111
<![endif]-->
12-
12+
1313
<!-- Required script -->
1414
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
1515
<script src="js/jquery.movingboxes.js"></script>
16-
16+
1717
<!-- Demo only -->
1818
<link href="demo/demo.css" media="screen" rel="stylesheet">
1919
<script src="demo/demo.js"></script>
@@ -24,9 +24,7 @@
2424
div#slider-two { width: 500px; }
2525
div#slider-two > div { width: 360px; }
2626
</style>
27-
<script>
28-
29-
</script>
27+
3028
</head>
3129
<body>
3230

js/jquery.movingboxes.js

+50-48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Moving Boxes v2.2.2
2+
* Moving Boxes v2.2.3
33
* by Chris Coyier
44
* http://css-tricks.com/moving-boxes/
55
*/
@@ -35,15 +35,13 @@
3535

3636
base.initialized = false;
3737
base.currentlyMoving = false;
38-
base.curPanel = 1;
38+
base.curPanel = (o.initAnimation) ? 1 : (o.hashTags) ? base.getHash() || o.startPanel : o.startPanel;
3939

40-
// make backwards compatible - width & panelWidth deprecated in 2.2.2
41-
if (!o.width) { o.width = base.$el.width(); }
42-
if (!o.panelWidth) {
43-
o.panelWidth = base.$panels.eq(0).width();
44-
} else {
45-
o.panelWidth = o.width * o.panelWidth; // change panelWidth (deprecation option) fraction into a px width
46-
}
40+
// save original slider width
41+
base.width = (o.width) ? parseInt(o.width,10) : base.$el.width();
42+
// save panel width, o.panelWidth originally a fraction (0.5 of o.width) if defined, or get first panel width
43+
// now can be set after initialization to resize using fraction (value <= 2) or px (all values > 2)
44+
base.pWidth = (o.panelWidth) ? (o.panelWidth <=2 ? o.panelWidth * base.width : o.panelWidth) : base.$panels.eq(0).width();
4745

4846
// Set up click on left/right arrows
4947
base.$left = base.$wrap.find('.mb-left').click(function(){
@@ -56,12 +54,28 @@
5654
});
5755

5856
// code to run to update MovingBoxes when the number of panels change
59-
base.update();
60-
$(window).load(function(){ base.update(false); }); // animate height after all images load
57+
base.update(false);
58+
$(window).load(function(){ // animate height after all images load
59+
base.update(false);
60+
61+
// Set up "Current" panel
62+
var startPanel = (o.hashTags) ? base.getHash() || o.startPanel : o.startPanel;
63+
// animate to chosen start panel - starting from the first panel makes it look better
64+
setTimeout(function(){
65+
base.change(startPanel, function(){
66+
base.initialized = true;
67+
base.$el.trigger( 'initialized.movingBoxes', [ base, startPanel ] );
68+
}, false);
69+
}, o.speed * 2 );
70+
71+
});
6172

6273
// go to clicked panel
63-
base.$el.delegate('.mb-panel', 'click', function(){
64-
base.change( base.$panels.index($(this)) + base.adj );
74+
base.$el.delegate('.mb-panel', 'click', function(e){
75+
if (!$(this).hasClass(o.currentPanel)) {
76+
e.preventDefault(); // prevent non-current panel links from working
77+
base.change( base.$panels.index($(this)) + base.adj );
78+
}
6579
});
6680

6781
// Activate moving box on click or when an internal link obtains focus
@@ -70,8 +84,10 @@
7084
});
7185
base.$panels.delegate('a', 'focus' ,function(){
7286
// focused link centered in moving box
73-
var loc = base.$panels.index($(this).closest('.mb-panel')) + 1;
74-
if (loc !== base.curPanel){ base.change( base.$panels.index($(this).closest('.mb-panel')) + 1, {}, false ); }
87+
var loc = base.$panels.index($(this).closest('.mb-panel')) + base.adj;
88+
if (loc !== base.curPanel){
89+
base.change( base.$panels.index($(this).closest('.mb-panel')) + base.adj, {}, false );
90+
}
7591
});
7692

7793
// Add keyboard navigation
@@ -92,36 +108,25 @@
92108
}
93109
});
94110

95-
// Set up "Current" panel
96-
var startPanel = (o.hashTags) ? base.getHash() || o.startPanel : o.startPanel;
97-
98111
// Bind Events
99112
$.each('initialized initChange beforeAnimation completed'.split(' '), function(i,evt){
100113
if ($.isFunction(o[evt])){
101114
base.$el.bind(evt + '.movingBoxes', o[evt]);
102115
}
103116
});
104117

105-
// animate to chosen start panel - starting from the first panel makes it look better
106-
setTimeout(function(){
107-
base.change(startPanel, function(){
108-
base.initialized = true;
109-
base.$el.trigger( 'initialized.movingBoxes', [ base, startPanel ] );
110-
}, false);
111-
}, o.speed * 2 );
112-
113118
};
114119

115120
// update the panel, flag is used to prevent events from firing
116-
base.update = function(flag){
117-
var t;
121+
base.update = function(flag, callback){
118122

119123
// Infinite loop
120124
base.$el.children('.cloned').remove();
121125
base.$panels = base.$el.children();
122126
base.adj = (o.wrap && base.$panels.length > 1) ? 0 : 1; // count adjustment for infinite panels
123127

124-
base.$wrap.css('width', o.width); // set wrapper width
128+
base.width = (o.width) ? parseInt(o.width,10) : base.width;
129+
base.$wrap.css('width', base.width); // set wrapper width
125130

126131
if (o.wrap && base.$panels.length > 1) {
127132
base.$el.prepend( base.$panels.filter(':last').clone().addClass('cloned') );
@@ -137,7 +142,7 @@
137142
// defined $panels again to include cloned panels
138143
base.$panels = base.$el.children()
139144
.addClass('mb-panel')
140-
.css({ width : o.panelWidth, margin: 0 })
145+
.css('margin',0)
141146
// inner wrap of each panel
142147
.each(function(){
143148
if ($(this).find('.mb-inside').length === 0) {
@@ -147,9 +152,7 @@
147152
base.totalPanels = base.$panels.filter(':not(.cloned)').length; // don't include cloned panels in total
148153

149154
// save 'cur' numbers (current larger panel size), use stored sizes if they exist
150-
t = base.$panels.eq(base.curPanel - base.adj);
151-
base.curWidth = t.width();
152-
if (!o.panelWidth) { o.panelWidth = base.curWidth; }
155+
base.curWidth = (o.panelWidth) ? (o.panelWidth <=2 ? o.panelWidth * base.width : o.panelWidth) : base.pWidth;
153156

154157
// save 'reg' (reduced size) numbers
155158
base.regWidth = base.curWidth * o.reducedSize;
@@ -170,16 +173,16 @@
170173
base.$el.css({
171174
position : 'absolute',
172175
// add a bit more width to each box (100px should cover margin/padding, etc; then add 1/2 overall width in case only one panel exists
173-
width : (base.curWidth + 100) * base.$panels.length + (o.width - base.curWidth) / 2,
176+
width : (base.curWidth + 100) * base.$panels.length + (base.width - base.curWidth) / 2,
174177
height : Math.max.apply( this, base.heights ) + 10
175178
});
176179
base.$window.css({ height : (o.fixedHeight) ? Math.max.apply( this, base.heights ) : base.heights[base.curPanel - base.adj] });
177180
// add padding so scrollLeft = 0 centers the left-most panel (needed because scrollLeft cannot be < 0)
178-
base.$panels.eq(0).css({ 'margin-left' : (o.width - base.curWidth) / 2 });
181+
base.$panels.eq(0).css({ 'margin-left' : (base.width - base.curWidth) / 2 });
179182

180183
base.buildNav();
181184

182-
base.change(base.curPanel, null, true); // initialize from first panel... then scroll to start panel
185+
base.change(base.curPanel, callback, (flag === false) ? false : true); // initialize from first panel... then scroll to start panel
183186

184187
};
185188

@@ -229,12 +232,12 @@
229232
var panels = base.$panels.eq(num - base.adj);
230233
if (o.reducedSize === 1) {
231234
panels.css({ width: base.curWidth }); // excluding fontsize change to prevent video flicker
232-
if (base.initialized) { base.completed(num, flag); }
235+
base.completed(num, flag);
233236
} else {
234237
panels.animate({ width: base.curWidth, fontSize: '1em' }, (time === 0) ? 0 : o.speed, function(){
235238
// completed event trigger
236239
// even though animation is not queued, trigger is here because it is the last animation to complete
237-
if (base.initialized) { base.completed(num, flag); }
240+
base.completed(num, flag);
238241
});
239242
}
240243
};
@@ -265,11 +268,12 @@
265268
return;
266269
}
267270
var ani, leftValue, wrapped = false;
271+
flag = flag !== false;
268272

269273
// make sure it's a number and not a string
270274
curPanel = parseInt(curPanel, 10);
271275

272-
if (base.initialized) {
276+
if (base.initialized && flag) {
273277
// make this moving box active
274278
base.active();
275279
// initChange event - has extra parameter with targeted panel (not cleaned)
@@ -283,40 +287,37 @@
283287
curPanel = 1;
284288
base.returnToNormal(0, 0);
285289
base.growBigger(0, 0, false);
286-
leftValue = base.$panels.eq(0).position().left - (o.width - base.curWidth) / 2; // - ( base.curWidth - base.regWidth );
290+
leftValue = base.$panels.eq(0).position().left - (base.width - base.curWidth) / 2; // - ( base.curWidth - base.regWidth );
287291
base.$window.scrollLeft(leftValue);
288292
} else if (curPanel === 0) {
289293
wrapped = false;
290294
curPanel = base.totalPanels;
295+
base.returnToNormal(curPanel + 1, 0);
291296
base.growBigger(curPanel + 1, 0, false);
292-
leftValue = base.$panels.eq(curPanel + 1).position().left - (o.width - base.curWidth) / 2; // - ( base.curWidth - base.regWidth );
297+
leftValue = base.$panels.eq(curPanel + 1).position().left - (base.width - base.curWidth) / 2; // - ( base.curWidth - base.regWidth );
293298
base.$window.scrollLeft(leftValue);
294299
}
295300
}
296301

297302
if ( curPanel < base.adj ) { curPanel = (o.wrap) ? base.totalPanels : 1; }
298303
if ( curPanel > base.totalPanels - base.adj ) { curPanel = (o.wrap) ? 1 : base.totalPanels; }
299304

300-
// don't do anything if it's the same panel
301-
if (base.initialized && base.curPanel === curPanel && !flag) { return false; }
302-
303305
// abort if panel is already animating
304306
// animation callback to clear this flag is not called when the slider doesn't move, so include base.initialized
305307
if (!base.currentlyMoving || !base.initialized) {
306308
base.currentlyMoving = true;
307309

308310
// center panel in scroll window
309311
base.$curPanel = base.$panels.eq(curPanel - base.adj);
310-
leftValue = base.$curPanel.position().left - (o.width - base.curWidth) / 2;
312+
leftValue = base.$curPanel.position().left - (base.width - base.curWidth) / 2;
311313

312314
// when scrolling right, add the difference of the larger current panel width
313315
if (curPanel > base.curPanel || wrapped) { leftValue -= ( base.curWidth - base.regWidth ); }
314316
ani = (o.fixedHeight) ? { scrollLeft : leftValue } : { scrollLeft: leftValue, height: base.heights[curPanel - base.adj] };
315317

316318
base.curPanel = curPanel;
317-
318319
// before animation trigger
319-
if (base.initialized) { base.$el.trigger( 'beforeAnimation.movingBoxes', [ base, curPanel ] ); }
320+
if (base.initialized && flag) { base.$el.trigger( 'beforeAnimation.movingBoxes', [ base, curPanel ] ); }
320321
// animate the panels
321322
base.$window.animate( ani,
322323
{
@@ -341,7 +342,7 @@
341342
}
342343

343344
// Update navigation links
344-
if (o.buildNav && base.$nav) {
345+
if (o.buildNav && base.$nav.length) {
345346
base.$nav.find('a')
346347
.removeClass(o.currentPanel)
347348
.eq(curPanel - 1).addClass(o.currentPanel);
@@ -398,6 +399,7 @@
398399

399400
// Behaviour
400401
speed : 500, // animation time in milliseconds
402+
initAnimation: true, // if true, movingBoxes will initialize, then animate into the starting slide (if not the first slide)
401403
hashTags : true, // if true, hash tags are enabled
402404
wrap : false, // if true, the panel will "wrap" (it really rewinds/fast forwards) at the ends
403405
buildNav : false, // if true, navigation links will be added

0 commit comments

Comments
 (0)