css - 我想通过单击按钮来移动图像?
问题描述
我想通过单击按钮来移动 img。我知道这个问题被问了很多,但我无法使用它们。
继承人相关的HTML:
<button class="ggE" (click)="gegenEnemy(chosenHero, actualEnemy);moveFist(); ...
<div class="col">
<img id="img" class="fist" src="...">
这是函数(打字稿):
moveFist(): void {
document.getElementById('img').classList.add('fist');
}
还有我的css,它使图片从左向右移动:
.fist {
width: 70px;
height: 70px;
position: relative;
animation-name: example;
animation-duration: 2s;
}
@keyframes example {
0% {left:50px; top:20px;}
100% {left:450px; top:20px;}
}
那么为什么我不能通过单击按钮来连接它呢?
解决方案
首先验证您的HTML代码
<button class="ggE" onclick="moveFist();">button</button>
<div class="col">
<img id="img" class="" src="https://via.placeholder.com/150">
<div>
编辑:
function moveFist(){
document.getElementById("img").classList.remove("fist");
// element.offsetWidth = element.offsetWidth;
// Do this instead:
void img.offsetWidth;
document.getElementById("img").classList.add("fist");
}
通过它的偏移宽度设置img的宽度,即
img.offsetWidth;
你可以这样吗?
function moveFist(){
document.getElementById("img").classList.remove("fist");
// element.offsetWidth = element.offsetWidth;
// Do this instead:
void img.offsetWidth;
document.getElementById("img").classList.add("fist");
}
img{
width: 70px;
height: 70px;
position: relative;
left:50px;
animation-duration: 2s;
top:20px;
}
.fist{
position:relative;
animation-name: example;
}
@keyframes example {
0% {left:50px; top:20px;}
100% {left:450px; top:20px;}
}
<button class="ggE" onclick="moveFist()">button</button>
<div class="col">
<img id="img" class="" src="https://via.placeholder.com/150">
<div>
推荐阅读
- reactjs - 在反应中导航到我页面的不同部分
- php - 简码中的 WooCommerce do_action 弄乱了页面布局
- python-3.x - 如何使用 DataFrame 将 csv 文件转换为嵌套的 json 格式
- haskell - 和有什么区别。和 (.) 在 Haskell 中?
- r - 贬低一个数据集,我发现了两种导致不同结果的不同方式
- python - 如何在python中为类添加属性
- scala - 具有协方差的交集类型
- angular - 如何在具有角度的离子项目上导入 npm semver?
- java - Java - 从字符串打开 URL
- java - 我用 DTO、Entity 创建了一个弹簧靴,但也无法存储它