javascript - 预加载页面适用于所有主流浏览器,但 Safari 除外

标签 javascript jquery html css frontend

我有一个预加载页面,该页面在客户端浏览器中完全加载网页之前显示 CSS 动画。它适用于 Chrome、Firefox 和 Internet Explorer,但无法在 Safari 中运行。甚至 CSS 命令在 Safari 中也无法正常工作,我不明白为什么!

当我在 Safari 中打开它时,第一个问题是动画无法正常工作。我什至添加了 @-webkit-keyframes 但它没有改变任何东西。但更奇怪的是,javascript 代码也不起作用。例如,即使我指定 body 不应再滚动,我仍然可以向下滚动它。我想这是 javascript 的跨浏览器问题,但我不知道它到底是什么。是因为我正在使用事件“加载”吗?也许 Safari 无法识别它?

我想使用 jQuery 重写代码,但找不到 window.addEvenListener("load",callback()); 的等效项在 jQuery 中。事件监听器 .load() 已被弃用。

更新:问题似乎是 Safari 没有正确执行 overflow: hidden。有谁知道这个问题的解决方法吗?

这是 MVC

document.body.style.overflow = "hidden";
    
setTimeout(() => {
  window.addEventListener('load', loaded());  
}, 5000);
    

function loaded(){
  document.getElementById('loading').style.display = 'none';
  document.body.style.overflowY = 'visible';
}
#loading {
  display: block;
  width: 100vw;
  height: 100vh;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: white;
  z-index: 10;
}
#flex_wrapper{
  position: relative;
  width: 100%;
  top: calc(50% - 50px);
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}
#loading_text{
  width: 100%;
  font-size: 1.2em;
  font-weight: 400;
  position: relative;
  top: calc(50% - 30px);
  height: 50px;
  text-align: center;
  line-height: 25px;
}
.loading_bar{
  width: 8px;
  height: 50px;
  background-color: black;
  margin: 0 5px;
  border-radius: 10px;
  animation: loading 1s infinite;
}
@keyframes loading{
  0%{
    height: 0px;
  }
  50%{
    height: 50px;
  }
  100%{
    height: 0px;
  }
}
.loading_bar:nth-child(2){
  animation-delay: 0.1s;
}

.loading_bar:nth-child(3){
  animation-delay: 0.2s;
}

.loading_bar:nth-child(3){
  animation-delay: 0.3s;
}

.loading_bar:nth-child(4){
  animation-delay: 0.4s;
}

.loading_bar:nth-child(5){
  animation-delay: 0.5s;
}

.loading_bar:nth-child(6){
  animation-delay: 0.6s;
}

.loading_bar:nth-child(7){
  animation-delay: 0.7s;
}

.loading_bar:nth-child(8){
  animation-delay: 0.8s;
}

.loading_bar:nth-child(9){
  animation-delay: 0.9s;
}

.loading_bar:nth-child(10){
  animation-delay: 1s;
}
<div id="loading">
  <div id="flex_wrapper">
    <div class="loading_bar"></div>
    <div class="loading_bar"></div>
    <div class="loading_bar"></div>
    <div class="loading_bar"></div>
    <div class="loading_bar"></div>
    <div class="loading_bar"></div>
    <div class="loading_bar"></div>
    <div class="loading_bar"></div>
    <div class="loading_bar"></div>
    <div class="loading_bar"></div>
  </div>
  <div id="loading_text">
    Please wait<br> Loading...
  </div>
</div>

最佳答案

好吧,我终于找到问题了!

问题是 overflow: hidden; 不足以在 iOS 上停止滚动。相反,您应该阻止事件 touchmove 的默认操作使滚动无法进行。这是我的 JavaScript 代码的更正版本,它可以在桌面和移动浏览器上运行:

<script language="javascript" type="text/javascript">
disableScroll();
document.body.style.overflowY = "hidden";

window.addEventListener("load", function(){
  document.getElementById("loading").style.display = "none";
  document.body.style.overflowY = "visible";
  enableScroll();
});  

function preventDefault(e){
  e.preventDefault();
}

function disableScroll(){
    document.body.addEventListener('touchmove', preventDefault, { passive: false });
}
function enableScroll(){
    document.body.removeEventListener('touchmove', preventDefault, { passive: false });
}
</script>

这个想法归功于这个 post

关于javascript - 预加载页面适用于所有主流浏览器,但 Safari 除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55305505/

相关文章:

javascript - React JS从列表中删除特定项目

asp.net-mvc-3 - JQuery 模态弹出问题中的 ASP.net MVC 3 不引人注目的客户端验证

jquery - 如何在加载淡出效果时在网站主页上显示整页图像

javascript - 如何使用 javascript 获取动态变化的 div 的高度?

html - 定位跨度元素以包装文本 block

javascript - 无法将指令应用于 AngularJS 中的两个不同 Controller

javascript - 当高度/宽度从像素切换到百分比时图像消失?

php - IE浏览器后退按钮问题

javascript - Libgdx 等框架如何处理 Web 目标的线程

javascript - 如何使用jquery将html存储在隐藏字段中?