Giter VIP home page Giter VIP logo

scrollsample's Introduction

Infinite Scroll

使用技術

這次使用 vite 做開發, UI 框架使用 Tailwind

使用 compensation api 做邏輯拆分

重點程式碼


const calcDisplay = () => {
  // 實際顯示的高度=可視視窗高度 + 滾動距離 - 上方 navbar 高度
  const height = window.innerHeight + window.scrollY - 152;

  // 252 為卡片的高度
  const num = Math.floor(height / 252);

  // 目前可實際顯示卡片的數量 = 一行容納數量 * 一列容納數量
  const total = size.value * num;

  if (displayData.value.length === data.value.length) return;

  for (let j = displayData.value.length; j < total; j += 1) {
    const item = data.value[j];
    displayData.value.push(item);
  }
};
// 使用 setTimeout 進行節流 ,避免使用者短時間一直觸發消耗效能
const debounce = (func, delay) => {
  let timer;
  return () => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      func();
    }, delay);
  };
};
onMounted(() => {
  addEventListener("scroll", debounce(calcDisplay, 250));
});

Coding Style

  • Vue 3 Script Setup
  • Vue SFC (由上而下排列)
    • script: <script setup>
    • template: <template>
    • style: <style scoped>

Vite 插件功能

開發

# install deps
npm install

# run dev server
npm run dev

# run build
npm run build

scrollsample's People

Contributors

jason06286 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.