jegtheme

jparalax

Mar 17th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. /** paralax plugin **/
  2. (function($) {
  3. $.fn.jparalax = function ( options, startcallback, finishcallback ) {
  4. var $this = this;
  5. var settings = {
  6. effect : 'fixedbackground',
  7. speed : 100,
  8. container : window
  9. };
  10.  
  11. startcallback = startcallback ? startcallback : [];
  12. finishcallback = finishcallback ? finishcallback : [];
  13.  
  14. var options = $.extend( {}, settings, options );
  15.  
  16. return this.each(function (i) {
  17. if($(this).attr('data-parent')) {
  18. var parent = "#" + $(this).attr('data-parent');
  19. var element = $(parent);
  20. var child = $(this);
  21. var childpos = $(child).position();
  22.  
  23. /** data center **/
  24. if($(child).attr('data-top') == 'center'){
  25. var datatop = ( $(window).height() - $(child).height()) / 2;
  26. $(child).attr('data-top', datatop).css("top", datatop + "px");
  27. }
  28. } else {
  29. var element = $(this);
  30. }
  31.  
  32. var position = $(element).offset();
  33. var o = options;
  34. var $container = $(o.container);
  35. var speed = o.speed;
  36. var effect = o.effect;
  37. var bgpos = $(element).css('backgroundPosition').split(" ");
  38.  
  39. var useragent = detectbrowser();
  40.  
  41.  
  42.  
  43. function detectbrowser() {
  44. if (navigator.userAgent.indexOf('Firefox') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Firefox') + 8)) >= 3.6){//Firefox
  45. return "firefox";
  46. } else if (navigator.userAgent.indexOf('Chrome') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Chrome') + 7).split(' ')[0]) >= 15){//Chrome
  47. return "chrome";
  48. } else if(navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Version') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Version') + 8).split(' ')[0]) >= 5){//Safari
  49. return "safari";
  50. } else{
  51. return "unknown";
  52. }
  53. }
  54.  
  55. function calcpos() {
  56. var minpos = position.top - $container.height();
  57. var maxpos = position.top + $(element).height() + $('nav').height();
  58.  
  59. return {
  60. 'min' : minpos,
  61. 'max' : maxpos,
  62. 'diff' : ( maxpos - minpos )
  63. };
  64. }
  65.  
  66. function fixedbackground(ratio, scrollpos, dpos){
  67. var bgposresult = parseInt(bgpos[1]) - ( ratio * speed );
  68. var newposition = bgpos[0] + " " + bgposresult + "px";
  69. $(element).css('background-position', newposition);
  70. }
  71.  
  72. function staytop(scrollpos, ratio, dpos) {
  73.  
  74. var thistop = scrollpos - position.top + parseInt($(child).attr("data-top")) + $('nav').height();
  75. thistop = ( thistop - ( ratio * speed ) ) + "px";
  76. $(child).css("top", thistop);
  77.  
  78. /*
  79. if( scrollpos > ( position.top - $('nav').height() ) ) {
  80. var unknownvar = ( ( position.top - $('nav').height() ) / dpos.diff ) * speed;
  81. var thistop = ( scrollpos - position.top + parseInt($(child).attr("data-top")) + $('nav').height() + unknownvar );
  82. thistop = ( thistop - ( ratio * speed ) ) + "px";
  83. $(child).css("top", thistop);
  84. } else {
  85. $(child).css("top", $(child).attr("data-top") + "px");
  86. }
  87. */
  88. }
  89.  
  90. function staytopfixed(scrollpos, ratio, dpos) {
  91. if( scrollpos > ( position.top - $('nav').height() )) {
  92. } else {
  93. $(child).css("top", $(child).attr("data-top") + "px").css("position", "relative");
  94. }
  95. }
  96.  
  97. function doparalax() {
  98. var dpos = calcpos();
  99. var scrollpos = $(window).scrollTop();
  100.  
  101. if( scrollpos >= dpos.min && scrollpos <= dpos.max) {
  102. if(effect == 'fixedbackground') {
  103. var ratio = ( scrollpos - dpos.min ) / dpos.diff;
  104. fixedbackground(ratio, scrollpos, dpos);
  105. } else if (effect = 'staytop') {
  106. var ratio = ( scrollpos - dpos.min ) / dpos.diff;
  107.  
  108. if(useragent == "firefox") {
  109. staytop(scrollpos, ratio, dpos);
  110. } else {
  111. staytopfixed(scrollpos, ratio, dpos);
  112. }
  113. }
  114. }
  115. }
  116.  
  117.  
  118. $container.bind('scroll load resize', function(e){
  119. doparalax();
  120. });
  121. });
  122. };
  123. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment