简体   繁体   English

由JavaScript触发的CSS转换

[英]CSS Transition Triggered by JavaScript

I want to have the following JavaScript function to transition function between from have none display to block when generate_loading_screen() is called to to when it finishes transition between display block to none. 我希望有以下JavaScript函数来转换函数,在调用generate_loading_screen()时,无需显示就可以阻塞,当它完成display block to none之间的转换时。 How do I do this? 我该怎么做呢?

 function generate_loading_screen() { window.setInterval(function(){ if (progress_percent < 75) { document.getElementById("loading_screen").style.display = "block"; document.getElementById("body_of").style.filter = "grayscale(1)"; } else { document.getElementById("loading_screen").style.display = "none"; document.getElementById("body_of").style.filter = "none"; stop_generating_loading(); } }, 50); }; function stop_generating_loading() { clearInterval(generate_loading_screen); }; 
 .loading { position: fixed; border: 16px solid #dbdbdb; border-radius: 50%; border-top: 16px solid #53f442; margin-left: 44%; margin-top: 10%; width: 120px; height: 120px; animation: spin 2s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } 
 <div class="loading" id="loading_screen" style="display: none;"></div> 

Just extra info: progress_percent is a variable that determines how much of the rest of the web-app has loaded. 只是额外信息:progress_percent是一个变量,用于确定已加载的其余网络应用程序的数量。 The grayscale filter does not affect the whole page, just the ID body_of 灰度过滤器不会影响整个页面,只会影响ID body_of

Thanks in advance 提前致谢

window.setInterval returns an intervalId which you need to cancel in order to stop the interval window.setInterval返回intervalId你需要以停止取消interval

let timer;
function generate_loading_screen() {
  timer = window.setInterval(function(){
    if (progress_percent < 75) {
      document.getElementById("loading_screen").style.display = "block";
      document.getElementById("body_of").style.filter = "grayscale(1)";
    }
    else {
      document.getElementById("loading_screen").style.display = "none";
      document.getElementById("body_of").style.filter = "none";
      stop_generating_loading();
    }
  }, 50);
};

function stop_generating_loading() {
  clearInterval(timer);
};

Probably better to use a opacity transition by adding a class when your percent reaches 100. 当百分比达到100时,通过添加类可能更好地使用不透明度转换。

Codepen for working example or see below. Codepen的工作示例或见下文。

HTML: HTML:

<div class="loading" id="loading_screen"></div>

CSS: CSS:

.loading {
  position: fixed;
  border: 16px solid #dbdbdb;
  border-radius: 50%;
  border-top: 16px solid #53f442;
  margin-left: 44%;
  margin-top: 10%;
  width: 120px;
  height: 120px;
  animation: spin 2s linear infinite;
  opacity: 100%;
  transition: opacity 1s ease-in-out;
}

.done_loading {
  opacity: 0;
}

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

Javascript: 使用Javascript:

var progress_percent = 25;
var interval;

function generate_loading_screen() {
  interval = window.setInterval(function(){
    progress_percent += 1; //totest
    if (progress_percent > 75) {
      document.getElementById("loading_screen").className = "loading done_loading";
      //stop_generating_loading();
    }

    //TESTING
    if(progress_percent > 100){
      console.log("Reached 100%");
      document.getElementById("loading_screen").className = 'loading';
      progress_percent = 0;
    }
    //
  }, 50);
};

function stop_generating_loading() {
  clearInterval(interval);
};

document.addEventListener('DOMContentLoaded', function(){
  generate_loading_screen();
});

Remove all the testing code to get this to work once, you might need to add additional code for your body div. 删除所有测试代码以使其工作一次,您可能需要为您的body div添加其他代码。 Let me know if you need me to add more to this example! 如果您需要我为此示例添加更多内容,请告诉我们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM