LoginSignup
3
5

More than 1 year has passed since last update.

Nuxtでスマホアプリのようにsplash screenを表示させる方法

Last updated at Posted at 2021-10-09

こんな感じ

gif.gif

はじめに

Nuxtで作ったwebアプリで、スマホアプリのsplash screen(👇のような起動時に表示される画像)を表示させたいと思いました。
splash-screen-1-860x400.png

よくあるローディング画面とは違って、必ず指定した秒数だけ表示させるようにしたかったので色々と調べた結果をここに記録します!!

splash screen用の画面

最初の画面が表示される前に、ふんわりとロゴが表示されます。

components/LoadingScreen.vue
<template>
  <div :class="{ loader: true, fadeout: !isLoading }">
    <div class="page-container">
      <img class="splash" src="@/assets/images/Logo.png" />
    </div>
  </div>
</template>

<script lang="ts">
import Vue from "vue";
export default Vue.extend({
  name: "LoadingScreen",
  props: ["isLoading"],
});
</script>

<style scoped>
.page-container {
    max-width: 500px;
    width: 100%;
    margin: auto;
}

.loader {
  background-color: #ffffff;
  bottom: 0;
  color: white;
  display: flex;
  align-items: center;
  text-align: center;
  left: 0;
  overflow: hidden;
  position: fixed;
  right: 0;
  top: 0;
}

.splash {
  width: 150px;
}

.fadeout {
  animation: fadeout 2s forwards;
}

@keyframes fadeout {
  to {
    opacity: 0;
    visibility: hidden;
  }
}
</style>

LoadingScreen.vueを一番最初に表示させる

Nuxtで一番上の階層?にあるdefault.vueを編集。
これでindex.vueが読み込まれる前にsplash screenっぽい感じで、ロゴ画像をふんわりと表示させることに成功しました!

layouts/default.vue
<template>
  <div>
    <LoadingScreen :isLoading="isLoading" />
    <transition name="fade" v-if="!isLoading">
      <Nuxt />
    </transition>
  </div>
</template>

<script>
const LoadingScreen = () => import("../components/LoadingScreen.vue");
export default {
  components: {
    LoadingScreen,
  },
  data() {
    return {
      isLoading: true,
    };
  },
  mounted() {
    setTimeout(() => {
      this.isLoading = false;
    }, 1000);
  },
};
</script>

<style scoped>
.fade-enter-active,
.fade-leave-active {
    transition: opacity 1s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
}
</style>

参考

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5