<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
const isFixed = ref(false);
const isScrolledOut = ref(false);
const scrollThreshold = 300;
const handleScroll = () => {
const scrollY = window.scrollY;
isFixed.value = scrollY > scrollThreshold;
isScrolledOut.value = scrollY > scrollThreshold; // 画像をスライドアウトさせる
};
onMounted(() => {
window.addEventListener('scroll', handleScroll);
});
onUnmounted(() => {
window.removeEventListener('scroll', handleScroll);
});
</script>
<template>
<div class="wrap">
<div :class="['contenta', { 'fixed': isFixed, 'visible': isFixed }]">
<img src="/test/image1.svg" class="image1" :class="{ 'slide-out': isScrolledOut }" />
<div class="overlay">テキスト</div>
<img src="/test/image2.svg" class="image2" :class="{ 'slide-out': isScrolledOut }" />
</div>
</div>
</template>
<style scoped>
.wrap {
padding-top:300px;
width:100%;
height: 1200px;
display: flex;
flex-direction: column;
margin: 0 auto;
}
.contenta {
width: 100%;
position: relative;
transition: transform 0.5s ease-in-out, opacity 0.5s ease-in-out;
}
.fixed {
position: fixed;
top: 0;
transform: translateY(-100%);
width: 100%;
z-index: 1000;
height: 100px;
background: rgba(160, 160, 160, 0.2);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
opacity: 0;
}
.visible {
transform: translateY(0);
opacity: 1;
}
/* 画像をスライドアウトするアニメーション */
.slide-out {
transform: translateY(-100%);
opacity: 0;
transition: transform 0.3s ease-in-out, opacity 0.3s ease-in-out;
transition-delay: 0s; /* すべて同時に動かす */
}
.image1,
.image2,
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.image1 {
z-index: 1;
width: 64px;
height: 64px;
left: 42px;
}
.overlay {
z-index: 2;
background: aquamarine;
display: flex;
align-items: center;
justify-content: center;
top: 20px;
width: 155px;
height: 50px;
}
.image2 {
z-index: 3;
width: 16px;
height: 16px;
top: 12px;
left: 86px;
}
</style>