【问题标题】:Closing page animation on browser refresh / clicking the internal menu links在浏览器刷新/单击内部菜单链接时关闭页面动画
【发布时间】:2020-08-31 00:52:26
【问题描述】:

我制作了一个打开页面动画,它会在页面加载后立即运行。

我的问题是关闭动画时间。例如,它持续 2 秒,我喜欢在浏览器刷新/单击内部菜单链接(浏览器操作最多)时实现它。 我知道如何在 onunload 上运行脚本并研究了很多实现,但问题是浏览器在 2 秒结束之前离开页面。它削减了我看到的动画已经开始。

是否可以让浏览器暂停刷新或加载下一页 2 秒?

我能想到的唯一一件事是在我的内部链接上绑定 JS 代码以启动关闭动画,并在 2 秒后进行实际链接重定向。 Lats 说这可以在内部链接中使用,但我可以让浏览器操作做同样的事情吗?

setTimeout(function() {
  $(document).ready(function() {
    $('#plahta').prop('checked', false);
  });
}, 2000);


setTimeout(function() {
  $(document).ready(function() {
    $('#plahta').prop('checked', true);
  });
}, 4000);
.curtain {
  margin: 0 auto;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.curtain__wrapper {
  width: 100%;
  height: 100%;
}

.curtain__wrapper input[type=checkbox] {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0;
  top: 0;
  left: 0;
}

.curtain__wrapper input[type=checkbox]:checked~div.curtain__panel--left {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}

.curtain__wrapper input[type=checkbox]:checked~div.curtain__panel--right {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}

.curtain__panel {
  display: -webkit-box;
  display: flex;
  -webkit-box-align: center;
  align-items: center;
  background: #23232e;
  color: #fff;
  float: left;
  position: relative;
  width: 50%;
  height: 100vh;
  -webkit-transition: all 2s ease-in-out;
  transition: all 2s ease-in-out;
  z-index: 80;
  transition-timing-function: ease-in-out;
}

.curtain__panel--left {
  -webkit-box-pack: end;
  justify-content: flex-end;
  -webkit-transform: translateX(-100%);
  transform: translateX(-100%);
}

.curtain__panel--right {
  -webkit-box-pack: start;
  justify-content: flex-start;
  -webkit-transform: translateX(100%);
  transform: translateX(100%);
}

.curtain__content {
  position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="curtain">
  <div class="curtain__wrapper">
    <input type="checkbox" id="plahta" checked>
    <div class="curtain__panel curtain__panel--left">
      <span class="intro">DOBRO</span>
    </div>
    <main class="wrapper dark curtain__content" id="main">
      <div id="fullpage" class="w3-container spinner">
        <h1>TEST</h1>
      </div>
    </main>
    <div class="curtain__panel curtain__panel--right">
      <span class="intro">DOŠLI</span>
    </div>
  </div>
</div>

【问题讨论】:

  • 你真的需要使用单页架构才能让这样的东西工作
  • @RoryMcCrossan 我明白你在说什么,我已经在使用整页部分但只有几个子页面应该由他们自己设计,不同的设计等等,所以我认为太分开了他们。它现在重量轻且速度快,因此将它们分开听起来更好,然后一次性加载,等等。

标签: javascript jquery css animation css-transitions


【解决方案1】:

您不能暂停、停止或阻止浏览器退出或刷新页面,除非使用alert()

如果您想重定向,您可以检测元素上的click 事件,触发动画,并在动画完成时将回调设置为location.href = "someurl",并带有您要重定向到的URL。

我认为您可以设法在单页网络应用程序中处理刷新。但这并不意味着你应该。当人们退出/刷新时,他们不会关心看到动画播放。这是我的 UX 建议。

【讨论】:

  • 是的,我知道警报,这就是为什么我宁愿问是否有其他解决方法。就点击而言,我在“我能想到的唯一事情”部分中提到了自己。我认为这样做不好。而且由于我认为我不会得到任何其他答案(不得不问),所以稍后会接受你的答案,这样你就可以得到第一分。欢迎来到 SO。
  • 谢谢。想象一下,如果可以用 JS 阻止页面关闭 - 你无法关闭一些广告弹出窗口,或者更糟糕的是,色情弹出窗口?
  • 哦,我知道这一切,不是我的第一个牛仔竞技表演。问题不是关于关闭,而是关于暂停/延长/延迟刷新和打开内部域链接。但看起来我让我的想象力有点疯狂。干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 2019-09-23
相关资源
最近更新 更多