5秒倒计时
<script>
var num =5;
var btn = document.getElementById("btn");
var timer
/* 1、让按钮进入倒计时的状态(不能点击的状态)*/
btn.onclick = function(){
this.disabled = true;
this.innerHTML = num;
/* 2、每过1s num-- */
timer = setInterval(function(){
num--
if(num>=0){
btn.innerHTML = num;
}else{
btn.disabled = false;
btn.innerHTML = "发送验证码";
num =5;
clearInterval(timer);
}
},1000)
}
/* 使用setTimeout去实现 */
</script>
渐变滚动条
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{margin:0;padding:0}
#nav{
height: 50px;
background-color: #ff2d51;
position: fixed;
width:100%;
opacity: 0;
}
body{
height: 2000px;
}
</style>
</head>
<body>
<div id="nav">
</div>
<script>
/* 当滚动条滚动到距离顶部200px的时候 导航条完全显示 */
var nav = document.getElementById("nav")
/* 1、对窗口执行监听事件 */
window.onscroll = function(){
/* 2、获取滚动条的高度 */
var scrollTop = document.documentElement.scrollTop;
/* 3、得到透明度 */
var opacity = scrollTop/200;
if(opacity>1){
opacity=1
}
nav.style.opacity = opacity;
console.log(opacity)
}
</script>
</body>
</html>
tab切换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.current{
color:orange
}
.content{
width:100px;
height: 100px;
border:1px solid #333;
position: relative;
}
.content>div{
width:100%;
height: 100%;
position: absolute;
}
.content>div:first-child{
background-color: red;
}
.content>div:last-child{
background-color: yellow;
display: none;
}
</style>
</head>
<body>
<ul>
<li class="current">登录</li>
<li>注册</li>
</ul>
<div class="content">
<div>登录的内容</div>
<div>注册的内容</div>
</div>
<script>
var lis = document.querySelectorAll('ul li');
var divs = document.querySelectorAll('.content div')
for(var i = 0;i<lis.length;i++){
lis[i].index = i;
lis[i].onclick = function() {
for(var i = 0;i<lis.length;i++) {
lis[i].className = '';
}
this.className = "current";
for(var i =0;i<divs.length;i++) {
divs[i].style.display = 'none';
}
divs[this.index].style.display = 'block'
}
}
</script>
</body>
</html>
tab切换jquery方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="libs/jquery-3.6.0.js"></script>
<style>
.current{
color:orange
}
.content{
width:100px;
height: 100px;
border:1px solid #333;
position: relative;
}
.content>div{
width:100%;
height: 100%;
position: absolute;
}
.content>div:first-child{
background-color: red;
}
.content>div:last-child{
background-color: yellow;
display: none;
}
</style>
<script>
$(function(){
$(".dl li").click(function (){
$(this).css("color","red").siblings().css("color","");
var index = $(this).index();
console.log(index);
$(".content div").eq(index).show();
$(".content div").eq(index).siblings().hide();
})
})
</script>
</head>
<body>
<ul class="dl">
<li >登录</li>
<li>注册</li>
</ul>
<div class="content">
<div>登录的内容</div>
<div>注册的内容</div>
</div>
</body>
</html>