{"id":7611,"date":"2015-09-15T01:52:18","date_gmt":"2015-09-15T01:52:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/09\/15\/srobbin-jquery-backstretch\/"},"modified":"2015-09-15T01:52:18","modified_gmt":"2015-09-15T01:52:18","slug":"srobbin-jquery-backstretch","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/09\/15\/srobbin-jquery-backstretch\/","title":{"rendered":"srobbin\/jquery-backstretch"},"content":{"rendered":"<p>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.<\/p>\n<h2>Demo<\/h2>\n<p>There are a couple of examples included with this package, or feel free to check it out live on the project page itself.<\/p>\n<h2>Setup<\/h2>\n<p>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):<\/p>\n<pre><code>\n\n\n  \/\/ To attach Backstrech as the body's background\n  $.backstretch(\"path\/to\/image.jpg\");\n\n  \/\/ You may also attach Backstretch to a block-level element\n  $(\".foo\").backstretch(\"path\/to\/image.jpg\");\n\n  \/\/ Or, to start a slideshow, just pass in an array of images\n  $(\".foo\").backstretch([\n    \"path\/to\/image.jpg\",\n    \"path\/to\/image2.jpg\",\n    \"path\/to\/image3.jpg\"    \n  ], {duration: 4000});\n\n<\/code><\/pre>\n<h2>Options<\/h2>\n<table>\n<tr>\n<th>Name<\/th>\n<th>Description<\/th>\n<th>Type<\/th>\n<th>Default<\/th>\n<\/tr>\n<tr>\n<td><code>centeredX<\/code><\/td>\n<td>The ratio of the width\/height of the image doesn\u2019t 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.<\/td>\n<td>Boolean<\/td>\n<td>true<\/td>\n<\/tr>\n<tr>\n<td><code>centeredY<\/code><\/td>\n<td>This parameter controls whether or not we center the image on the Y axis to account for the aforementioned discrepancy.<\/td>\n<td>Boolean<\/td>\n<td>true<\/td>\n<\/tr>\n<tr>\n<td><code>fade<\/code><\/td>\n<td>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).<\/td>\n<td>Integer or String<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td><code>duration<\/code><\/td>\n<td>The amount of time in between slides, when using Backstretch as a slideshow, expressed as the number of milliseconds.<\/td>\n<td>Integer<\/td>\n<td>5000<\/td>\n<\/tr>\n<\/table>\n<h2>Slideshow API<\/h2>\n<p>Once you\u2019ve instantiated a Backstretch slideshow, there are many actions that you can perform it:<\/p>\n<pre><code>\/\/ Start a slideshow\n$('.foo').backstretch([\n  'path\/to\/image.jpg',\n  'path\/to\/image2.jpg',\n  'path\/to\/image3.jpg'\n]);\n\n\/\/ Pause the slideshow\n$('.foo').backstretch(\"pause\");\n\n\/\/ Advance to the next slide\n$('.foo').backstretch(\"next\");\n<\/code><\/pre>\n<table>\n<tr>\n<th>Method<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><code>.backstretch(\"show\", n)<\/code><\/td>\n<td>Jump to the slide at a given index, where n is the number of the image that you\u2019d like to display. Slide number starts at 0.<\/td>\n<\/tr>\n<tr>\n<td><code>.backstretch(\"prev\")<\/code><\/td>\n<td>Display the previous image in a slideshow.<\/td>\n<\/tr>\n<tr>\n<td><code>.backstretch(\"next\")<\/code><\/td>\n<td>Advance to the next image in a slideshow.<\/td>\n<\/tr>\n<tr>\n<td><code>.backstretch(\"pause\")<\/code><\/td>\n<td>Pause the slideshow.<\/td>\n<\/tr>\n<tr>\n<td><code>.backstretch(\"resume\")<\/code><\/td>\n<td>Resume a paused slideshow.<\/td>\n<\/tr>\n<tr>\n<td><code>.backstretch(\"destroy\", preserveBackground)<\/code><\/td>\n<td>Destroy the Backstretch instance. Optionally, you can pass in a Boolean parameter, preserveBackground, to determine whether or not you\u2019d like to keep the current image stretched as the background image.<\/td>\n<\/tr>\n<tr>\n<td><code>.backstretch(\"resize\")<\/code><\/td>\n<td>This method is called automatically when the container (window or block-level element) is resized, however you can always call this manually if needed.<\/td>\n<\/tr>\n<\/table>\n<h2>Public Variables<\/h2>\n<p>Sometimes, you\u2019ll want to access Backstretch\u2019s images after you\u2019ve instantiated the plugin. For example, perhaps you\u2019d like to be able add more images to a slideshow. Doing so is easy. You can access the images array as follows:<\/p>\n<pre><code>$('.foo').backstretch([\n  'path\/to\/image.jpg',\n  'path\/to\/image2.jpg',\n  'path\/to\/image3.jpg'\n]);\n\n\/\/ Access the instance\nvar instance = $('.foo').data('backstretch');\n\n\/\/ Then, you can manipulate the images array directly\ninstance.images.push('path\/to\/image4.jpg')\n<\/code><\/pre>\n<p>Additionally, the current index of a slideshow is available through the instance as well:<\/p>\n<pre><code>$(\"body\").data(\"backstretch\").index;\n<\/code><\/pre>\n<h2>Events<\/h2>\n<h3>backstretch.before<\/h3>\n<p>Backstretch will fire a \u201cbackstretch.before\u201d 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.<\/p>\n<pre><code>$(window).on(\"backstretch.before\", function (e, instance, index) {\n  \/\/ If we wanted to stop the slideshow after it reached the end\n  if (index === instance.images.length - 1) {\n    instance.pause();\n  };\n});\n<\/code><\/pre>\n<h3>backstretch.after<\/h3>\n<p>Backstretch will also fire a \u201cbackstretch.after\u201d event after the new images has completed loading.<\/p>\n<pre><code>$(window).on(\"backstretch.after\", function (e, instance, index) {\n  \/\/ Do something\n});\n<\/code><\/pre>\n<h2>Changelog<\/h2>\n<h3>Version 2.0<\/h3>\n<ul>\n<li>Now accepts an array of images to create a slideshow<\/li>\n<li>Can attach Backstretch to any block-level element, not just the body<\/li>\n<li>Deprecated \u201cspeed\u201d option in favor of \u201cfade\u201d for fadeIn timing<\/li>\n<li>Added \u201cduration\u201d option, and Slideshow API<\/li>\n<\/ul>\n<h3>Version 1.2<\/h3>\n<ul>\n<li>You can now called backstretch twice, and it will replace the existing image.<\/li>\n<\/ul>\n<h3>Version 1.1<\/h3>\n<ul>\n<li>Added \u2018centeredX\u2019 and \u2018centeredY\u2019 options.<\/li>\n<li>Removed \u2018hideUntilReady\u2019 option. It looks pretty bad if you don\u2019t hide the image until it\u2019s fully loaded.<\/li>\n<li>Fixed IE img onload bug.<\/li>\n<li>Now supports iPhone\/iPad orientation changes.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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. Demo There are a couple of examples included with this package, or feel free to check it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7611","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7611"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7611\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}