0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

見えないところにある要素を超アピールしてくるアレをVueUseでサクッと実装

Last updated at Posted at 2023-03-22

見えないところにある要素を超アピールしてくるアレ

スクロールしてぇぇぇぇ~とアピールするアレです。

image.png

自己主張してるけどスクロールしたら消えるアレです。

ということでVueUseに便利な機能がありましたので紹介。

VueUseの「useElementVisibility」

「useElementVisibility」

機能はシンプルに
「指定した要素が画面領域にいるかどうか真偽値をrefオブジェクトで保持してくれる」

使い方

画面の要素に入っているかどうかをチェックしたい要素のrefオブジェクトをuseElementVisibilityに入れるだけ!

めちゃくちゃ簡単\(^o^)/

import { useElementVisibility } from '@vueuse/core';
const elmBottom = ref<HTMLElement | null>(null);
const elmBottomIsVisible = useElementVisibility(elmBottom);
<button type="button" class="btn btn-favo" ref="elmBottom">Favo</button>

あとはこれをうまく利用して・・・

見えないところにある要素を超アピールしてくるアレ


<script setup lang="ts">
import { useElementVisibility } from '@vueuse/core';
import { ref } from 'vue';
const elmTop = ref<HTMLElement | null>(null);
const elmBottom = ref<HTMLElement | null>(null);
const elmBottomIsVisible = useElementVisibility(elmBottom);
const bottomScroll = () => {
  if (elmBottom.value === null) return;
  elmBottom.value.scrollIntoView({
    behavior: 'smooth',
    block: 'center',
    inline: 'center',
  });
};
const topScroll = () => {
  if (elmTop.value === null) return;
  elmTop.value.scrollIntoView({
    behavior: 'smooth',
    block: 'start',
    inline: 'center',
  });
};
</script>
<template>
  <div class="container-fluid">
    <div class="card mt-2 mb-3" ref="elmTop">
      <div class="card-header bg-info">見えないところにある要素を超アピールしてくるアレ</div>
      <div class="card-body">
        Bing Chatで教えてもらったPoem(深い意味はないw) とりあえずスクロールする領域の高さを作りたかっただけ。
        <div class="title">その①</div>
        <div class="poems">
          The Lake Isle of Innisfree I will arise and go now, and go to Innisfree, And a small cabin build there, of
          clay and wattles made: Nine bean-rows will I have there, a hive for the honey-bee; And live alone in the
          bee-loud glade. And I shall have some peace there, for peace comes dropping slow, Dropping from the veils of
          the morning to where the cricket sings; There midnight's all a glimmer, and noon a purple glow, And evening
          full of the linnet's wings. I will arise and go now, for always night and day I hear lake water lapping with
          low sounds by the shore; While I stand on the roadway, or on the pavements grey, I hear it in the deep heart's
          core.
        </div>
        <div class="title">その②</div>
        <div class="poems">
          Shall I compare thee to a summer’s day? Thou art more lovely and more temperate: Rough winds do shake the
          darling buds of May, And summer’s lease hath all too short a date:
        </div>
        <div class="title">その③</div>
        <div class="poems">
          Let me not to the marriage of true minds Admit impediments. Love is not love Which alters when it alteration
          finds, Or bends with the remover to remove:
        </div>
      </div>
      <div class="card-body">
        <button type="button" class="btn btn-favo" ref="elmBottom" @click="topScroll">Favo</button>
      </div>
    </div>
  </div>
  <teleport to="#teleport">
    <div class="pop-up" @click="bottomScroll()" :class="{ isShow: !elmBottomIsVisible }">
      <i class="fas fa-angle-double-down" style="--data-delay: 0ms"></i>
      <i class="fas fa-angle-double-down" style="--data-delay: 100ms"></i>
      <i class="fas fa-angle-double-down" style="--data-delay: 200ms"></i>
      <span>Favo!</span>
      <i class="fas fa-angle-double-down" style="--data-delay: 200ms"></i>
      <i class="fas fa-angle-double-down" style="--data-delay: 100ms"></i>
      <i class="fas fa-angle-double-down" style="--data-delay: 0ms"></i>
    </div>
  </teleport>
</template>

<style lang="scss">
@keyframes pikopiko {
  0% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(0, -5px);
  }
  100% {
    transform: translate(0, 0);
  }
}
</style>
<style lang="scss" scoped>
.pop-up {
  position: fixed;
  inset: auto auto 0 0;
  width: auto;
  height: 40px;
  padding: 10px;
  background-color: rgb(253, 87, 114);
  border-radius: 0 10px 0 0;
  color: white;
  transform: translateY(40px);
  transition: all 400ms;
  cursor: pointer;
  &.isShow {
    transform: translateY(0);
    > i {
      animation: pikopiko 300ms infinite linear alternate;
      animation-delay: var(--data-delay);
    }
  }
}
.btn-favo {
  background-color: rgb(243, 169, 182);
  color: rgb(255, 255, 255);
  &:hover {
    background-color: rgb(255, 212, 220);
  }
  &:active {
    background-color: rgb(248, 158, 174);
  }
}
.title {
  margin: 10px 0 0 0;
  font-size: 12px;
  background-color: gray;
  color: white;
}

.card {
  max-width: 300px;
}
.card,
.card-header,
.card-body {
  border-color: rgb(24, 7, 112);
}
</style>


↓見た目は・・・

image.png

↓スクロールしたら消える
image.png

デモページを用意したので・・・

思う存分スクロールしてください

GitHub

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?