/sites/all/modules/parallax/parallax_admin/parallax_admin.js

https://gitlab.com/leoplanxxi/dr7-web-buap-2016 · JavaScript · 119 lines · 73 code · 19 blank · 27 comment · 14 complexity · 9a624aa4c9e5381ba32df6cacea63065 MD5 · raw file

  1. /**
  2. * @file
  3. * Enable Parallax effect for any block created by the user
  4. *
  5. * This module enables the user to select none, Same, or Opposite directions
  6. * when creating a block. Selecting Same or Opposite places a data attribute
  7. * which is ready by the attached javascript file, and then targeted for
  8. * calculations causing parallax effect based on 'same' or 'opposite' value.
  9. */
  10. (function ($, Drupal, window, document, undefined) {
  11. Drupal.behaviors.parallax_admin = {
  12. attach: function(context, settings) {
  13. $(function() {
  14. // Enable parallax only if not mobile device
  15. if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) === false) {
  16. enableParallax(parallaxItems);
  17. }
  18. });
  19. }
  20. };
  21. // This function currently accepts an array of target selectors in the form
  22. // of strings, cycled through for parallax effects.
  23. function enableParallax(targetsObj) {
  24. $.each(targetsObj, function() {
  25. var currentObj = this;
  26. // check if object exists on page
  27. if ($(currentObj[0]).length > 0) {
  28. $(window).load(function() {
  29. parallaxCalculation(currentObj);
  30. });
  31. $(window).scroll(function() {
  32. parallaxCalculation(currentObj);
  33. });
  34. $(window).resize(function() {
  35. parallaxCalculation(currentObj);
  36. });
  37. }
  38. });
  39. }
  40. // Determines if each element passed to enableParallax is in the viewport and
  41. // passes through TRUE when element dimensions first are visible in viewport
  42. function isScrolledIntoView(elem) {
  43. var docViewTop = $(window).scrollTop();
  44. var docViewBottom = docViewTop + $(window).height();
  45. if (elem.offset() !== null) {
  46. var elemTop = elem.offset().top;
  47. var elemBottom = elemTop + elem.height();
  48. return ((elemTop <= docViewBottom) && (elemBottom >= docViewTop));
  49. } else {
  50. return true;
  51. }
  52. }
  53. function parallaxCalculation(currentObj) {
  54. var $currentSelector = $(currentObj[0]);
  55. var verticalValue = currentObj[1];
  56. var horizontalValue = currentObj[2];
  57. if (isScrolledIntoView($currentSelector)) { //On Scroll or Load, check if object is in view
  58. // Calculate position of top of target element relative to top of page
  59. // and height of target element
  60. var divTop = $currentSelector.offset().top;
  61. var divHeight = $currentSelector.height();
  62. // Calculate distance scrolled from top, the center of the viewport
  63. // (necessary for calculations), and the height of the viewport
  64. var docViewTop = $(window).scrollTop();
  65. var docViewSize = $(window).height();
  66. var docViewCenter = docViewTop + (docViewSize / 2);
  67. // Calculate the full range of effective parallax movement on the page
  68. var fullRange = docViewSize + divHeight;
  69. // My head hurts too much to explain this one. Ask Kyle.
  70. var currPosition = docViewCenter - (divTop - (docViewSize / 2));
  71. // Calculate percentage location of current position baseed on full range of parallax movement
  72. var currVerticalPercentage = (currPosition / fullRange) * 100;
  73. var currHorizontalPercentage = (currPosition / fullRange) * 100;
  74. // Determines if vertical parallax direction is opposite, and invertes the value
  75. // on a scale of 1-100
  76. if (verticalValue.indexOf("bottom-to-top") > -1) {
  77. $currentSelector.attr("data-position-vertical", currVerticalPercentage);
  78. currVerticalPercentage = 100 - $currentSelector.attr("data-position-vertical");
  79. }
  80. if (horizontalValue.indexOf("right-to-left") > -1) {
  81. $currentSelector.attr("data-position-horizontal", currHorizontalPercentage);
  82. currHorizontalPercentage = 100 - $currentSelector.attr("data-position-horizontal");
  83. }
  84. // Horizontal Background Percentage
  85. if (horizontalValue != "none") {
  86. coords = currHorizontalPercentage + "%";
  87. } else {
  88. coords = "50%";
  89. }
  90. // Vertical Background Percentage
  91. if (verticalValue != "none") {
  92. coords += " " + currVerticalPercentage + "%";
  93. } else {
  94. coords += " 50%";
  95. }
  96. // Apply background Position and apply background-size: Cover to avoid image tiling
  97. $currentSelector.css({ "backgroundPosition": coords });
  98. }
  99. }
  100. })(jQuery, Drupal, this, this.document);