jquery-inertiaScroll.js 3.6 KB

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