【发布时间】:2019-01-30 08:43:01
【问题描述】:
代码
我有 3 个带文本的 div (1, 2, 3)。
当用户将鼠标悬停在每个 div 上时,它应该改变图像。
我有一个 1s 的缓动过渡以使其平滑。
BUG
如果您将鼠标悬停在不同 div 上的速度过快,则过渡会不稳定或无法加载。我正在寻找一种解决方案,用户可以快速浏览 div,并且图像将缓慢过渡到最新的 div。提前谢谢你。
CODEPEN 演示
现场演示:
img1 = 'url(https://images.unsplash.com/photo-1533273859801-d731381dfe2d)';
img2 = 'url(https://images.unsplash.com/photo-1534939444268-6a9ff2733c32)';
img3 = 'url(https://images.unsplash.com/photo-1534841515798-3d43f5434123)';
$('.p1').hover(function(){
$('.bg').css({'background-image': img1});
});
$('.p2').hover(function(){
$('.bg').css({'background-image': img2});
});
$('.p3').hover(function(){
$('.bg').css({'background-image': img3});
});
body {
margin: 0;
font-family: sans-serif;
}
.bg {
position: absolute;
width: 100%;
height: 100%;
background: url(https://images.unsplash.com/photo-1533273859801-d731381dfe2d) no-repeat center;
background-size: cover;
z-index: -1;
background-color: #989898;
background-blend-mode: multiply;
transition: background-image 1s ease
}
.projects {
position: absolute;
margin: auto;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100vw;
height: 16em;
}
.p {
font-size: 1.2em;
position: relative;
padding: 2rem 0;
color: #fff;
opacity: 0.5;
letter-spacing: 4px;
text-indent: 4px;
font-weight: 400;
transition: opacity 0.5s ease;
text-align: center;
}
.p:hover {
opacity: 1;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bg"></div>
<div class="projects">
<div class="p p1">1</div>
<div class="p p2">2</div>
<div class="p p3">3</div>
</div>
【问题讨论】:
-
这些图片有多大?别介意波涛汹涌的过渡,他们差点把我的电脑冻住了。
-
@ibrahimmahrir 哈哈,它们来自 unsplash,它们是高分辨率的,但使用我的快速 wifi 仍然是个问题。过渡错误是独立的。
-
在几次悬停/悬停后错误是否仍然存在?
-
是的,还是有问题
-
@ibrahimmahrir 用较低分辨率的图像更新了 codepen 演示 codepen.io/grysn/pen/jvbBQd?editors=0110 仍然有问题
标签: javascript jquery css css-transitions transition