【发布时间】:2021-11-28 14:30:08
【问题描述】:
您好,提前感谢您阅读我的问题。
目标:设置图像,使其在滚动到视图后平滑过渡到设置位置 - 但仍会对 :hover 做出反应。使用@keyframes 和一点JavaScript,我将图像设置为opacity: 0,并将其最终不透明度设置为opacity: .85。然后我在 CSS 中添加了一个悬停效果,使其成为opacity: 1
问题是,一旦完成过渡 - 它就会消失 - 恢复到原来的不透明度为零。我设法让它冻结在.85 和animation-fill-mode: forwards,而不是animation-fill-mode: none,但它不会响应:hover
下面是实际问题的测试 sn-p:
let observer_img = new IntersectionObserver(updates => {
updates.forEach(update => {
if (update.isIntersecting) {
update.target.classList.add('shift_frame_center_img');
} else {
update.target.classList.remove('shift_frame_center_img');
}
});
}, { threshold: 0 });
[...document.querySelectorAll('.features-img-wrapper img')].forEach(element => observer_img.observe(element));
body {
width: 100%;
height: 100vh;
background-color: lightgrey;
}
/* CHILD */
.features-img-wrapper img {
width: 10rem;
display: block;
margin-left: auto;
margin-right: auto;
margin-top: 8rem;
opacity: 0;
transition: all .5s;
}
/* APPEND-CHILD */
.shift_frame_center_img {
animation: center_img 1s 0.5s none;
}
/* CHILD ON HOVER */
.features-img-wrapper img:hover {
opacity: 1;
transform: scale(1.035);
}
/* KEYFRAMES */
@keyframes center_img {
0% {
transform: translateY(20rem);
}
100% {
transform: translateY(0);
opacity: .85;
}
}
<body>
<div class="features-img-wrapper">
<img src="https://synapse.it/wp-content/uploads/2020/12/test.png">
</div>
</body>
如果我能帮上忙,那就太好了,我有点初学者,已经花了几个小时在这方面,欢迎所有反馈。非常感谢。
【问题讨论】:
标签: javascript html css user-interface animation