css - 悬停时的 SVG 抖动动画
问题描述
当您将鼠标悬停在它上面时,我正在尝试使用 CSS 关键帧进行 svg img 抖动。
这是我想要达到的效果:https ://www.w3schools.com/code/tryit.asp?filename=FZ7CQHXPG42J
有一些 javascript 可以为相同的 svg 设置动画,但它以父 div #ellipse 为目标,当单击汉堡包图标时,它会从左向右移动。我看不出它会如何干扰,但我添加它只是为了确保。
我在下面添加了相关代码。有关完整代码,请参阅网站。
// Other unrelated(!?) animations on #ellipse //
function moveEllipseRight() {
ellipse.animate([
// keyframes
{
transform: 'translateX(0px)'
},
{
transform: 'translateX(' + width + 'px)'
}
], {
// timing options
duration: 500,
iterations: 1,
easing: 'ease-in-out',
fill: 'forwards'
});
}
function moveEllipseLeft() {
ellipse.animate([
// keyframes
{
transform: 'translateX(' + width + 'px)'
},
{
transform: 'translateX(0px)'
}
], {
// timing options
duration: 500,
iterations: 1,
easing: 'ease-in-out',
fill: 'forwards'
});
}
#ellipse {
position: absolute;
top: 120px;
z-index: -99;
animation: 3s linear 0s slide 1;
left: -200px;
}
img.shake:hover {
animation: shake 0.5s;
animation-iteration-count: infinite;
}
@keyframes shake {
0% {
transform: translate(1px, 1px) rotate(0deg);
}
10% {
transform: translate(-1px, -2px) rotate(-1deg);
}
20% {
transform: translate(-3px, 0px) rotate(1deg);
}
30% {
transform: translate(3px, 2px) rotate(0deg);
}
40% {
transform: translate(1px, -1px) rotate(1deg);
}
50% {
transform: translate(-1px, 2px) rotate(-1deg);
}
60% {
transform: translate(-3px, 1px) rotate(0deg);
}
70% {
transform: translate(3px, 1px) rotate(-1deg);
}
80% {
transform: translate(-1px, -1px) rotate(1deg);
}
90% {
transform: translate(1px, 2px) rotate(0deg);
}
100% {
transform: translate(1px, -2px) rotate(-1deg);
}
}
<div id="ellipse">
<img src="https://lh5.googleusercontent.com/-yqttzktPkDY/AAAAAAAAAAI/AAAAAAAABGU/z6CVGRmY-C8/photo.jpg?sz=328" alt="ellipse" class="shake" width="400" height="400" />
</div>
解决方案
问题在于你的#ellipse
风格。
#ellipse {
position: absolute;
top: 120px;
z-index: -99; /* remove this, it is not doing anything useful. */
animation: 3s linear 0s slide 1;
left: -200px;
}
问题是,悬停根本没有被触发,因为它在container
元素后面是负z-index
的。这个负 z 索引根本没有用,除非您打算将文本放在图像上方,我在您的网站上看不到。
推荐阅读
- ffmpeg - FFprobe 输出中的“has_b_frames”是什么意思?
- highcharts - Highmaps 钻取和不一致样式的工具提示?
- php - php发送的电子邮件标题中的特殊字符
- java - 配置 log4j2 为不同的包有不同的日志级别
- javascript - 我怎样才能在这里触发 popstate 事件?
- swift - 有人可以帮我阻止 arc4random 生成的数字再次出现吗?
- python - 您可以使用 Twitter API 查询的每个帐户的推文是否有时间限制?
- html - 如何使用引导 css 制作导航项?
- grafana - Grafana 中按天分组的 InfluxDB 数据移动了一天
- google-sheets - 根据输入在 Google 表格中创建动态列表