scroll-event.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width" />
  6. <title>scroll event</title>
  7. <link rel="stylesheet" href="../css/flickity.css" />
  8. <style>
  9. * { box-sizing: border-box; }
  10. html { overflow-y: scroll; }
  11. .carousel {
  12. border: 1px solid;
  13. margin-bottom: 40px;
  14. /* width: 1000px;*/
  15. }
  16. .carousel__cell {
  17. /* width: 25%;*/
  18. width: 32%;
  19. height: 200px;
  20. background: #BDF;
  21. margin-left: 1%;
  22. margin-right: 1%;
  23. font-size: 40px;
  24. }
  25. /*.carousel__cell--width2 { width: 40%; }
  26. .carousel__cell--width3 { width: 80%; }*/
  27. .carousel__cell--width2 { width: 66%; }
  28. .carousel__cell--width3 { width: 100%; }
  29. .carousel__cell.is-selected {
  30. background: #68F;
  31. }
  32. .progress-bar {
  33. height: 10px;
  34. width: 0;
  35. background: #333;
  36. }
  37. @media (min-width: 1000px) {
  38. /* fit four */
  39. .carousel__cell { width: 23.5%; }
  40. .carousel__cell--width2 { width: 49%; }
  41. .carousel__cell--width3 { width: 74.5%; }
  42. }
  43. /* ---- parallax ---- */
  44. .parallax {
  45. margin-top: 100px;
  46. position: relative;
  47. overflow-x: hidden;
  48. padding-bottom: 40px;
  49. }
  50. .parallax__carousel__cell {
  51. background: hsla(0, 100%, 50%, 0.4);
  52. height: 220px;
  53. width: 50%;
  54. margin: 40px 5%;
  55. }
  56. .parallax__layer {
  57. position: absolute;
  58. left: 0;
  59. top: 0;
  60. }
  61. .parallax__layer--bg {
  62. top: 70px;
  63. width: 80%;
  64. height: 160px;
  65. }
  66. .parallax__layer--fg {
  67. pointer-events: none;
  68. width: 125%;
  69. height: 300px;
  70. }
  71. .parallax__layer__cell {
  72. position: absolute;
  73. width: 50%;
  74. margin: 0 5%;
  75. height: 100%;
  76. }
  77. .parallax__layer__cell:nth-child(1) { left: 0%; }
  78. .parallax__layer__cell:nth-child(2) { left: 60%; }
  79. .parallax__layer__cell:nth-child(3) { left: 120%; }
  80. .parallax__layer__cell:nth-child(4) { left: 180%; }
  81. .parallax__layer__cell:nth-child(5) { left: 240%; }
  82. .parallax__layer__cell--bg {
  83. background: hsla(210, 100%, 50%, 0.4);
  84. }
  85. .parallax__layer__cell--fg {
  86. background: hsla(60, 100%, 50%, 0.4);
  87. }
  88. /* ---- ---- */
  89. .image-carousel__cell {
  90. margin-right: 20px;
  91. overflow: hidden;
  92. }
  93. .image-carousel__cell img {
  94. display: block;
  95. height: 400px;
  96. }
  97. </style>
  98. </head>
  99. <body>
  100. <h1>scroll event</h1>
  101. <div class="carousel carousel1">
  102. <div class="carousel__cell carousel__cell--width2">0</div>
  103. <div class="carousel__cell">1</div>
  104. <div class="carousel__cell">2</div>
  105. <div class="carousel__cell carousel__cell--width2">3</div>
  106. <div class="carousel__cell carousel__cell--width2">4</div>
  107. <div class="carousel__cell">5</div>
  108. <div class="carousel__cell">6</div>
  109. <div class="carousel__cell">7</div>
  110. <div class="carousel__cell carousel__cell--width3">8</div>
  111. <div class="carousel__cell">9</div>
  112. <div class="carousel__cell">10</div>
  113. </div>
  114. <div class="progress-bar"></div>
  115. <div class="parallax">
  116. <div class="parallax__layer parallax__layer--bg">
  117. <div class="parallax__layer__cell parallax__layer__cell--bg"></div>
  118. <div class="parallax__layer__cell parallax__layer__cell--bg"></div>
  119. <div class="parallax__layer__cell parallax__layer__cell--bg"></div>
  120. <div class="parallax__layer__cell parallax__layer__cell--bg"></div>
  121. <div class="parallax__layer__cell parallax__layer__cell--bg"></div>
  122. </div>
  123. <div class="parallax__carousel">
  124. <div class="parallax__carousel__cell"></div>
  125. <div class="parallax__carousel__cell"></div>
  126. <div class="parallax__carousel__cell"></div>
  127. <div class="parallax__carousel__cell"></div>
  128. <div class="parallax__carousel__cell"></div>
  129. </div>
  130. <div class="parallax__layer parallax__layer--fg">
  131. <div class="parallax__layer__cell parallax__layer__cell--fg"></div>
  132. <div class="parallax__layer__cell parallax__layer__cell--fg"></div>
  133. <div class="parallax__layer__cell parallax__layer__cell--fg"></div>
  134. <div class="parallax__layer__cell parallax__layer__cell--fg"></div>
  135. <div class="parallax__layer__cell parallax__layer__cell--fg"></div>
  136. </div>
  137. </div>
  138. <div class="image-carousel">
  139. <div class="image-carousel__cell"><img src="http://i.imgur.com/r8p3Xgq.jpg" /></div>
  140. <div class="image-carousel__cell"><img src="http://i.imgur.com/q9zO6tw.jpg" /></div>
  141. <div class="image-carousel__cell"><img src="http://i.imgur.com/bwy74ok.jpg" /></div>
  142. <div class="image-carousel__cell"><img src="http://i.imgur.com/hODreXI.jpg" /></div>
  143. <div class="image-carousel__cell"><img src="http://i.imgur.com/UORFJ3w.jpg" /></div>
  144. <div class="image-carousel__cell"><img src="http://i.imgur.com/bAZWoqx.jpg" /></div>
  145. <div class="image-carousel__cell"><img src="http://i.imgur.com/PgmEBSB.jpg" /></div>
  146. <div class="image-carousel__cell"><img src="http://i.imgur.com/aboaFoB.jpg" /></div>
  147. <div class="image-carousel__cell"><img src="http://i.imgur.com/LkmcILl.jpg" /></div>
  148. </div>
  149. <script src="../bower_components/get-size/get-size.js"></script>
  150. <script src="../bower_components/desandro-matches-selector/matches-selector.js"></script>
  151. <script src="../bower_components/ev-emitter/ev-emitter.js"></script>
  152. <script src="../bower_components/unipointer/unipointer.js"></script>
  153. <script src="../bower_components/unidragger/unidragger.js"></script>
  154. <script src="../bower_components/fizzy-ui-utils/utils.js"></script>
  155. <script src="../js/cell.js"></script>
  156. <script src="../js/slide.js"></script>
  157. <script src="../js/animate.js"></script>
  158. <script src="../js/flickity.js"></script>
  159. <script src="../js/drag.js"></script>
  160. <script src="../js/prev-next-button.js"></script>
  161. <script src="../js/page-dots.js"></script>
  162. <script src="../js/player.js"></script>
  163. <script src="../js/add-remove-cell.js"></script>
  164. <script src="../js/lazyload.js"></script>
  165. <script>
  166. var flkty = new Flickity( '.carousel1', {
  167. initialIndex: 2,
  168. // groupCells: true,
  169. // wrapAround: true,
  170. // cellAlign: 'right'
  171. });
  172. var progressBar = document.querySelector('.progress-bar');
  173. flkty.on( 'scroll', function( progress ) {
  174. console.log( progress );
  175. var width = Math.max( 0, Math.min( 1, progress ) );
  176. progressBar.style.width = width * 100 + '%';
  177. });
  178. flkty.reposition();
  179. // ----- ----- //
  180. var paraBG = document.querySelector('.parallax__layer--bg');
  181. var paraFG = document.querySelector('.parallax__layer--fg');
  182. var paraFlkty = new Flickity( '.parallax__carousel', {
  183. });
  184. var cellRatio = 0.6;
  185. var bgRatio = 0.8;
  186. var fgRatio = 1.25;
  187. paraFlkty.on( 'scroll', function( progress ) {
  188. // console.log( progress );
  189. paraBG.style.left = ( 0.5 - ( 0.5 + progress * 4 ) * cellRatio * bgRatio ) * 100 + '%';
  190. paraFG.style.left = ( 0.5 - ( 0.5 + progress * 4 ) * cellRatio * fgRatio ) * 100 + '%';
  191. });
  192. paraFlkty.reposition();
  193. // ----- ----- //
  194. var imgFlkty = new Flickity( '.image-carousel', {
  195. });
  196. window.onload = function() {
  197. imgFlkty.reposition();
  198. }
  199. var imgs = document.querySelectorAll('.image-carousel img');
  200. imgFlkty.on( 'scroll', function( progress ) {
  201. imgFlkty.slides.forEach( function( slide, i ) {
  202. var img = imgs[i];
  203. var x = ( slide.target + imgFlkty.x ) * -0.333;
  204. img.style.transform = 'translateX( ' + x + 'px)';
  205. });
  206. });
  207. </script>
  208. </body>
  209. </html>