Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
# Backstretch
2
 
3
Backstretch is a simple jQuery plugin that allows you to add a dynamically-resized, slideshow-capable background image to any page or element. The image will stretch to fit the page/element, and will automatically resize as the window/element size changes.
4
## Demo
5
 
6
There are a couple of examples included with this package, or feel free to check it out live [on the project page itself](http://srobbin.com/jquery-plugins/backstretch/).
7
 
8
## Setup
9
 
10
Include the jQuery library (version 1.7 or newer) and Backstretch plugin files in your webpage (preferably at the bottom of the page, before the closing BODY tag):
11
 
12
```html
13
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
14
<script src="jquery.backstretch.min.js"></script>
15
<script>
16
  // To attach Backstrech as the body's background
17
  $.backstretch("path/to/image.jpg");
18
 
19
  // You may also attach Backstretch to a block-level element
20
  $(".foo").backstretch("path/to/image.jpg");
21
 
22
  // Or, to start a slideshow, just pass in an array of images
23
  $(".foo").backstretch([
24
    "path/to/image.jpg",
25
    "path/to/image2.jpg",
26
    "path/to/image3.jpg"
27
  ], {duration: 4000});
28
</script>
29
```
30
 
31
## Options
32
 
33
| Name | Description | Type | Default |
34
|------|-------------|------|---------|
35
| `centeredX` | The ratio of the width/height of the image doesn't always jive with the width/height of the window. This parameter controls whether or not we center the image on the X axis to account for the discrepancy. | Boolean | true |
36
| `centeredY` | This parameter controls whether or not we center the image on the Y axis to account for the aforementioned discrepancy. | Boolean | true |
37
| `fade` | This is the speed at which the image will fade in. Integers in milliseconds are accepted, as well as standard jQuery speed strings (slow, normal, fast). | Integer or String | 0 |
38
| `duration` | The amount of time in between slides, when using Backstretch as a slideshow, expressed as the number of milliseconds. | Integer | 5000 |
39
 
40
## Slideshow API
41
 
42
Once you've instantiated a Backstretch slideshow, there are many actions that you can perform it:
43
 
44
```javascript
45
// Start a slideshow
46
$('.foo').backstretch([
47
  'path/to/image.jpg',
48
  'path/to/image2.jpg',
49
  'path/to/image3.jpg'
50
]);
51
 
52
// Pause the slideshow
53
$('.foo').backstretch("pause");
54
 
55
// Advance to the next slide
56
$('.foo').backstretch("next");
57
```
58
 
59
| Method | Description |
60
|------|-------------|
61
| `.backstretch("show", n)` | Jump to the slide at a given index, where n is the number of the image that you'd like to display. Slide number starts at 0. |
62
| `.backstretch("prev")` | Display the previous image in a slideshow. |
63
| `.backstretch("next")` | Advance to the next image in a slideshow. |
64
| `.backstretch("pause")` | Pause the slideshow. |
65
| `.backstretch("resume")` | Resume a paused slideshow. |
66
| `.backstretch("destroy", preserveBackground)` | Destroy the Backstretch instance. Optionally, you can pass in a Boolean parameter, preserveBackground, to determine whether or not you'd like to keep the current image stretched as the background image. |
67
| `.backstretch("resize")` | This method is called automatically when the container (window or block-level element) is resized, however you can always call this manually if needed. |
68
 
69
## Public Variables
70
 
71
Sometimes, you'll want to access Backstretch's images after you've instantiated the plugin. For example, perhaps you'd like to be able add more images to a slideshow. Doing so is easy. You can access the images array as follows:
72
 
73
```javascript
74
$('.foo').backstretch([
75
  'path/to/image.jpg',
76
  'path/to/image2.jpg',
77
  'path/to/image3.jpg'
78
]);
79
 
80
// Access the instance
81
var instance = $('.foo').data('backstretch');
82
 
83
// Then, you can manipulate the images array directly
84
instance.images.push('path/to/image4.jpg')
85
```
86
 
87
Additionally, the current index of a slideshow is available through the instance as well:
88
 
89
```javascript
90
$("body").data("backstretch").index;
91
```
92
 
93
## Events
94
 
95
### backstretch.before
96
 
97
Backstretch will fire a "backstretch.before" event before a new image loads, triggering a function that is passed the event, Backstretch instance, and index of the image that will be displayed. If you listen for that event, you can, for example, coordinate other changes to coincide with your slideshow.
98
 
99
```javascript
100
$(window).on("backstretch.before", function (e, instance, index) {
101
  // If we wanted to stop the slideshow after it reached the end
102
  if (index === instance.images.length - 1) {
103
    instance.pause();
104
  };
105
});
106
```
107
 
108
### backstretch.after
109
 
110
Backstretch will also fire a "backstretch.after" event after the new images has completed loading.
111
 
112
```javascript
113
$(window).on("backstretch.after", function (e, instance, index) {
114
  // Do something
115
});
116
```
117
 
118
## Changelog
119
 
120
### Version 2.0
121
 
122
* Now accepts an array of images to create a slideshow
123
* Can attach Backstretch to any block-level element, not just the body
124
* Deprecated "speed" option in favor of "fade" for fadeIn timing
125
* Added "duration" option, and Slideshow API
126
 
127
### Version 1.2
128
 
129
* You can now called backstretch twice, and it will replace the existing image.
130
 
131
### Version 1.1
132
 
133
* Added 'centeredX' and 'centeredY' options.
134
* Removed 'hideUntilReady' option. It looks pretty bad if you don't hide the image until it's fully loaded.
135
* Fixed IE img onload bug.
136
* Now supports iPhone/iPad orientation changes.