Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
  <div class="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
</body>
              
            
!

CSS

              
                .container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.box {
  width: 500px;
  height: 500px;
}
              
            
!

JS

              
                /*
     * @description Interval 내 반복되는 이벤트를 무시함.
     * @param {function} f 디바운싱 처리할 로직이 정의된 함수.
     * @param {number} interval 무시할 시간 구간 
     * @returns {function}
     */
const debounce = (f, interval = 30) => {
  let now = null;

  return (...param) => {
    if (now) clearTimeout(now);

    now = setTimeout(() => {
      f(...param);
    }, interval);
  };
};
/*
     * @description 연이은 이벤트의 Interval 단위 실행을 보장.
     * @param {function} f 쓰로틀링 처리할 로직이 정의된 함수.
     * @param {number} interval 실행 주기 
     * @returns {function}
     */
const throttling = (f, interval = 300) => {
  let isPending = false;

  return (...param) => {
    if (!isPending) {
      isPending = !!setTimeout(() => {
        f(...param);
        isPending = false;
      }, interval);
    }
  };
};

const boxes = document.getElementsByClassName("box");
const fix = (f, ...args) => () => f(...args);

const DIRECTION = {
  X: 1,
  Y: 2,
  BOTH: 3
};

/*
     * @description 해당 요소가 viewport 안에 위치해있는지 확인한다.
     * @param {HTMLElement} element 대상 요소
     * @param {number} viewablePercent 처리를 시작할 요소의 크기 비율.
     * @param {DIRECTION} direction 처리할 방향을 지정합니다 (기본값 : DIRECTION.BOTH)
     * @returns {Boolean}
     */
const isInViewport = (
  element,
  viewablePercent = 0.5,
  direction = DIRECTION.BOTH
) => {
  const viewportHeight = window.innerHeight;
  const viewportWidth = window.innerWidth;

  const domRect = element.getBoundingClientRect();
  const { height, width, top, left } = domRect;

  let resultX = false;
  let resultY = false;

  if (direction & DIRECTION.X) {
    const viewableSizeX = width * viewablePercent;
    const startPointX = viewableSizeX - width;
    const endPointX = viewportWidth - viewableSizeX;

    resultX = startPointX <= left && endPointX > left;
  }

  if (direction & DIRECTION.Y) {
    const viewableSizeY = height * viewablePercent;
    const startPointY = viewableSizeY - height;
    const endPointY = viewportHeight - viewableSizeY;

    resultY = startPointY <= top && endPointY > top;
  }

  return direction === DIRECTION.BOTH ? resultX && resultY : resultY || resultX;
};

/*
     * @description nodeList의 요소들이 각각 viewPort에 진입하기 전까지 특정한 처리를 지연한다.
     * @param {NodeList} nodeList 처리할 대상이 되는 요소.
     * @param {function} f 처리할 로직이 정의된 함수.
     * @param {DIRECTION} direction 처리할 방향을 지정합니다 (기본값 : DIRECTION.BOTH)
     * @param {number} viewablePercent 처리를 시작할 요소의 크기 비율.
     */
const lazyLoading = (
  nodeList,
  f,
  viewablePercent = 0.3,
  direction = DIRECTION.BOTH
) => {
  const targetList = Array.from(nodeList);
  const result = targetList
    .filter(element => isInViewport(element, viewablePercent, direction))
    .forEach(element => {
      f(element);
    });

  return result;
};

const handler = fix(
  lazyLoading, // lazyLoading 처리를 한다
  boxes, // box 요소들에 대해
  v => {
    if (!v.classList.contains("viewable")) {
      v.classList.add("viewable"); // viewable 클래스를 추가한다
      const domRect = v.getBoundingClientRect();
      const round = Math.round;
      const width = round(domRect.width + Math.random() * 10);
      const height = round(domRect.height + Math.random() * 10);

      v.style.backgroundImage = `url(http://placeimg.com/${width}/${height})`;
      v.style.backgroundSize = "cover";
    }
  },
  0.5, // 요소의 50%가 보였을 때
  DIRECTION.Y
); // X축만 확인함

handler();
window.addEventListener("scroll", throttling(handler, 300));

              
            
!
999px

Console