当前位置: 首页 > news >正文

前端之轮播图代码优化

1、html中固定多个img图片

在这里插入图片描述

const imges = document.querySelectorAll('.image');
let currentIndex = 0;
function showNextSlide() {imges[currentIndex].classList.remove('active');currentIndex = (currentIndex + 1) % imges.length;imges[currentIndex].classList.add('active');
}
setInterval(showNextSlide, 1000);
console.log(['Hello 笔.COOL 控制台'], imges);
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><link rel="stylesheet" href="./style.css" /></head><body><div class="card-container"><imgclass="image active"src="https://img.cdn1.vip/i/6900682318366_1761634339.webp"mode="scaleToFill"/><imgclass="image"src="https://img.cdn1.vip/i/6900682336dd2_1761634339.webp"mode="scaleToFill"/><imgclass="image"src="https://img.cdn1.vip/i/690068233d0cf_1761634339.webp"mode="scaleToFill"/></div><script src="./index.js"></script></body>
</html>
body {background-color: rgba(0, 0, 0, 0.5);
}
.card-container {position: relative;width: 100%;height: 200px;
}
.image {width: 100%;height: 100%;position: absolute;opacity: 0;transition: opacity 0.5s ease-in-out;
}
.image.active {opacity: 1;
}

3、js 动态 使一个img变换多个src

渐变动画不生效

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><link rel="stylesheet" href="./style.css" /></head><body><div class="card-container"><img class="image active" mode="scaleToFill" id="carousel" /></div><script src="./index.js"></script></body>
</html>
const img = document.getElementById('carousel');
let currentIndex = 0;
const imgUrls = ['https://img.cdn1.vip/i/6900682318366_1761634339.webp','https://img.cdn1.vip/i/6900682336dd2_1761634339.webp','https://img.cdn1.vip/i/690068233d0cf_1761634339.webp',
];
function showNextSlide() {currentIndex = (currentIndex + 1) % imgUrls.length;img.src = imgUrls[currentIndex];
}
setInterval(showNextSlide, 1000);

3、js 动态加载多个img

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><link rel="stylesheet" href="./style.css" /></head><body><div id="slides" class="card-container"></div><script src="./index.js"></script></body>
</html>
const imgContent = document.getElementById('slides');
const imgUrls = ['https://img.cdn1.vip/i/6900682318366_1761634339.webp','https://img.cdn1.vip/i/6900682336dd2_1761634339.webp','https://img.cdn1.vip/i/690068233d0cf_1761634339.webp',
];
imgUrls.forEach((src, index) => {const img = document.createElement('img');img.src = src;img.classList.add('image');if (index === 0) img.classList.add('active');imgContent.appendChild(img);
});
const imgs = document.querySelectorAll('.image');
let currentIndex = 0;
function showNextSlide() {imgs[currentIndex].classList.remove('active');currentIndex = (currentIndex + 1) % imgUrls.length;imgs[currentIndex].classList.add('active');
}
setInterval(showNextSlide, 1000);
console.log(['Hello 笔.COOL 控制台'], imges);
http://www.dtcms.com/a/541276.html

相关文章:

  • 德州网站有哪些特价服务器
  • 山西省太原建设工程信息网站网站地域分站怎么做
  • 【机器学习09】调试策略、错误分析、数据增强、迁移学习
  • 网站开发相关技术天空影院手机免费观看在线
  • 南昌哪里做网站好几百块钱建网站
  • 目标使用过期的TLS1.0 版协议
  • 笔试-计算网络信号
  • 一流的网站建设做ui必要的网站
  • 摄像头拍摄照片
  • UVa 1620 Lazy Susan
  • 中国工程建筑门户网站官网房产网签
  • RabbitMQ事务机制详解
  • 网站开发人员的水平wordpress听说对百度不友好
  • 中国网站空间西安营销策划推广公司
  • 【AI工具】dify智能体-Kimi-K2+Mermaid ,一键生成系统架构图
  • 如何利用代理 IP 构建分布式爬虫系统架构?
  • 拿别的公司名字做网站凡科网站怎么修改昨天做的网站
  • Gin 框架中路由的底层实现原理
  • 公司网站开发费进什么费用利用小米路由器mini做网站
  • h5游戏免费下载:飞机炸弹?
  • 【c++ qt】QtConcurrent与QFutureWatcher:实现高效异步计算
  • puppeteer生成PDF实践
  • Windows桌面添加我的电脑
  • 响应式网站和非响应式网站的区别wordpress 兼容php7
  • 03.OpenStack界面管理
  • 深度学习与大模型完全指南:从神经网络基础到模型训练实战
  • 神经网络发展【深度学习】
  • 类似红盟的网站怎么做阿里巴巴官网登录
  • 自创字 网站php开源网站管理系统
  • Linux Shell 中静默登录另一台机器并执行SQL文件