JS 自动滚动页面到底部

function getScrollTop() {
    var scrollTop = 0,
        bodyScrollTop = 0,
        documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop =
        bodyScrollTop - documentScrollTop > 0
            ? bodyScrollTop
            : documentScrollTop;
    return scrollTop;
}

function getScrollHeight() {
    var scrollHeight = 0,
        bodyScrollHeight = 0,
        documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight =
        bodyScrollHeight - documentScrollHeight > 0
            ? bodyScrollHeight
            : documentScrollHeight;
    return scrollHeight;
}

function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}

//
function autoScroll() {
    if (getScrollTop() + getWindowHeight() < getScrollHeight()) {
        window.scrollTo({
            top: window.scrollY + 5,
            behavior: "smooth",
        });
        var timerId = setTimeout(autoScroll, 50);
        // stop when click
        document.addEventListener("click", (event) => {
            window.clearInterval(timerId);
        });
    }
}
autoScroll();

通过浏览器标签(Bookmarklet方式)调用,拖拽下面的链接到浏览器标签栏:

git 统计仓库中代码行数

列出每个文件行数,以及总行数
git ls-files | xargs wc -l

指定文件夹
git ls-files src lib | xargs wc -l
以上示例是指定仓库中 lib 文件夹

过滤文件夹或文件类型
 git ls-files | grep -Ev 'examples|.txt' | xargs wc -l
以上示例是过滤 examples 文件夹和所有 .txt 文件

Python 取列表共有元素(交集)

交集:对于给定的两个集合,返回一个包含两个集合中共有元素的新集合。

使用 Python 元组数据类型 set 来实现交集操作。

a = [1, 2, 3]
b = [3, 4, 5]
c = set(a).intersection(set(b))
print(list(c))
# [3]

AWS Amplify 删除 APP失败之 Invoking remove all backends execution failed

原因:有 backend environment,需要先删除

一般这时通过 Web Console 删除后端环境也会失败。可以通过 AWS CLI 来删除:
aws amplify delete-backend-environment --app-id YOUR-APP-ID --environment-name ENV-NAME --region YOR-REGION-NAME

  • APP ID 查寻方式:
    App settings -> General, App ARN 的最后部分。例如:
    arn:aws:amplify:ap-southeast-1:YOUR-ACCOUNT-ID:apps/APP-ID

CSS 禁止元素响应鼠标事件

当我们需要某些 DOM 元素不响应鼠标事件时,例如点击(click) 悬停(hover)等, 可以使用 CSS 属性直接实现,方便快捷。

CSS 方法:

div {
	pointer-events: none;
}

如果项目使用 tailwindcss, 可以使用类名: pointer-events-none

CSS 如何实现开关按钮(switch)

/attachments/3c3c91087455ce213729ff00352ce488.png

推荐使用现成的组件,不用重复造轮子。

  • Next.js 可以用 [nextui-org[(https://nextui.org/docs/components/switch)
  1. (关键)使用 <Label> 关联 <Input type=checkbox>,响应鼠标操作
    方法一:按照固定层级:<label><input /> <span or any other elment> </label>
    方法二:使用 labelfor 属性等于 input 的ID

Python 当前工作目录的路径

获取程序运行的工作目录。

  • 当前工作目录的相对路径
    os.curdir,是个常量,Unix-like 系统下是 .

  • 当前工作目录的绝对路径
    os.getcwd()os.path.abspath(os.curdir)

  • 当前工作目录的上级(父)目录的路径
    os.path.dirname(os.getcwd())