我在CSS和JavaScript的加载顺序之间遇到了一点小问题.

基本上,正在发生的事情是,我有一个蓝色背景的div,我的css应用了一个"剪切",对角切片.当页面第一次加载时(或CTRL + CTRL + R),"cut"在很短但很明显的持续时间内不会出现,然后在JavaScript加载后出现.

我有这个CSS

.banner-bottom-fx 
{
    position: absolute;
    bottom: 0;
    left: 0;
    width: 0;
    height: 0;
    border-top: 0 solid transparent; /* changes via JavaScript */
    border-right: 0 solid white; /* changes via JavaScript */
}

上边框和右边框的值通过此脚本更改的位置

function UpdateBannerFx()
{
    const banner = document.querySelector('.banner-bottom-fx');

    const borderRightWidth = window.innerWidth;
    const borderTopWidth = borderRightWidth / 30;

    banner.style.borderRightWidth = borderRightWidth + 'px';
    banner.style.borderTopWidth = borderTopWidth + 'px';
}

document.addEventListener("DOMContentLoaded", function() 
{
    UpdateBannerFx();
    // Update border widths when window is resized
    window.addEventListener('resize', UpdateBannerFx);
});

我知道"闪烁"是由于我的CSS styles.css,它在我的头文件中加载,然后是JavaScript从main. js加载到页脚.有什么方法可以防止这种闪烁?

推荐答案

你不需要JavaScript来实现这个.100vw等于window.innerWidth.

.banner-bottom-fx {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 0;
    height: 0;
    border-top: calc(100vw / 30) solid transparent; /* window.innerWidth / 30 */
    border-right: 100vw solid white;  /* window.innerWidth */
}

看到CSS Values and Units

Javascript相关问答推荐

如何解决chrome—extension代码中的错误,它会实时覆盖google—meet的面部图像?'

为什么!逗号和空格不会作为输出返回,如果它在参数上?

CheckBox作为Vue3中的一个组件

如何将Openjphjs与next.js一起使用?

在react JS中映射数组对象的嵌套数据

无法使用单击按钮时的useState将数据从一个页面传递到另一个页面

如何在文本字段中输入变量?

如何迭代叔父元素的子HTML元素

为什么我的getAsFile()方法返回空?

Web Crypto API解密失败,RSA-OAEP

检索相加到点的子项

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

为什么客户端没有收到来自服务器的响应消息?

为什么JPG图像不能在VITE中导入以进行react ?

在不扭曲纹理的情况下在顶点着色器中旋转UV

JWT Cookie安全性

从异步操作返回对象

脚本语法错误只是一个字符串,而不是一个对象?

如何在加载页面时使锚定标记成为焦点

在使用JavaScript以HTML格式显示Google Drive中的图像时遇到问题