JS实现加载更多

有两种方式

方式一:第一步:将逻辑抽取封装在hooks - > useScroll.js中

import { onMounted, onUnmounted } from "vue";

export default function useScroll(reachBottomCB) {
  const scrollListenerHandler = () => {
    const clientHeight = document.documentElement.clientHeight;
    const scrollTop = document.documentElement.scrollTop;
    const scrollHeight = document.documentElement.scrollHeight;
    console.log("-------");
    if (clientHeight + scrollTop >= scrollHeight) {
      console.log("滚动到底部了");
      if (reachBottomCB) reachBottomCB();
    }
  };

  onMounted(() => {
    window.addEventListener("scroll", scrollListenerHandler);
  });

  onUnmounted(() => {
    window.removeEventListener("scroll", scrollListenerHandler);
  });
}

第二步在组件中引入和使用

import useScroll from "@/hooks/useScroll.js";

//使用
useScroll(() => {
  homeStore.fetchHouselistDate();
});

方法二:

第一步:将逻辑抽取封装在hooks - > useScroll.js中

// 第二种方式
export default function useScroll() {
  const isReachBottom = ref(false);
  const scrollTop = ref(0);
  const clientHeight = ref(0);
  const scrollHeight = ref(0);

  const scrollListenerHandler = () => {
    clientHeight.value = document.documentElement.clientHeight;
    scrollTop.value = document.documentElement.scrollTop;
    scrollHeight.value = document.documentElement.scrollHeight;
    if (clientHeight.value + scrollTop.value >= scrollHeight.value) {
      console.log("滚动到底部了");
      isReachBottom.value = true;
    }
  };
  onMounted(() => {
    window.addEventListener("scroll", scrollListenerHandler);
  });

  onUnmounted(() => {
    window.removeEventListener("scroll", scrollListenerHandler);
  });
  return { isReachBottom, clientHeight, scrollTop, scrollHeight };
}

第二步在组件中引入和使用

import { watch } from "vue";
import useScroll from "@/hooks/useScroll.js";

// 加载更多第二种写法
const { isReachBottom } = useScroll();
watch(isReachBottom, (newValue) => {
  if (newValue) {
    homeStore.fetchHouselistDate().then(() => {
      isReachBottom.value = false;
    });
  }
});

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在VSCode中实现加载更多的功能,可以使用JavaScript实现。可以通过监听滚动事件来触发加载更多的操作。 首先,你需要在HTML文件中引入相应的JavaScript代码。你可以从VSCode官方网站上下载VSCode的最新版本,并将它放在你的项目中。下载地址是。然后,在HTML文件中插入这段代码。 接下来,你需要编写JavaScript代码来实现滚动加载更多的功能。你可以使用addEventListener()方法来监听滚动事件,一旦滚动到底部,就触发加载更多的操作。你可以根据你的具体需求编写相应的加载逻辑。 如果你对JavaScript的滚动加载更多功能还有疑问,你可以访问获取更多信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vsCode首次加载Vue项目](https://blog.csdn.net/qq_22325259/article/details/119430148)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【JavaScript源代码】JavaScript实现滚动加载更多.docx](https://download.csdn.net/download/mmoo_python/72080335)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [vscode调试无法加载类怎么办](https://blog.csdn.net/weixin_28724319/article/details/114661252)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值