DOM

smoth scrolling

利用动画间隔收集并translate滚动元素,相当于纯css实现的顺滑滚动。
虽然挺顺滑,但让人头昏。
https://tympanus.net/Tutorials/SmoothScrollAnimations/
https://tympanus.net/codrops/2019/07/10/how-to-add-smooth-scrolling-with-inner-image-animations-to-a-web-page/

  1. // SmoothScroll
  2. class SmoothScroll {
  3. constructor() {
  4. // the <main> element
  5. this.DOM = {main: document.querySelector('main')};
  6. // the scrollable element
  7. // we translate this element when scrolling (y-axis)
  8. this.DOM.scrollable = this.DOM.main.querySelector('div[data-scroll]');
  9. // the items on the page
  10. this.items = [];
  11. this.DOM.content = this.DOM.main.querySelector('.block--story');
  12. [...this.DOM.content.querySelectorAll('.block__item')].forEach(item => this.items.push(new Item(item)));
  13. // here we define which property will change as we scroll the page
  14. // in this case we will be translating on the y-axis
  15. // we interpolate between the previous and current value to achieve the smooth scrolling effect
  16. this.renderedStyles = {
  17. translationY: {
  18. // interpolated value
  19. previous: 0,
  20. // current value
  21. current: 0,
  22. // amount to interpolate
  23. ease: 0.1,
  24. // current value setter
  25. // in this case the value of the translation will be the same like the document scroll
  26. setValue: () => docScroll
  27. }
  28. };
  29. // set the body's height
  30. this.setSize();
  31. // set the initial values
  32. this.update();
  33. // the <main> element's style needs to be modified
  34. this.style();
  35. // init/bind events
  36. this.initEvents();
  37. // start the render loop
  38. requestAnimationFrame(() => this.render());
  39. }
  40. update() {
  41. // sets the initial value (no interpolation) - translate the scroll value
  42. for (const key in this.renderedStyles ) {
  43. this.renderedStyles[key].current = this.renderedStyles[key].previous = this.renderedStyles[key].setValue();
  44. }
  45. // translate the scrollable element
  46. this.layout();
  47. }
  48. layout() {
  49. this.DOM.scrollable.style.transform = `translate3d(0,${-1*this.renderedStyles.translationY.previous}px,0)`;
  50. }
  51. setSize() {
  52. // set the heigh of the body in order to keep the scrollbar on the page
  53. body.style.height = `${this.DOM.scrollable.scrollHeight}px`;
  54. }
  55. style() {
  56. // the <main> needs to "stick" to the screen and not scroll
  57. // for that we set it to position fixed and overflow hidden
  58. this.DOM.main.style.position = 'fixed';
  59. this.DOM.main.style.width = this.DOM.main.style.height = '100%';
  60. this.DOM.main.style.top = this.DOM.main.style.left = 0;
  61. this.DOM.main.style.overflow = 'hidden';
  62. }
  63. initEvents() {
  64. // on resize reset the body's height
  65. window.addEventListener('resize', () => this.setSize());
  66. }
  67. render() {
  68. // Get scrolling speed
  69. // Update lastScroll
  70. scrollingSpeed = Math.abs(docScroll - lastScroll);
  71. lastScroll = docScroll;
  72. // update the current and interpolated values
  73. for (const key in this.renderedStyles ) {
  74. this.renderedStyles[key].current = this.renderedStyles[key].setValue();
  75. this.renderedStyles[key].previous = MathUtils.lerp(this.renderedStyles[key].previous, this.renderedStyles[key].current, this.renderedStyles[key].ease);
  76. }
  77. // and translate the scrollable element
  78. this.layout();
  79. // for every item
  80. for (const item of this.items) {
  81. // if the item is inside the viewport call it's render function
  82. // this will update item's styles, based on the document scroll value and the item's position on the viewport
  83. if ( item.isVisible ) {
  84. if ( item.insideViewport ) {
  85. item.render();
  86. }
  87. else {
  88. item.insideViewport = true;
  89. item.update();
  90. }
  91. }
  92. else {
  93. item.insideViewport = false;
  94. }
  95. }
  96. // loop..
  97. requestAnimationFrame(() => this.render());
  98. }
  99. }

HTTP

POST & GET

GET 的 URL 会存在浏览器历史及WEB服务器日志里;
POST 不会;

GET应该用于处理,只读的操作,无副作用(操作数据库)。这样请求可被CDN缓存。
POST则可用于带副作用的操作。

所以,只取值,用GET,其他用POST。既省资源也安全。