ios-parallax.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. (function($){
  2. $.iosParallax = function(el, options){
  3. // To avoid scope issues, use 'base' instead of 'this'
  4. // to reference this class from internal events and functions.
  5. var base = this;
  6. // Access to jQuery and DOM versions of element
  7. base.$el = $(el);
  8. base.el = el;
  9. // Add a reverse reference to the DOM object
  10. base.$el.data("iosParallax", base);
  11. var centerCoordinates = {x: 0, y: 0};
  12. var targetCoordinates = {x: 0, y: 0};
  13. var transitionCoordinates = {x: 0, y: 0};
  14. function getBackgroundImageUrl(){
  15. var backgroundImage = base.$el.css('background-image').match(/url\(.*\)/ig);
  16. if ( ! backgroundImage || backgroundImage.length < 1) {
  17. throw 'No background image found';
  18. }
  19. return backgroundImage[0].replace(/url\(|'|"|'|"|\)$/ig, "");
  20. }
  21. function getBackgroundImageSize(){
  22. var img = new Image;
  23. img.src = getBackgroundImageUrl();
  24. return {width: img.width, height: img.height};
  25. }
  26. function setCenterCoordinates(){
  27. var bgImgSize = getBackgroundImageSize();
  28. centerCoordinates.x = -1 * Math.abs(bgImgSize.width - base.$el.width()) / 2;
  29. centerCoordinates.y = -1 * Math.abs(bgImgSize.height - base.$el.height()) / 2;
  30. targetCoordinates.x = centerCoordinates.x;
  31. targetCoordinates.y = centerCoordinates.y;
  32. transitionCoordinates.x = centerCoordinates.x;
  33. transitionCoordinates.y = centerCoordinates.y;
  34. }
  35. function bindEvents(){
  36. base.$el.mousemove(function(e){
  37. var width = base.options.movementFactor / base.$el.width();
  38. var height = base.options.movementFactor / base.$el.height();
  39. var cursorX = e.pageX - ($(window).width() / 2);
  40. var cursorY = e.pageY - ($(window).height() / 2);
  41. targetCoordinates.x = width * cursorX * -1 + centerCoordinates.x;
  42. targetCoordinates.y = height * cursorY * -1 + centerCoordinates.y;
  43. });
  44. // Slowly converge the background image position to the target coordinates in 60 FPS
  45. var loop = setInterval(function(){
  46. transitionCoordinates.x += ((targetCoordinates.x - transitionCoordinates.x) / base.options.dampenFactor);
  47. transitionCoordinates.y += ((targetCoordinates.y - transitionCoordinates.y) / base.options.dampenFactor);
  48. base.$el.css("background-position", transitionCoordinates.x+"px "+transitionCoordinates.y+"px");
  49. }, 16);
  50. $(window).resize(function(){
  51. // Re-center the image
  52. setCenterCoordinates();
  53. });
  54. // There's a problem with getting image height and width when the image isn't loaded.
  55. var img = new Image;
  56. img.src = getBackgroundImageUrl();
  57. $(img).load(function(){
  58. setCenterCoordinates();
  59. });
  60. };
  61. base.init = function(){
  62. base.options = $.extend({}, $.iosParallax.defaultOptions, options);
  63. bindEvents();
  64. };
  65. base.init();
  66. };
  67. $.iosParallax.defaultOptions = {
  68. // How fast the background moves
  69. movementFactor: 50,
  70. // How much to dampen the movement (higher is slower)
  71. dampenFactor: 36
  72. };
  73. $.fn.iosParallax = function(options){
  74. return this.each(function(){
  75. (new $.iosParallax(this, options));
  76. });
  77. };
  78. })(jQuery);