jquery-inertiaScroll.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. jquery-inertiaScroll
  3. Copyright(c) 2017 Go Nishiduka
  4. This software is released under the MIT License.
  5. http://opensource.org/licenses/mit-license.php
  6. Last Update: 2017-10-23
  7. Version:1.1.1
  8. */
  9. 'use strict';
  10. (function($){
  11. $.fn.inertiaScroll = function(options){
  12. //////////////////////////////////////////////////
  13. // options
  14. //////////////////////////////////////////////////
  15. var settings = $.extend({
  16. parent: '',
  17. childDelta1: 0.02,
  18. childDelta2: 0.1,
  19. parentDelta: 0.08
  20. }, options);
  21. //////////////////////////////////////////////////
  22. // jqeury object
  23. //////////////////////////////////////////////////
  24. var $window = $(window);
  25. var $body = $('body');
  26. var $parent = settings.parent;
  27. var $child = this;
  28. //////////////////////////////////////////////////
  29. // inertiaScroll childClass
  30. //////////////////////////////////////////////////
  31. var ChildBox = function(elm, offset, speed, margin){
  32. this.elm = elm;
  33. this.offset = offset !== undefined ? offset : 0;
  34. this.speed = speed !== undefined ? speed : 1;
  35. this.margin = margin !== undefined ? margin : 0;
  36. };
  37. ChildBox.prototype.update = function(windowOffset,offsetBottom){
  38. this.offset += (windowOffset * settings.childDelta1 * Number(this.speed) - this.offset) * settings.childDelta2;
  39. this.elm.css({transform:'translate3d(' + 0 + ',' + ( Number(this.margin) - Number(this.offset) ) + 'px ,' + 0 +')'});
  40. };
  41. //////////////////////////////////////////////////
  42. // inertiaScroll parentClass
  43. //////////////////////////////////////////////////
  44. var ParentBox = function(elm, offset, speed, margin){
  45. ChildBox.apply(this,arguments);
  46. };
  47. ParentBox.prototype = Object.create(ChildBox.prototype,{
  48. constructor:{
  49. value: ParentBox
  50. }
  51. });
  52. ParentBox.prototype.update = function(windowOffset){
  53. this.offset += (windowOffset - this.offset) * settings.parentDelta;
  54. this.elm.css({transform:'translate3d(' + 0 + ',' + (-this.offset) + 'px ,' + 0 + ')'});
  55. };
  56. ParentBox.prototype.setcss = function(){
  57. this.elm.css({
  58. 'width':'100%',
  59. 'position':'fixed'
  60. });
  61. };
  62. //////////////////////////////////////////////////
  63. // make Object
  64. //////////////////////////////////////////////////
  65. var Boxes = function(){
  66. this.ChildBox = [];
  67. this.ChildBoxLength = 0;
  68. this.ParentBox = '';
  69. this.windowHeight = 0;
  70. };
  71. Boxes.prototype = {
  72. init:function(){
  73. this.createElm($child,$parent);
  74. this.loop();
  75. },
  76. createElm:function(child,parent){
  77. this.ParentBox = new ParentBox(parent,0,1);
  78. this.ParentBox.setcss();
  79. this.boxArrayLength = child.length;
  80. for (var i = 0; i < this.boxArrayLength; i++) {
  81. var e = child.eq(i);
  82. var speed = e.data('speed');
  83. var margin = e.data('margin');
  84. this.ChildBox.push(new ChildBox(e,0,speed,margin));
  85. }
  86. },
  87. smoothScroll:function(){
  88. var windowOffset = $window.scrollTop();
  89. var offsetBottom = windowOffset + this.windowHeight;
  90. this.ParentBox.update(windowOffset);
  91. for (var i = 0; i < this.boxArrayLength; i++) {
  92. this.ChildBox[i].update(windowOffset,offsetBottom);
  93. }
  94. },
  95. loop:function(){
  96. this.smoothScroll();
  97. window.requestAnimationFrame(this.loop.bind(this));
  98. }
  99. };
  100. //////////////////////////////////////////////////
  101. // Done
  102. //////////////////////////////////////////////////
  103. $(function(){
  104. $body.height($parent.height());
  105. var boxes = new Boxes();
  106. boxes.init();
  107. });
  108. return this;
  109. };
  110. })(jQuery);