javascript - 如何使按钮在第一次单击时消失,等待,然后在几秒钟后出现在另一个位置?
问题描述
我创建了一个按钮(#nextbtn),当它被点击时会淡出。我如何让它等待,比如 3 秒,然后在之前的 100 像素以下淡出?并在每次单击时继续执行此操作?这是我到目前为止所拥有的:CSS:
#nextbtn {
transition: margin-top 1s, opacity 1s;
opacity:1;
}
HTML:
<button id="nextbtn" onclick="nextClicked()">NEXT</button>
JS:
function nextClicked() {
var next = document.getElementById("nextbtn")
next.style.opacity = "0"
next.style.marginTop = "-30px"
}
解决方案
这是代码:
而不是使用顶部 [位置:相对] 使用边距
var next = document.getElementById("nextbtn");
var top1 = 0;
function nextClicked() {
next.style.opacity = "0";
setTimeout(()=>{
top1 += 100;
next.style.opacity = "1";
next.style.top = top1 + "px";
},3000);// 3 sec
}
#nextbtn {
transition: margin-top 1s, opacity 1s;
position: relative;
top: 0px;
}
<button id="nextbtn" onclick="nextClicked()">NEXT</button>
推荐阅读
- javascript - 如何接收用户输入的日期并将其与 date() javascript 函数匹配?
- rust - FnBox 示例抛出错误:盒装中没有 FnBox?
- stripe-payments - 为什么 Stripe checkout Webhooks 会将 customer_email 发送为 null?
- java - 创建输入字符菜单
- python - 并排显示两张 Folium 地图?
- html - 悬停在菜单上显示每个菜单中的下拉菜单列表
- ios - Swift:更改 iOS 13 的状态栏颜色
- javascript - 复选框收音机中的刷新方法不起作用
- c# - 在 Onion 架构中抛出异常与返回操作结果对象
- c - 在 Visual Studio 代码中运行的 gcc mingw 中编译 c 程序时出错