javascript - 启动画面水平定位不一致

标签 javascript html ios css

我正在创建一个网站,其中显示 gif 动画作为启动屏幕。不幸的是,在 iPhone 上查看时的水平尺寸不一致。 当网站首次加载时,动画 gif 会出现在屏幕中央的右侧。如果重新加载网站,那么它似乎工作得很好。

非常感谢您的建议:)

这是代码:

<小时/>

HTML

<div id="overlay">
<div id="overlay-content"><img src='videosmall.gif'></div>
</div>
<小时/>

CSS

#overlay {
  display: none;
  position: fixed;
  top: 0%;
  left: 0%;
  width: 100%;
  height: 100%;
  background-color: rgb(222, 222, 222);
}

#overlay-content {
  position: fixed;
  display: none;
}
<小时/>

JavaScript (jQuery)

function main() {
  $('#overlay').show();
  $('#overlay-content').show().center();
}


setTimeout(function() {
  $("#overlay").fadeOut();
  $('.i3section').fadeIn(5000);
}, 5000);



$.fn.center = function() {
  this.css("position", "absolute");
  this.css("top", Math.max(0, (
      ($(window).height() - $(this).outerHeight()) / 2) +
    $(window).scrollTop()) + "px");

  this.css("left", Math.max(0, (
      ($(window).width() - $(this).outerWidth()) / 2) +
    $(window).scrollLeft()) + "px");
  return this;
}

$(document).ready(main);

最佳答案

您应该尝试使用 CSS 来定位叠加层及其内容,以避免此问题。
+ 动画会更加流畅

示例:

HTML

<div id="overlay">
  <img src='https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg?v=6e4af45f4d66'>
</div>

CSS

#overlay {
  /* Display overlay in fullscreen */
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;

  /* Center it's content */
  display: flex;
  justify-content: center;
  align-items: center;

  /* Add nice animation */
  transition: visibility 0.4s, opacity 0.4s;

  /* While it's hidden */
  visibility: hidden;
  opacity: 0;
}
#overlay.open {
  /* While it's shown */
  visibility: visible;
  opacity: 1;
}
#overlay img {
  /* Styling image */
  display: block;
  width: 128px;
  height: auto;
}

JS

/* Showing the overlay */
$('#overlay').addClass('open');

setTimeout(function () 
{
  /* Hiding the overlay */
  $('#overlay').removeClass('open');
}, 3000);

fiddle : https://jsfiddle.net/JacobDesight/vkga6mhk/

关于javascript - 启动画面水平定位不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44606713/

相关文章:

javascript - setInterval 可以随时间漂移吗?

javascript - 可以跨越多列的砌体布局

ios - 如何制作从 SwiftUI 列表中删除多行的动画?

jquery - 面包屑 V 形 CSS

c# - 如何在构建 UIImageView 时创建图像

ios - 有什么方法可以获取在 iOS 中使用 flurry 删除应用程序的次数?

javascript - JavaScript 中 -10 到 10 之间的随机数

javascript - 为什么需要将自定义事件分派(dispatch)给对象?

javascript - 我的私有(private)组件中应该包含多少安全逻辑?

javascript - 在动态 iframe 中加载图像