jquery.swipebox.js
930 lines
| 1 | /*! Swipebox v1.3.0.2 | Constantin Saguin csag.co | MIT License | github.com/brutaldesign/swipebox */ |
| 2 | |
| 3 | ;( function ( window, document, $, undefined ) { |
| 4 | |
| 5 | $.swipebox = function( elem, options ) { |
| 6 | |
| 7 | // Default options |
| 8 | var ui, |
| 9 | defaults = { |
| 10 | useCSS : true, |
| 11 | useSVG : true, |
| 12 | initialIndexOnArray : 0, |
| 13 | removeBarsOnMobile : true, |
| 14 | hideCloseButtonOnMobile : false, |
| 15 | hideBarsDelay : 3000, |
| 16 | videoMaxWidth : 1140, |
| 17 | vimeoColor : 'cccccc', |
| 18 | beforeOpen: null, |
| 19 | afterOpen: null, |
| 20 | afterClose: null, |
| 21 | loopAtEnd: false, |
| 22 | autoplayVideos: false, |
| 23 | queryStringData: {}, |
| 24 | toggleClassOnLoad: '' |
| 25 | }, |
| 26 | |
| 27 | plugin = this, |
| 28 | elements = [], // slides array [ { href:'...', title:'...' }, ...], |
| 29 | $elem, |
| 30 | selector = elem.selector, |
| 31 | $selector = $( selector ), |
| 32 | isMobile = navigator.userAgent.match( /(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i ), |
| 33 | isTouch = isMobile !== null || document.createTouch !== undefined || ( 'ontouchstart' in window ) || ( 'onmsgesturechange' in window ) || navigator.msMaxTouchPoints, |
| 34 | supportSVG = !! document.createElementNS && !! document.createElementNS( 'http://www.w3.org/2000/svg', 'svg').createSVGRect, |
| 35 | winWidth = window.innerWidth ? window.innerWidth : $( window ).width(), |
| 36 | winHeight = window.innerHeight ? window.innerHeight : $( window ).height(), |
| 37 | currentX = 0, |
| 38 | /* jshint multistr: true */ |
| 39 | html = '<div id="swipebox-overlay">\ |
| 40 | <div id="swipebox-container">\ |
| 41 | <div id="swipebox-slider"></div>\ |
| 42 | <div id="swipebox-top-bar">\ |
| 43 | <div id="swipebox-title"></div>\ |
| 44 | </div>\ |
| 45 | <div id="swipebox-bottom-bar">\ |
| 46 | <div id="swipebox-arrows">\ |
| 47 | <a id="swipebox-prev"></a>\ |
| 48 | <a id="swipebox-next"></a>\ |
| 49 | </div>\ |
| 50 | </div>\ |
| 51 | <a id="swipebox-close"></a>\ |
| 52 | </div>\ |
| 53 | </div>'; |
| 54 | |
| 55 | plugin.settings = {}; |
| 56 | |
| 57 | $.swipebox.close = function () { |
| 58 | ui.closeSlide(); |
| 59 | }; |
| 60 | |
| 61 | $.swipebox.extend = function () { |
| 62 | return ui; |
| 63 | }; |
| 64 | |
| 65 | plugin.init = function() { |
| 66 | |
| 67 | plugin.settings = $.extend( {}, defaults, options ); |
| 68 | |
| 69 | if ( $.isArray( elem ) ) { |
| 70 | |
| 71 | elements = elem; |
| 72 | ui.target = $( window ); |
| 73 | ui.init( plugin.settings.initialIndexOnArray ); |
| 74 | |
| 75 | } else { |
| 76 | |
| 77 | $( document ).on( 'click', selector, function( event ) { |
| 78 | |
| 79 | // console.log( isTouch ); |
| 80 | |
| 81 | if ( event.target.parentNode.className === 'slide current' ) { |
| 82 | |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if ( ! $.isArray( elem ) ) { |
| 87 | ui.destroy(); |
| 88 | $elem = $( selector ); |
| 89 | ui.actions(); |
| 90 | } |
| 91 | |
| 92 | elements = []; |
| 93 | var index , relType, relVal; |
| 94 | |
| 95 | // Allow for HTML5 compliant attribute before legacy use of rel |
| 96 | if ( ! relVal ) { |
| 97 | relType = 'data-rel'; |
| 98 | relVal = $( this ).attr( relType ); |
| 99 | } |
| 100 | |
| 101 | if ( ! relVal ) { |
| 102 | relType = 'rel'; |
| 103 | relVal = $( this ).attr( relType ); |
| 104 | } |
| 105 | |
| 106 | if ( relVal && relVal !== '' && relVal !== 'nofollow' ) { |
| 107 | $elem = $selector.filter( '[' + relType + '="' + relVal + '"]' ); |
| 108 | } else { |
| 109 | $elem = $( selector ); |
| 110 | } |
| 111 | |
| 112 | $elem.each( function() { |
| 113 | |
| 114 | var title = null, |
| 115 | href = null; |
| 116 | |
| 117 | if ( $( this ).attr( 'title' ) ) { |
| 118 | title = $( this ).attr( 'title' ); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | if ( $( this ).attr( 'href' ) ) { |
| 123 | href = $( this ).attr( 'href' ); |
| 124 | } |
| 125 | |
| 126 | elements.push( { |
| 127 | href: href, |
| 128 | title: title |
| 129 | } ); |
| 130 | } ); |
| 131 | |
| 132 | index = $elem.index( $( this ) ); |
| 133 | event.preventDefault(); |
| 134 | event.stopPropagation(); |
| 135 | ui.target = $( event.target ); |
| 136 | ui.init( index ); |
| 137 | } ); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | ui = { |
| 142 | |
| 143 | /** |
| 144 | * Initiate Swipebox |
| 145 | */ |
| 146 | init : function( index ) { |
| 147 | if ( plugin.settings.beforeOpen ) { |
| 148 | plugin.settings.beforeOpen(); |
| 149 | } |
| 150 | this.target.trigger( 'swipebox-start' ); |
| 151 | $.swipebox.isOpen = true; |
| 152 | this.build(); |
| 153 | this.openSlide( index ); |
| 154 | this.openMedia( index ); |
| 155 | this.preloadMedia( index+1 ); |
| 156 | this.preloadMedia( index-1 ); |
| 157 | if ( plugin.settings.afterOpen ) { |
| 158 | plugin.settings.afterOpen(); |
| 159 | } |
| 160 | }, |
| 161 | |
| 162 | /** |
| 163 | * Built HTML containers and fire main functions |
| 164 | */ |
| 165 | build : function () { |
| 166 | var $this = this, bg; |
| 167 | |
| 168 | $( 'body' ).append( html ); |
| 169 | |
| 170 | if ( supportSVG && plugin.settings.useSVG === true ) { |
| 171 | bg = $( '#swipebox-close' ).css( 'background-image' ); |
| 172 | bg = bg.replace( 'png', 'svg' ); |
| 173 | $( '#swipebox-prev, #swipebox-next, #swipebox-close' ).css( { |
| 174 | 'background-image' : bg |
| 175 | } ); |
| 176 | } |
| 177 | |
| 178 | if ( isMobile && plugin.settings.removeBarsOnMobile ) { |
| 179 | $( '#swipebox-bottom-bar, #swipebox-top-bar' ).remove(); |
| 180 | } |
| 181 | |
| 182 | $.each( elements, function() { |
| 183 | $( '#swipebox-slider' ).append( '<div class="slide"></div>' ); |
| 184 | } ); |
| 185 | |
| 186 | $this.setDim(); |
| 187 | $this.actions(); |
| 188 | |
| 189 | if ( isTouch ) { |
| 190 | $this.gesture(); |
| 191 | } |
| 192 | |
| 193 | // Devices can have both touch and keyboard input so always allow key events |
| 194 | $this.keyboard(); |
| 195 | |
| 196 | $this.animBars(); |
| 197 | $this.resize(); |
| 198 | |
| 199 | }, |
| 200 | |
| 201 | /** |
| 202 | * Set dimensions depending on windows width and height |
| 203 | */ |
| 204 | setDim : function () { |
| 205 | |
| 206 | var width, height, sliderCss = {}; |
| 207 | |
| 208 | // Reset dimensions on mobile orientation change |
| 209 | if ( 'onorientationchange' in window ) { |
| 210 | |
| 211 | window.addEventListener( 'orientationchange', function() { |
| 212 | if ( window.orientation === 0 ) { |
| 213 | width = winWidth; |
| 214 | height = winHeight; |
| 215 | } else if ( window.orientation === 90 || window.orientation === -90 ) { |
| 216 | width = winHeight; |
| 217 | height = winWidth; |
| 218 | } |
| 219 | }, false ); |
| 220 | |
| 221 | |
| 222 | } else { |
| 223 | |
| 224 | width = window.innerWidth ? window.innerWidth : $( window ).width(); |
| 225 | height = window.innerHeight ? window.innerHeight : $( window ).height(); |
| 226 | } |
| 227 | |
| 228 | sliderCss = { |
| 229 | width : width, |
| 230 | height : height |
| 231 | }; |
| 232 | |
| 233 | $( '#swipebox-overlay' ).css( sliderCss ); |
| 234 | |
| 235 | }, |
| 236 | |
| 237 | /** |
| 238 | * Reset dimensions on window resize envent |
| 239 | */ |
| 240 | resize : function () { |
| 241 | var $this = this; |
| 242 | |
| 243 | $( window ).resize( function() { |
| 244 | $this.setDim(); |
| 245 | } ).resize(); |
| 246 | }, |
| 247 | |
| 248 | /** |
| 249 | * Check if device supports CSS transitions |
| 250 | */ |
| 251 | supportTransition : function () { |
| 252 | |
| 253 | var prefixes = 'transition WebkitTransition MozTransition OTransition msTransition KhtmlTransition'.split( ' ' ), |
| 254 | i; |
| 255 | |
| 256 | for ( i = 0; i < prefixes.length; i++ ) { |
| 257 | if ( document.createElement( 'div' ).style[ prefixes[i] ] !== undefined ) { |
| 258 | return prefixes[i]; |
| 259 | } |
| 260 | } |
| 261 | return false; |
| 262 | }, |
| 263 | |
| 264 | /** |
| 265 | * Check if CSS transitions are allowed (options + devicesupport) |
| 266 | */ |
| 267 | doCssTrans : function () { |
| 268 | if ( plugin.settings.useCSS && this.supportTransition() ) { |
| 269 | return true; |
| 270 | } |
| 271 | }, |
| 272 | |
| 273 | /** |
| 274 | * Touch navigation |
| 275 | */ |
| 276 | gesture : function () { |
| 277 | |
| 278 | var $this = this, |
| 279 | index, |
| 280 | hDistance, |
| 281 | vDistance, |
| 282 | hDistanceLast, |
| 283 | vDistanceLast, |
| 284 | hDistancePercent, |
| 285 | vSwipe = false, |
| 286 | hSwipe = false, |
| 287 | hSwipMinDistance = 10, |
| 288 | vSwipMinDistance = 50, |
| 289 | startCoords = {}, |
| 290 | endCoords = {}, |
| 291 | bars = $( '#swipebox-top-bar, #swipebox-bottom-bar' ), |
| 292 | slider = $( '#swipebox-slider' ); |
| 293 | |
| 294 | bars.addClass( 'visible-bars' ); |
| 295 | $this.setTimeout(); |
| 296 | |
| 297 | $( 'body' ).bind( 'touchstart', function( event ) { |
| 298 | |
| 299 | $( this ).addClass( 'touching' ); |
| 300 | index = $( '#swipebox-slider .slide' ).index( $( '#swipebox-slider .slide.current' ) ); |
| 301 | endCoords = event.originalEvent.targetTouches[0]; |
| 302 | startCoords.pageX = event.originalEvent.targetTouches[0].pageX; |
| 303 | startCoords.pageY = event.originalEvent.targetTouches[0].pageY; |
| 304 | |
| 305 | $( '#swipebox-slider' ).css( { |
| 306 | '-webkit-transform' : 'translate3d(' + currentX +'%, 0, 0)', |
| 307 | 'transform' : 'translate3d(' + currentX + '%, 0, 0)' |
| 308 | } ); |
| 309 | |
| 310 | $( '.touching' ).bind( 'touchmove',function( event ) { |
| 311 | event.preventDefault(); |
| 312 | event.stopPropagation(); |
| 313 | endCoords = event.originalEvent.targetTouches[0]; |
| 314 | |
| 315 | if ( ! hSwipe ) { |
| 316 | vDistanceLast = vDistance; |
| 317 | vDistance = endCoords.pageY - startCoords.pageY; |
| 318 | if ( Math.abs( vDistance ) >= vSwipMinDistance || vSwipe ) { |
| 319 | var opacity = 0.75 - Math.abs(vDistance) / slider.height(); |
| 320 | |
| 321 | slider.css( { 'top': vDistance + 'px' } ); |
| 322 | slider.css( { 'opacity': opacity } ); |
| 323 | |
| 324 | vSwipe = true; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | hDistanceLast = hDistance; |
| 329 | hDistance = endCoords.pageX - startCoords.pageX; |
| 330 | hDistancePercent = hDistance * 100 / winWidth; |
| 331 | |
| 332 | if ( ! hSwipe && ! vSwipe && Math.abs( hDistance ) >= hSwipMinDistance ) { |
| 333 | $( '#swipebox-slider' ).css( { |
| 334 | '-webkit-transition' : '', |
| 335 | 'transition' : '' |
| 336 | } ); |
| 337 | hSwipe = true; |
| 338 | } |
| 339 | |
| 340 | if ( hSwipe ) { |
| 341 | |
| 342 | // swipe left |
| 343 | if ( 0 < hDistance ) { |
| 344 | |
| 345 | // first slide |
| 346 | if ( 0 === index ) { |
| 347 | // console.log( 'first' ); |
| 348 | $( '#swipebox-overlay' ).addClass( 'leftSpringTouch' ); |
| 349 | } else { |
| 350 | // Follow gesture |
| 351 | $( '#swipebox-overlay' ).removeClass( 'leftSpringTouch' ).removeClass( 'rightSpringTouch' ); |
| 352 | $( '#swipebox-slider' ).css( { |
| 353 | '-webkit-transform' : 'translate3d(' + ( currentX + hDistancePercent ) +'%, 0, 0)', |
| 354 | 'transform' : 'translate3d(' + ( currentX + hDistancePercent ) + '%, 0, 0)' |
| 355 | } ); |
| 356 | } |
| 357 | |
| 358 | // swipe rught |
| 359 | } else if ( 0 > hDistance ) { |
| 360 | |
| 361 | // last Slide |
| 362 | if ( elements.length === index +1 ) { |
| 363 | // console.log( 'last' ); |
| 364 | $( '#swipebox-overlay' ).addClass( 'rightSpringTouch' ); |
| 365 | } else { |
| 366 | $( '#swipebox-overlay' ).removeClass( 'leftSpringTouch' ).removeClass( 'rightSpringTouch' ); |
| 367 | $( '#swipebox-slider' ).css( { |
| 368 | '-webkit-transform' : 'translate3d(' + ( currentX + hDistancePercent ) +'%, 0, 0)', |
| 369 | 'transform' : 'translate3d(' + ( currentX + hDistancePercent ) + '%, 0, 0)' |
| 370 | } ); |
| 371 | } |
| 372 | |
| 373 | } |
| 374 | } |
| 375 | } ); |
| 376 | |
| 377 | return false; |
| 378 | |
| 379 | } ).bind( 'touchend',function( event ) { |
| 380 | event.preventDefault(); |
| 381 | event.stopPropagation(); |
| 382 | |
| 383 | $( '#swipebox-slider' ).css( { |
| 384 | '-webkit-transition' : '-webkit-transform 0.4s ease', |
| 385 | 'transition' : 'transform 0.4s ease' |
| 386 | } ); |
| 387 | |
| 388 | vDistance = endCoords.pageY - startCoords.pageY; |
| 389 | hDistance = endCoords.pageX - startCoords.pageX; |
| 390 | hDistancePercent = hDistance*100/winWidth; |
| 391 | |
| 392 | // Swipe to bottom to close |
| 393 | if ( vSwipe ) { |
| 394 | vSwipe = false; |
| 395 | if ( Math.abs( vDistance ) >= 2 * vSwipMinDistance && Math.abs( vDistance ) > Math.abs( vDistanceLast ) ) { |
| 396 | var vOffset = vDistance > 0 ? slider.height() : - slider.height(); |
| 397 | slider.animate( { top: vOffset + 'px', 'opacity': 0 }, |
| 398 | 300, |
| 399 | function () { |
| 400 | $this.closeSlide(); |
| 401 | } ); |
| 402 | } else { |
| 403 | slider.animate( { top: 0, 'opacity': 1 }, 300 ); |
| 404 | } |
| 405 | |
| 406 | } else if ( hSwipe ) { |
| 407 | |
| 408 | hSwipe = false; |
| 409 | |
| 410 | // swipeLeft |
| 411 | if( hDistance >= hSwipMinDistance && hDistance >= hDistanceLast) { |
| 412 | |
| 413 | $this.getPrev(); |
| 414 | |
| 415 | // swipeRight |
| 416 | } else if ( hDistance <= -hSwipMinDistance && hDistance <= hDistanceLast) { |
| 417 | |
| 418 | $this.getNext(); |
| 419 | } |
| 420 | |
| 421 | } else { // Top and bottom bars have been removed on touchable devices |
| 422 | // tap |
| 423 | if ( ! bars.hasClass( 'visible-bars' ) ) { |
| 424 | $this.showBars(); |
| 425 | $this.setTimeout(); |
| 426 | } else { |
| 427 | $this.clearTimeout(); |
| 428 | $this.hideBars(); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | $( '#swipebox-slider' ).css( { |
| 433 | '-webkit-transform' : 'translate3d(' + currentX + '%, 0, 0)', |
| 434 | 'transform' : 'translate3d(' + currentX + '%, 0, 0)' |
| 435 | } ); |
| 436 | |
| 437 | $( '#swipebox-overlay' ).removeClass( 'leftSpringTouch' ).removeClass( 'rightSpringTouch' ); |
| 438 | $( '.touching' ).off( 'touchmove' ).removeClass( 'touching' ); |
| 439 | |
| 440 | } ); |
| 441 | }, |
| 442 | |
| 443 | /** |
| 444 | * Set timer to hide the action bars |
| 445 | */ |
| 446 | setTimeout: function () { |
| 447 | if ( plugin.settings.hideBarsDelay > 0 ) { |
| 448 | var $this = this; |
| 449 | $this.clearTimeout(); |
| 450 | $this.timeout = window.setTimeout( function() { |
| 451 | $this.hideBars(); |
| 452 | }, |
| 453 | |
| 454 | plugin.settings.hideBarsDelay |
| 455 | ); |
| 456 | } |
| 457 | }, |
| 458 | |
| 459 | /** |
| 460 | * Clear timer |
| 461 | */ |
| 462 | clearTimeout: function () { |
| 463 | window.clearTimeout( this.timeout ); |
| 464 | this.timeout = null; |
| 465 | }, |
| 466 | |
| 467 | /** |
| 468 | * Show navigation and title bars |
| 469 | */ |
| 470 | showBars : function () { |
| 471 | var bars = $( '#swipebox-top-bar, #swipebox-bottom-bar' ); |
| 472 | if ( this.doCssTrans() ) { |
| 473 | bars.addClass( 'visible-bars' ); |
| 474 | } else { |
| 475 | $( '#swipebox-top-bar' ).animate( { top : 0 }, 500 ); |
| 476 | $( '#swipebox-bottom-bar' ).animate( { bottom : 0 }, 500 ); |
| 477 | setTimeout( function() { |
| 478 | bars.addClass( 'visible-bars' ); |
| 479 | }, 1000 ); |
| 480 | } |
| 481 | }, |
| 482 | |
| 483 | /** |
| 484 | * Hide navigation and title bars |
| 485 | */ |
| 486 | hideBars : function () { |
| 487 | var bars = $( '#swipebox-top-bar, #swipebox-bottom-bar' ); |
| 488 | if ( this.doCssTrans() ) { |
| 489 | bars.removeClass( 'visible-bars' ); |
| 490 | } else { |
| 491 | $( '#swipebox-top-bar' ).animate( { top : '-50px' }, 500 ); |
| 492 | $( '#swipebox-bottom-bar' ).animate( { bottom : '-50px' }, 500 ); |
| 493 | setTimeout( function() { |
| 494 | bars.removeClass( 'visible-bars' ); |
| 495 | }, 1000 ); |
| 496 | } |
| 497 | }, |
| 498 | |
| 499 | /** |
| 500 | * Animate navigation and top bars |
| 501 | */ |
| 502 | animBars : function () { |
| 503 | var $this = this, |
| 504 | bars = $( '#swipebox-top-bar, #swipebox-bottom-bar' ); |
| 505 | |
| 506 | bars.addClass( 'visible-bars' ); |
| 507 | $this.setTimeout(); |
| 508 | |
| 509 | $( '#swipebox-slider' ).click( function() { |
| 510 | if ( ! bars.hasClass( 'visible-bars' ) ) { |
| 511 | $this.showBars(); |
| 512 | $this.setTimeout(); |
| 513 | } |
| 514 | } ); |
| 515 | |
| 516 | $( '#swipebox-bottom-bar' ).hover( function() { |
| 517 | $this.showBars(); |
| 518 | bars.addClass( 'visible-bars' ); |
| 519 | $this.clearTimeout(); |
| 520 | |
| 521 | }, function() { |
| 522 | if ( plugin.settings.hideBarsDelay > 0 ) { |
| 523 | bars.removeClass( 'visible-bars' ); |
| 524 | $this.setTimeout(); |
| 525 | } |
| 526 | |
| 527 | } ); |
| 528 | }, |
| 529 | |
| 530 | /** |
| 531 | * Keyboard navigation |
| 532 | */ |
| 533 | keyboard : function () { |
| 534 | var $this = this; |
| 535 | $( window ).bind( 'keyup', function( event ) { |
| 536 | event.preventDefault(); |
| 537 | event.stopPropagation(); |
| 538 | |
| 539 | if ( event.keyCode === 37 ) { |
| 540 | |
| 541 | $this.getPrev(); |
| 542 | |
| 543 | } else if ( event.keyCode === 39 ) { |
| 544 | |
| 545 | $this.getNext(); |
| 546 | |
| 547 | } else if ( event.keyCode === 27 ) { |
| 548 | |
| 549 | $this.closeSlide(); |
| 550 | } |
| 551 | } ); |
| 552 | }, |
| 553 | |
| 554 | /** |
| 555 | * Navigation events : go to next slide, go to prevous slide and close |
| 556 | */ |
| 557 | actions : function () { |
| 558 | var $this = this, |
| 559 | action = 'touchend click'; // Just detect for both event types to allow for multi-input |
| 560 | |
| 561 | if ( elements.length < 2 ) { |
| 562 | |
| 563 | $( '#swipebox-bottom-bar' ).hide(); |
| 564 | |
| 565 | if ( undefined === elements[ 1 ] ) { |
| 566 | $( '#swipebox-top-bar' ).hide(); |
| 567 | } |
| 568 | |
| 569 | } else { |
| 570 | $( '#swipebox-prev' ).bind( action, function( event ) { |
| 571 | event.preventDefault(); |
| 572 | event.stopPropagation(); |
| 573 | $this.getPrev(); |
| 574 | $this.setTimeout(); |
| 575 | } ); |
| 576 | |
| 577 | $( '#swipebox-next' ).bind( action, function( event ) { |
| 578 | event.preventDefault(); |
| 579 | event.stopPropagation(); |
| 580 | $this.getNext(); |
| 581 | $this.setTimeout(); |
| 582 | } ); |
| 583 | } |
| 584 | |
| 585 | $( '#swipebox-close' ).bind( action, function() { |
| 586 | $this.closeSlide(); |
| 587 | } ); |
| 588 | }, |
| 589 | |
| 590 | /** |
| 591 | * Set current slide |
| 592 | */ |
| 593 | setSlide : function ( index, isFirst ) { |
| 594 | |
| 595 | isFirst = isFirst || false; |
| 596 | |
| 597 | var slider = $( '#swipebox-slider' ); |
| 598 | |
| 599 | currentX = -index*100; |
| 600 | |
| 601 | if ( this.doCssTrans() ) { |
| 602 | slider.css( { |
| 603 | '-webkit-transform' : 'translate3d(' + (-index*100)+'%, 0, 0)', |
| 604 | 'transform' : 'translate3d(' + (-index*100)+'%, 0, 0)' |
| 605 | } ); |
| 606 | } else { |
| 607 | slider.animate( { left : ( -index*100 )+'%' } ); |
| 608 | } |
| 609 | |
| 610 | $( '#swipebox-slider .slide' ).removeClass( 'current' ); |
| 611 | $( '#swipebox-slider .slide' ).eq( index ).addClass( 'current' ); |
| 612 | this.setTitle( index ); |
| 613 | |
| 614 | if ( isFirst ) { |
| 615 | slider.fadeIn(); |
| 616 | } |
| 617 | |
| 618 | $( '#swipebox-prev, #swipebox-next' ).removeClass( 'disabled' ); |
| 619 | |
| 620 | if ( index === 0 ) { |
| 621 | $( '#swipebox-prev' ).addClass( 'disabled' ); |
| 622 | } else if ( index === elements.length - 1 && plugin.settings.loopAtEnd !== true ) { |
| 623 | $( '#swipebox-next' ).addClass( 'disabled' ); |
| 624 | } |
| 625 | }, |
| 626 | |
| 627 | /** |
| 628 | * Open slide |
| 629 | */ |
| 630 | openSlide : function ( index ) { |
| 631 | $( 'html' ).addClass( 'swipebox-html' ); |
| 632 | if ( isTouch ) { |
| 633 | $( 'html' ).addClass( 'swipebox-touch' ); |
| 634 | |
| 635 | if ( plugin.settings.hideCloseButtonOnMobile ) { |
| 636 | $( 'html' ).addClass( 'swipebox-no-close-button' ); |
| 637 | } |
| 638 | } else { |
| 639 | $( 'html' ).addClass( 'swipebox-no-touch' ); |
| 640 | } |
| 641 | $( window ).trigger( 'resize' ); // fix scroll bar visibility on desktop |
| 642 | this.setSlide( index, true ); |
| 643 | }, |
| 644 | |
| 645 | /** |
| 646 | * Set a time out if the media is a video |
| 647 | */ |
| 648 | preloadMedia : function ( index ) { |
| 649 | var $this = this, |
| 650 | src = null; |
| 651 | |
| 652 | if ( elements[ index ] !== undefined ) { |
| 653 | src = elements[ index ].href; |
| 654 | } |
| 655 | |
| 656 | if ( ! $this.isVideo( src ) ) { |
| 657 | setTimeout( function() { |
| 658 | $this.openMedia( index ); |
| 659 | }, 1000); |
| 660 | } else { |
| 661 | $this.openMedia( index ); |
| 662 | } |
| 663 | }, |
| 664 | |
| 665 | /** |
| 666 | * Open |
| 667 | */ |
| 668 | openMedia : function ( index ) { |
| 669 | var $this = this, |
| 670 | src, |
| 671 | slide; |
| 672 | |
| 673 | if ( elements[ index ] !== undefined ) { |
| 674 | src = elements[ index ].href; |
| 675 | } |
| 676 | |
| 677 | if ( index < 0 || index >= elements.length ) { |
| 678 | return false; |
| 679 | } |
| 680 | |
| 681 | slide = $( '#swipebox-slider .slide' ).eq( index ); |
| 682 | |
| 683 | if ( ! $this.isVideo( src ) ) { |
| 684 | slide.addClass( 'slide-loading' ); |
| 685 | $this.loadMedia( src, function() { |
| 686 | slide.removeClass( 'slide-loading' ); |
| 687 | slide.html( this ); |
| 688 | } ); |
| 689 | } else { |
| 690 | slide.html( $this.getVideo( src ) ); |
| 691 | } |
| 692 | |
| 693 | }, |
| 694 | |
| 695 | /** |
| 696 | * Set link title attribute as caption |
| 697 | */ |
| 698 | setTitle : function ( index ) { |
| 699 | var title = null; |
| 700 | |
| 701 | $( '#swipebox-title' ).empty(); |
| 702 | |
| 703 | if ( elements[ index ] !== undefined ) { |
| 704 | title = elements[ index ].title; |
| 705 | } |
| 706 | |
| 707 | if ( title ) { |
| 708 | $( '#swipebox-top-bar' ).show(); |
| 709 | $( '#swipebox-title' ).append( title ); |
| 710 | } else { |
| 711 | $( '#swipebox-top-bar' ).hide(); |
| 712 | } |
| 713 | }, |
| 714 | |
| 715 | /** |
| 716 | * Check if the URL is a video |
| 717 | */ |
| 718 | isVideo : function ( src ) { |
| 719 | |
| 720 | if ( src ) { |
| 721 | if ( src.match( /(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/) || src.match( /vimeo\.com\/([0-9]*)/ ) || src.match( /youtu\.be\/([a-zA-Z0-9\-_]+)/ ) ) { |
| 722 | return true; |
| 723 | } |
| 724 | |
| 725 | if ( src.toLowerCase().indexOf( 'swipeboxvideo=1' ) >= 0 ) { |
| 726 | |
| 727 | return true; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | }, |
| 732 | |
| 733 | /** |
| 734 | * Parse URI querystring and: |
| 735 | * - overrides value provided via dictionary |
| 736 | * - rebuild it again returning a string |
| 737 | */ |
| 738 | parseUri : function (uri, customData) { |
| 739 | var a = document.createElement('a'), |
| 740 | qs = {}; |
| 741 | |
| 742 | // Decode the URI |
| 743 | a.href = decodeURIComponent( uri ); |
| 744 | |
| 745 | // QueryString to Object |
| 746 | qs = JSON.parse( '{"' + a.search.toLowerCase().replace('?','').replace(/&/g,'","').replace(/=/g,'":"') + '"}' ); |
| 747 | |
| 748 | // Extend with custom data |
| 749 | if ( $.isPlainObject( customData ) ) { |
| 750 | qs = $.extend( qs, customData, plugin.settings.queryStringData ); // The dev has always the final word |
| 751 | } |
| 752 | |
| 753 | // Return querystring as a string |
| 754 | return $ |
| 755 | .map( qs, function (val, key) { |
| 756 | if ( val && val > '' ) { |
| 757 | return encodeURIComponent( key ) + '=' + encodeURIComponent( val ); |
| 758 | } |
| 759 | }) |
| 760 | .join('&'); |
| 761 | }, |
| 762 | |
| 763 | /** |
| 764 | * Get video iframe code from URL |
| 765 | */ |
| 766 | getVideo : function( url ) { |
| 767 | var iframe = '', |
| 768 | youtubeUrl = url.match( /((?:www\.)?youtube\.com|(?:www\.)?youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/ ), |
| 769 | youtubeShortUrl = url.match(/(?:www\.)?youtu\.be\/([a-zA-Z0-9\-_]+)/), |
| 770 | vimeoUrl = url.match( /(?:www\.)?vimeo\.com\/([0-9]*)/ ), |
| 771 | qs = ''; |
| 772 | if ( youtubeUrl || youtubeShortUrl) { |
| 773 | if ( youtubeShortUrl ) { |
| 774 | youtubeUrl = youtubeShortUrl; |
| 775 | } |
| 776 | qs = ui.parseUri( url, { |
| 777 | 'autoplay' : ( plugin.settings.autoplayVideos ? '1' : '0' ), |
| 778 | 'v' : '' |
| 779 | }); |
| 780 | iframe = '<iframe width="560" height="315" src="//' + youtubeUrl[1] + '/embed/' + youtubeUrl[2] + '?' + qs + '" frameborder="0" allowfullscreen></iframe>'; |
| 781 | |
| 782 | } else if ( vimeoUrl ) { |
| 783 | qs = ui.parseUri( url, { |
| 784 | 'autoplay' : ( plugin.settings.autoplayVideos ? '1' : '0' ), |
| 785 | 'byline' : '0', |
| 786 | 'portrait' : '0', |
| 787 | 'color': plugin.settings.vimeoColor |
| 788 | }); |
| 789 | iframe = '<iframe width="560" height="315" src="//player.vimeo.com/video/' + vimeoUrl[1] + '?' + qs + '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'; |
| 790 | |
| 791 | } else { |
| 792 | iframe = '<iframe width="560" height="315" src="' + url + '" frameborder="0" allowfullscreen></iframe>'; |
| 793 | } |
| 794 | |
| 795 | return '<div class="swipebox-video-container" style="max-width:' + plugin.settings.videoMaxWidth + 'px"><div class="swipebox-video">' + iframe + '</div></div>'; |
| 796 | }, |
| 797 | |
| 798 | /** |
| 799 | * Load image |
| 800 | */ |
| 801 | loadMedia : function ( src, callback ) { |
| 802 | // Inline content |
| 803 | if ( src.trim().indexOf('#') === 0 ) { |
| 804 | callback.call( |
| 805 | $('<div>', { |
| 806 | 'class' : 'swipebox-inline-container' |
| 807 | }) |
| 808 | .append( |
| 809 | $(src) |
| 810 | .clone() |
| 811 | .toggleClass( plugin.settings.toggleClassOnLoad ) |
| 812 | ) |
| 813 | ); |
| 814 | } |
| 815 | // Everything else |
| 816 | else { |
| 817 | if ( ! this.isVideo( src ) ) { |
| 818 | var img = $( '<img>' ).on( 'load', function() { |
| 819 | callback.call( img ); |
| 820 | } ); |
| 821 | |
| 822 | img.attr( 'src', src ); |
| 823 | } |
| 824 | } |
| 825 | }, |
| 826 | |
| 827 | /** |
| 828 | * Get next slide |
| 829 | */ |
| 830 | getNext : function () { |
| 831 | var $this = this, |
| 832 | src, |
| 833 | index = $( '#swipebox-slider .slide' ).index( $( '#swipebox-slider .slide.current' ) ); |
| 834 | if ( index + 1 < elements.length ) { |
| 835 | |
| 836 | src = $( '#swipebox-slider .slide' ).eq( index ).contents().find( 'iframe' ).attr( 'src' ); |
| 837 | $( '#swipebox-slider .slide' ).eq( index ).contents().find( 'iframe' ).attr( 'src', src ); |
| 838 | index++; |
| 839 | $this.setSlide( index ); |
| 840 | $this.preloadMedia( index+1 ); |
| 841 | } else { |
| 842 | |
| 843 | if ( plugin.settings.loopAtEnd === true ) { |
| 844 | src = $( '#swipebox-slider .slide' ).eq( index ).contents().find( 'iframe' ).attr( 'src' ); |
| 845 | $( '#swipebox-slider .slide' ).eq( index ).contents().find( 'iframe' ).attr( 'src', src ); |
| 846 | index = 0; |
| 847 | $this.preloadMedia( index ); |
| 848 | $this.setSlide( index ); |
| 849 | $this.preloadMedia( index + 1 ); |
| 850 | } else { |
| 851 | $( '#swipebox-overlay' ).addClass( 'rightSpring' ); |
| 852 | setTimeout( function() { |
| 853 | $( '#swipebox-overlay' ).removeClass( 'rightSpring' ); |
| 854 | }, 500 ); |
| 855 | } |
| 856 | } |
| 857 | }, |
| 858 | |
| 859 | /** |
| 860 | * Get previous slide |
| 861 | */ |
| 862 | getPrev : function () { |
| 863 | var index = $( '#swipebox-slider .slide' ).index( $( '#swipebox-slider .slide.current' ) ), |
| 864 | src; |
| 865 | if ( index > 0 ) { |
| 866 | src = $( '#swipebox-slider .slide' ).eq( index ).contents().find( 'iframe').attr( 'src' ); |
| 867 | $( '#swipebox-slider .slide' ).eq( index ).contents().find( 'iframe' ).attr( 'src', src ); |
| 868 | index--; |
| 869 | this.setSlide( index ); |
| 870 | this.preloadMedia( index-1 ); |
| 871 | } else { |
| 872 | $( '#swipebox-overlay' ).addClass( 'leftSpring' ); |
| 873 | setTimeout( function() { |
| 874 | $( '#swipebox-overlay' ).removeClass( 'leftSpring' ); |
| 875 | }, 500 ); |
| 876 | } |
| 877 | }, |
| 878 | |
| 879 | /** |
| 880 | * Close |
| 881 | */ |
| 882 | closeSlide : function () { |
| 883 | $( 'html' ).removeClass( 'swipebox-html' ); |
| 884 | $( 'html' ).removeClass( 'swipebox-touch' ); |
| 885 | $( window ).trigger( 'resize' ); |
| 886 | this.destroy(); |
| 887 | }, |
| 888 | |
| 889 | /** |
| 890 | * Destroy the whole thing |
| 891 | */ |
| 892 | destroy : function () { |
| 893 | $( window ).unbind( 'keyup' ); |
| 894 | $( 'body' ).unbind( 'touchstart' ); |
| 895 | $( 'body' ).unbind( 'touchmove' ); |
| 896 | $( 'body' ).unbind( 'touchend' ); |
| 897 | $( '#swipebox-slider' ).unbind(); |
| 898 | $( '#swipebox-overlay' ).remove(); |
| 899 | |
| 900 | if ( ! $.isArray( elem ) ) { |
| 901 | elem.removeData( '_swipebox' ); |
| 902 | } |
| 903 | |
| 904 | if ( this.target ) { |
| 905 | this.target.trigger( 'swipebox-destroy' ); |
| 906 | } |
| 907 | |
| 908 | $.swipebox.isOpen = false; |
| 909 | |
| 910 | if ( plugin.settings.afterClose ) { |
| 911 | plugin.settings.afterClose(); |
| 912 | } |
| 913 | } |
| 914 | }; |
| 915 | |
| 916 | plugin.init(); |
| 917 | }; |
| 918 | |
| 919 | $.fn.swipebox = function( options ) { |
| 920 | |
| 921 | if ( ! $.data( this, '_swipebox' ) ) { |
| 922 | var swipebox = new $.swipebox( this, options ); |
| 923 | this.data( '_swipebox', swipebox ); |
| 924 | } |
| 925 | return this.data( '_swipebox' ); |
| 926 | |
| 927 | }; |
| 928 | |
| 929 | }( window, document, jQuery ) ); |
| 930 |