css 当加载器被调用时,如何灰显整个页面?

6mzjoqzu  于 2023-05-08  发布在  其他
关注(0)|答案(4)|浏览(120)

我有一个简单的<div id="loader"></div>在html中,只有当 AJAX 在Jquery中运行时才会显示出来。现在这个加载器只是一个简单的旋转加载图标,我从https://www.w3schools.com/howto/howto_css_loader.asp,但我想使背景灰色,以及,所以它更容易看到加载图标。但我不知道该怎么做
下面是我的CSS:

#loader {
    position: absolute;
    left: 50%;
    top: 50%;
    z-index: 1;
    width: 120px;
    height: 120px;
    margin: -76px 0 0 -76px;
    border: 16px solid #f3f3f3;
    border-radius: 50%;
    border-top: 16px solid #3498db;
    -webkit-animation: spin 2s linear infinite;
    animation: spin 2s linear infinite;
}

@-webkit-keyframes spin {
    0% {
        -webkit-transform: rotate(0deg);
    }
    100% {
        -webkit-transform: rotate(360deg);
    }
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

这是我在jquery中显示我的加载器的方式:

// Does the following if Ajax starts
$(document).ajaxStart(function() {
    $("#loader").show();
});

// Does the following if Ajax stops
$(document).ajaxStop(function() {
    $("#loader").hide();
    $('#cancelBtn').removeAttr('hidden');
});
46scxncf

46scxncf1#

如果你不想更新你的html,你可以通过使用阴影来获得一个很好的效果:

#loader {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 1;
  width: 120px;
  height: 120px;
  margin: -76px 0 0 -76px;
  border: 16px solid #f3f3f3;
  border-radius: 50%;
  border-top: 16px solid #3498db;
  -webkit-animation: spin 2s linear infinite;
  animation: spin 2s linear infinite;
  background: #aaa;
  box-shadow: 0 0 0 100vw #aaa;
}

@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<div id="loader"></div>
guz6ccqo

guz6ccqo2#

总是使用类选择器这是一个很好的做法,并使用最大z索引值为任何加载,模态等重叠其他元素
只需在div中扭曲加载器并应用以下css规则。

*{
  padding:0;
  margin:0;
  box-sizing:border-box;
}

.loader {
  width: 100vw;
  height: 100vh;
  background-color: lightgray;
  z-index: 999;/* max value*/
  position:fixed;
  top:0;
  left:0;
}

.loader > div {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 120px;
    height: 120px;
    margin: -76px 0 0 -76px;
    border: 16px solid #f3f3f3;
    border-radius: 50%;
    border-top: 16px solid #3498db;
    -webkit-animation: spin 2s linear infinite;
    animation: spin 2s linear infinite;
}

@-webkit-keyframes spin {
    0% {
        -webkit-transform: rotate(0deg);
    }
    100% {
        -webkit-transform: rotate(360deg);
    }
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}
<div class='loader'>
  <div></div>
</div>
k4aesqcs

k4aesqcs3#

如果你想给背景添加颜色,你只需要再给#loader添加一个属性:

background-color: #f3f3f3;

如果你想在页面的整个宽度上添加背景,你可以添加div:

<div class='content'>
 The content is not visible when animation is active!

  <div class="container">
    <div id="loader"></div>
  </div>
</div>

CSS属性:

.container {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #f3f3f3;
  z-index: 9999;
  justify-content: center;
  align-items: center;
}

您还可以修改jQuery代码来切换动画:

$(document).ajaxStart(function() {
  $(".container").css("display", "flex");
});

// Does the following if Ajax stops
$(document).ajaxStop(function() {
  $(".container").css("display", "none");
});
hfwmuf9z

hfwmuf9z4#

将你的loader Package 在一个div中,像这样:

.loader-container {
   display: flex;
   position: fixed;
   width: 100%;
   height: 100%;
   top: 0;
   left: 0;
   z-index: 10;
   background-color: grey;
}

.loader {
   position: relative;
   z-index: 11;
   width: 120px;
   height: 120px;
   margin: auto;
   border: 16px solid #f3f3f3;
   border-radius: 50%;
   border-top: 16px solid #3498db;
   -webkit-animation: spin 2s linear infinite;
   animation: spin 2s linear infinite;
}

@-webkit-keyframes spin {
    0% {
        -webkit-transform: rotate(0deg);
    }
    100% {
        -webkit-transform: rotate(360deg);
    }
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}
<div class="loader-container">
   <div class="loader"></div>
</div>

相关问题