1倒计时
setInterval实现
3秒倒计时<body><button id="btn">3</button><script>var num=3;var btn = document.getElementById("btn");var timer = setInterval(function(){num--;if(num>=0){btn.innerHTML = num;}else{clearTimeout(timer);}},1000)</script></body>
setTimeout实现
<script>
var num=3;
var btn = document.getElementById("btn");
function go(){
setTimeout(function(){
num--;
if(num>=0){
btn.innerHTML = num;
go();
}
},1000)
}
go();
</script>
5秒倒计时
<body>
<button id="btn">发送验证码</button>
<script>
var num = 5;
var btn = document.getElementById('btn');
btn.onclick = function go(){
setTimeout(function(){
if(num>=0){
btn.innerHTML = num;
btn.disabled = true;
go();
num--;
}
else{
btn.disabled = false;
btn.innerHTML = '发送验证码';
num = 5;
}
},1000)
}
</script>
倒计时
<style>
span {
display: inline-block;
width: 50px;
line-height: 50px;
background-color: rgba(0, 0, 0, .6);
color: white;
text-align: center;
font-size: 20px;
}
</style>
</head>
<body>
<span class="day">1</span>
<span class="hour">2</span>
<span class="minute">3</span>
<span class="second">4</span>
<script>
var day = document.querySelector('.day')
var hour = document.querySelector('.hour');
var minute = document.querySelector('.minute');
var second = document.querySelector('.second');
var inputTime = +new Date('2021-10-14 23:25:00'); //返回的是用户输入时间总的毫秒数
conuntDown(); //避免刷新出现空白
setInterval(conuntDown, 1000)
function conuntDown() {
var nowTime = +new Date(); //返回的是当前时间总的毫秒数
var times = (inputTime-nowTime) / 1000; //返回的是当前时间总的毫秒数与用户输入时间总的毫秒数差值
var d = parseInt(times/60/60/24);//天
d = d<10?'0'+d:d;
day.innerHTML = d;
var h = parseInt(times / 60 / 60 % 24); //时
h = h < 10 ? '0' + h : h;
hour.innerHTML = h;
var m = parseInt(times / 60 % 60); //分
m = m < 10 ? '0' + m : m;
minute.innerHTML = m;
var s = parseInt(times % 60); //秒
s = s < 10 ? '0' + s : s;
second.innerHTML = s
}
</script>
2 nav
1 当滚动条滚动到大于300px的高度时 nav显示
<style>
*{
padding: 0;
margin: 0;
}
.nav{
width: 100%;
height: 50px;
background-color: orangered;
position: fixed;
opacity: 0;
}
body{
height: 2000px;
}
</style>
</head>
<body>
<div class="nav"></div>
<script>
var nav = document.getElementsByClassName('nav')[0];
window.onscroll = function (){
var scrolltop = scrollY;
var opacity = scrolltop/300;
if(opacity>1){
opacity=1;
}
nav.style.opacity = opacity;
}
</script>
</body>
2 当滚动条滚动到大于300px的高度时 nav显示 置顶按钮显示。点击回到顶部nav和置顶按钮消失
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 3000px;
}
.header {
width: 100%;
line-height: 80px;
text-align: center;
font-size: 30px;
color: #fff;
background-color: lightblue;
position: fixed;
opacity: 0;
transition: opacity .5s linear;
}
.gotop {
width: 50px;
height: 50px;
background-color: pink;
font-size: 20px;
text-align: center;
color: #fff;
position: fixed;
bottom: 50px;
right: 60px;
opacity: 0;
}
</style>
</head>
<body>
<div class="header">顶部导航</div>
<div class="gotop">回到顶部</div>
<script>
var header = document.querySelector('.header');
var gotop = document.querySelector('.gotop');
window.onscroll = function () {
var scrolltop = scrollY;
var opacity = scrolltop / 300;
if (scrolltop >= 300) {
header.style.opacity = opacity;
gotop.style.opacity = 1;
}
else{
header.style.opacity = 0;
gotop.style.opacity = 0;
}
}
gotop.onclick = function(){
window.scrollTo(
{
top:0,
behavior:'smooth'
}
)
}
</script>
</body>
3 tab切换

<style>
*{
padding: 0;
}
ul li {
display: inline-block;
margin: 8px;
}
.active{
color: orangered;
}
#content {
width: 100px;
height: 100px;
border: 1px solid orchid;
position: relative;
}
#content div {
width: 100%;
height: 100%;
position: absolute;
}
#content div:nth-child(1) {
background-color: orangered;
z-index: 10;
}
#content div:nth-child(2) {
background: lavender;
}
</style>
</head>
<body>
<ul >
<li class="active">登录</li>
<li >注册</li>
</ul>
<div id="content">
<div>登录内容</div>
<div>注册内容</div>
</div>
<script>
var lis = document.querySelectorAll('ul li');
var content = document.querySelectorAll('#content>div')
for(var i = 0;i<lis.length;i++){
lis[i].index = i;//lis[i].setAttribute('index',i)
lis[i].onclick = function(){
for(var i=0;i<lis.length;i++){
lis[i].classList.remove('active');
content[i].style.display = "none";
}
this.classList.add('active');
content[this.index].style.display = "block";
//var index =this.getAttribute('index');
// content[index].style.display = "block";
}
}
</script>
</body>
||
<ul>
<li>登录</li>
<li>注册</li>
<div class="show">
<div>登陆区域</div>
<div>注册区域</div>
</div>
</ul>
<script>
$('li').click(function(){
$(this).addClass('current').siblings().removeClass('current')
var index = $(this).index();
$('.show div').eq(index).show().siblings().hide();
})
</script>
tips:index() 方法返回指定元素相对于其他指定元素的 index 位置
如果未找到元素,index() 将返回 -1
点击切换图片
<body>
<div class="container">
<ul>
<li class="active"></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<div class="img">
<div><img src="../images/adv2.jpg" alt=""></div>
<div><img src="../images/adv3.jpg" alt=""></div>
<div><img src="../images/adv4.jpg" alt=""></div>
<div><img src="../images/adv5.jpg" alt=""></div>
</div>
<script>
var lis = document.querySelectorAll('ul li');
var imgs = document.querySelectorAll('.img>div')
// 为每个li添加点击事件
for(var i=0;i<lis.length;i++){
// 获取下标
lis[i].index = i;
lis[i].onclick = function(){
// 移除class样式
for (var i=0 ;i<lis.length;i++ ){
lis[i].className = "";
}
// 为当前点击的li添加class样式
this.className = 'active';
for(var i= 0;i<lis.length;i++){
imgs[i].style.display = 'none';
}
imgs[this.index].style.display = 'block';
}
}
</script>
||
<script>
$("ul li").click(function(){
$(this).addClass("active").siblings().removeClass("active");
var index = $(this).index();
$(".img div").eq(index).css({display:"block"}).siblings().css({display:"none"});
//$(".img div").eq(index).show().siblings().hide();
})
</script>
</body>
