jQuery头像图片切换
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jquery头像图片切换</title>
<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<style>
.box {
display: flex;
position: relative;
height: 100px;
margin-left: 100px;
position: relative;
}
.round {
width: 50px;
height: 50px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 0;
opacity: 0;overflow: hidden
}
.color1{
background-color: #f00;
}
.color2{
background-color: #f0f;
}
.color3{
background-color: #ff0;
}
.color4{
background-color: #00f;
}
.color5{
background-color: #0f0;
}
.color6{
background-color: #0ff;
}
.round:nth-child(1) {
transition: all 1s ease-in-out;
left: 0;
opacity: 0;
z-index: 5;
}
.round:nth-child(2) {
transition: all 0.8s ease-in-out;
left: 100px;
opacity: 1;
z-index: 4;
}
.round:nth-child(3) {
transition: all 0.8s ease-in-out;
left: 120px;
opacity: 1;
z-index: 3;
}
.round:nth-child(4) {
transition: all 0.8s ease-in-out;
left: 140px;
opacity: 1;
z-index: 2;
}
.round:nth-child(5) {
transition: all 0.8s ease-in-out;
left: 160px;
opacity: 1;
z-index: 1;
}
.round:nth-child(n+6) {
transition: all 0.8s ease-in-out;
opacity: 0;
z-index: 0;
}
.btn{
background-color: #f00;
color: #fff;
padding: 20px;
margin-bottom: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="box">
<div class="round color1"><img src="https://www.jq22.com/demo/switch202403161213/img/t1.png"></div>
<div class="round color2"><img src="https://www.jq22.com/demo/switch202403161213/img/t2.png"></div>
<div class="round color4"><img src="https://www.jq22.com/demo/switch202403161213/img/t3.png"></div>
<div class="round color3"><img src="https://www.jq22.com/demo/switch202403161213/img/t4.png"></div>
<div class="round color5"><img src="https://www.jq22.com/demo/switch202403161213/img/t5.png"></div>
<div class="round color6"><img src="https://www.jq22.com/demo/switch202403161213/img/t6.png"></div>
</div>
<div class="btn start">点击这里查看开始切换动画</div>
<div class="btn end">结束</div>
</body>
<script>
$(function(){
$('.start').click(function (){
timeInt()
})
$('.end').click(function (){
clearInterval(timer)
timer = null
})
let len = $('.round').length;
for(let i = 0;i<len;i++){
$('.round').eq(i).css('z-index',len - i)
}
// 修改此仍需改默认样式 否则默认显示和动画不匹配
const obj = {
width:100,//宽度
time:2000,//毫秒
num:3,//展示几个
interval:20,
}
var timer;
function timeInt(){
timer = setInterval(() => {
// $('.round').eq(0).css('opacity',1);
let len = $('.round').length;
for(let i = 0;i<len;i++){
let num = i * obj.interval + obj.width;
if(i== 0){//第一个
$('.round').eq(i).css('left',obj.width + 'px').css('opacity',1);
}else if(i == len - 1){//最后一个
$('.round').eq(i).css('left',num+'px').css('opacity',0);
}else if(i > obj.num){
$('.round').eq(i).css('left',num+'px').css('opacity',0);
}else{
$('.round').eq(i).css('left',num+'px');
}
}
var txt = document.getElementsByClassName('round');
setTimeout(() => {
$('.box').prepend(txt);
for(let i = 0;i<len;i++){
$('.round').eq(i).css('z-index',len - i)
}
$('.round').eq(0).css('left',0)
},500)
},obj.time)
}
})
</script>
</html>
页:
[1]