iCode
コード一覧
ログイン
新規登録
hover
Created by
ippei (#19)
<body> <ul id="section"> <li class="item"> <a href="" class="a"> <p>category</p> <div class="text"> <h2>test-ttl-01test-ttl-01test-ttl-01</h2> <p>2022.04.18</p> </div> </a> </li> <li class="item"> <a href="" class="a"> <p>category</p> <div class="text"> <h2>test-ttl-01test-ttl-01test-ttl-02</h2> <p>2022.04.18</p> </div> </a> </li> <li class="item"> <a href="" class="a"> <p>category</p> <div class="text"> <h2>test-ttl-01test-ttl-01test-ttl-03</h2> <p>2022.04.18</p> </div> </a> </li> <li class="item"> <a href="" class="a"> <p>category</p> <div class="text"> <h2>test-ttl-01test-ttl-01test-ttl-04</h2> <p>2022.04.18</p> </div> </a> </li> <div class="hover-stalker-img"></div> </ul> </body> <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.2/gsap.min.js"></script> <script type="text/javascript" src="js/main.js"></script> </body> </html>
/*font-family*/ $family-poppins:'Poppins', sans-serif; $family-zen-kaku: 'Zen Kaku Gothic Antique', sans-serif; /*color*/ $gray: rgb(41, 41, 41); $white: #fff; $white02:rgba(255, 255, 255, 0.228); $font-color-ctg: rgb(162, 162, 162); body { font-family: $family-poppins; font-family: $family-zen-kaku; background-color: $gray; } a { color: #fff; text-decoration: none; } li { list-style: none; } ul { width: 100%; height: 100vh; display: flex; justify-content: center; flex-flow: column; align-items: center; overflow: hidden; position: relative; li { width: 80%; margin: 0 auto; position: relative; z-index: 1; a { display: flex; align-items: center; padding: 48px 0; position: relative; &::before { content: ""; height: 1px; width: 100%; background-color: $white02; display: block; position: absolute; top: 0; } &::after { content: ""; height: 2px; width: 100%; background-color: $white; display: block; position: absolute; top: 0; transform: scale(0); transform-origin: right center; transition: transform 1s cubic-bezier(.19,1,.22,1); } > p { font-size: 16px; color: $font-color-ctg; margin-right: 5%; } div { h2 { font-size: 24px; letter-spacing: 0.07em; } p { font-size: 14px; color: $font-color-ctg; } } &:hover { &::after { transform: scale(1); transform-origin: left center; } } .hover-img { width: 350px; height: 350px; overflow: hidden; position: absolute; top: 0; left: 0; pointer-events: none; z-index: 0; opacity: 0; img { display: block; width: 100%; height: 100%; object-fit: cover; transform: scale(1.1); } } &.js-active { z-index: 2; p { z-index: 2; } .text { z-index: 2; h2 { opacity: 1; transition: opacity 2s cubic-bezier(.19,1,.22,1); } } } } } .hover-stalker-img { position: absolute; display: block; top: 0; left: 0; width: 350px; height: 350px; background-position: 0 0; background-size: cover; transform: translate(-50%, -50%); z-index: 0; opacity: 0; visibility: hidden; pointer-events: none; } }
const wrapper = document.getElementById('section'); wrapper.addEventListener('mouseenter', (e) => { gsap.to(image,{ opacity: .5, visibility: "visible", }) }) wrapper.addEventListener('mouseleave', (e) => { gsap.to(image, { autoAlpha: 0, }) }) const image = document.querySelector('.hover-stalker-img'); wrapper.addEventListener('mousemove', (e) => { gsap.to(image, { duration: 2, x: e.pageX + 300, y: e.pageY - 100, ease: "Power4.easeOut", }) }) const items = document.querySelectorAll('.item'); const imgArr = [ 'https://images.unsplash.com/photo-1509631179647-0177331693ae?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=988&q=80', 'https://images.unsplash.com/photo-1529139574466-a303027c1d8b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=987&q=80', 'https://images.unsplash.com/photo-1475180098004-ca77a66827be?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=986&q=80', 'https://images.unsplash.com/photo-1601762603339-fd61e28b698a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=987&q=80', ]; items.forEach((item, i) => { item.addEventListener('mouseenter', (e) => { image.style.backgroundImage = `url("${imgArr[i]}")`; gsap.set(image, { backgroundPosition: "center", }); gsap.to(image, { repeat: -1, ease: "liner", }); }) })
Copy
Copied