原生js的懒人轮播图
<style>
body{
margin: 0;
padding: 0px;
}
#carousel{
margin: auto; /* 居中 */
width: 600px; /* 设置宽度 */
position: relative; /* 相对定位 */
overflow: hidden; /* 超出隐藏 */
}
#carousel img{
position: absolute; /* 绝对定位 使图片堆叠 */
width: 600px; /* 设定大小 按比例缩放 */
transition: all .6s; /* 动画 */
opacity: 0; /* 隐藏 */
}
.imgActive{
opacity: 1 !important; /* 显示图片 最高优先级 */
}
/* 控制按钮的样式 */
#leftArrow,
#rightArrow{
position: absolute;
left: 5px;
top: 43%;
width: 25px;
height: 30px;
background-color: #eee;
display: flex;
justify-content: center;
align-items: center;
opacity: 0.7;
font-size: 20px;
cursor: pointer;
z-index: 1000;
}
#rightArrow{
left: auto;
right: 5px;
}
#sliderBtn{
position: absolute;
width: 100%;
bottom: 0;
display: flex;
justify-content: flex-end;
z-index: 1000;
}
.unitBtn{
width: 10px;
height: 10px;
background-color: #eee;
border-radius: 10px;
margin: 10px;
cursor: pointer;
}
.btnActive{
background-color: #4C98F7;
}
</style>
<body>
<!-- 轮播图容器 -->
<div id="carousel">
<img src="img/portfolio-2.jpg">
<img src="img/portfolio-3.jpg">
<img src="img/portfolio-4.jpg">
<img src="img/portfolio-5.jpg">
<img src="img/portfolio-1.jpg">
<!-- 按钮组 -->
<div id="leftArrow" class="iconfont icon-arrow-lift"></div> <!-- 左箭头切换按钮 -->
<div id="rightArrow" class="iconfont icon-arrow-right"></div> <!-- 右箭头切换按钮 -->
<div id="sliderBtn"></div> <!-- 切换按钮组 -->
</div>
</body>
<script>
var imgArr = []; // 图片数组
var curIndex = 0; // 当前显示图片
var timer = null; // 定时器
var btnList = []; // 右下角切换按钮组
function slide(targetIndex = curIndex + 1){
curIndex = targetIndex % imgArr.length; // 获取当前index
imgArr.forEach((v) => v.className = "" ); // 设置其他图片隐藏
imgArr[curIndex] .className = "imgActive"; // 设置当前index图片显示
btnList.forEach(v => v.className = "unitBtn") // 设置其他按钮为灰色
btnList[curIndex] .className = "unitBtn btnActive"; // 设置当前按钮为蓝色
}
function createBtnGroup(carousel,config){
document.getElementById("leftArrow").addEventListener('click',(e) => {
clearInterval(timer); // 清除定时器,避免手动切换时干扰
slide(curIndex-1); // 允许点击则切换上一张
timer = setInterval(() => {slide()},config.interval); // 重设定时器
})
document.getElementById("rightArrow").addEventListener('click',(e) => {
clearInterval(timer); // 清除定时器,避免手动切换时干扰
slide(curIndex+1); // 允许点击则切换下一张
timer = setInterval(() => {slide()},config.interval); // 重设定时器
})
var sliderBtn = document.getElementById("sliderBtn"); // 获取按钮容器的引用
imgArr.forEach((v,i) => {
let btn = document.createElement("div"); // 制作按钮
btn.className = i === 0 ? "unitBtn btnActive" : "unitBtn"; // 初设蓝色与灰色按钮样式
btn.addEventListener('click',(e) => {
clearInterval(timer); // 清除定时器,避免手动切换时干扰
slide(i); // // 允许点击则切换
timer = setInterval(() => {slide()},config.interval); // 重设定时器
})
btnList.push(btn); // 添加按钮到按钮组
sliderBtn.appendChild(btn); // 追加按钮到按钮容器
})
}
function eventDispose(carousel,config){
document.addEventListener('visibilitychange',function(){ // 浏览器切换页面会导致动画出现问题,监听页面切换
if(document.visibilityState=='hidden') clearInterval(timer); // 页面隐藏清除定时器
else timer = setInterval(() => {slide()},config.interval); // 重设定时器
});
carousel.addEventListener('mouseover',function(){ // 鼠标移动到容器则不切换动画,停止计时器
clearInterval(timer); // 页面隐藏清除定时器
});
carousel.addEventListener('mouseleave',function(){ // 鼠标移出容器则开始动画
timer = setInterval(() => {slide()},config.interval); // 重设定时器
});
}
(function start() {
var config = {
height: "300px", // 配置高度
interval: 4000 // 配置轮播时间间隔
}
var carousel = document.getElementById("carousel"); //获取容器对象的引用
carousel.style.height = config.height; // 将轮播容器高度设定
document.querySelectorAll("#carousel img").forEach((v,i) => {
imgArr.push(v); // 获取所有图片组成数组
v.className = i === 0 ? "imgActive" : "";
});
eventDispose(carousel,config); // 对一些浏览器事件处理
createBtnGroup(carousel,config); // 按钮组的处理
timer = setInterval(() => {slide()},config.interval); // 设置定时器定时切换
})();
</script>
/**
建议使用框架写轮播图!!!如BootStrap
官网地址:https://v5.bootcss.com/
**/
原生js的懒人轮播图的更多相关文章
- 原生JS面向对象思想封装轮播图组件
原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...
- 原生js写一个无缝轮播图插件(支持vue)
轮播图插件(Broadcast.js) 前言:写这个插件的原因 前段时间准备用vue加上网易云的nodejs接口,模拟网易云音乐移动端.因为想自己写一遍所有的代码以及加固自己的flex布局,所以没有使 ...
- 原生js实现响应式轮播图,支持电脑端点击切图,手机端滑动切图
轮播图的实现原理并不难,但是步骤有些繁琐.最近练习了一个轮播图,大部分是跟着网上的教程写的,然后自己做了一点兼容ie8的修改,加了点击切换图片的特效和手机端的滑动特效,让这个轮播图可以在响应式的网站中 ...
- 原生JS实现移动端轮播图
功能描述: 自动无缝轮播图片,底部小圆点跟图片保持一致:手指左右移动轮播图,移动距离大于50px播放下一张(或上一张),小于50px则回弹 具体功能实现: 1.定时器 自动轮播图片 先声明一个inde ...
- 用html +js+css 实现页面轮播图效果
html 页面 <html lang="en"> <head> <meta charset="UTF-8"> <met ...
- js访3d上下轮播图
js/css访3d上下轮播图 (附件) <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- 用js和jQuery做轮播图
Javascript或jQuery做轮播图 css样式 <style> a{ text-decoration:none; } .naver{ width: 100%; position:r ...
- JS框架_(Bootstrap.js)实现简单的轮播图
Bootstrap框架中 轮播(Carousel)插件是一种灵活的响应式的向站点添加滑块的方式 轮播图效果: <!DOCTYPE html> <html> <head&g ...
- js原声代码 轮播图
js轮播图 html部分:建立div,内嵌img标签,可以设置大小, <!doctype html> <html> <head> <meta charset= ...
随机推荐
- 栈和排序_via牛客网
题目 链接:https://ac.nowcoder.com/acm/contest/26886/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语 ...
- 背包问题学习笔记 / Dynamic Programming(updating)
01背包问题 朴素版:(二维数组) 状态表示: dp[i][j]:从前i个物品中选择(每个物品只能选0或1个)且总体积不超过j的集合的最大价值,则dp[n][m]就是最终答案(n:物品数量,m ...
- 在CDH webUI中部署HDFS HA
一.点击hdfs按钮进入hdfs配置界面 二.开始部署hdfs ha 三.分配角色 设置存储路径,这个可以自定义,我还在学习阶段我就默认了,之前改过,没起来,默认就好了:
- javascript引用奇趣
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Ubuntu14.04或16.04下普通用户的root权限获得
Ubuntu系统默认不允许使用root登录,因此初始root帐户是不能使用的,需要在普通账户下利用sudo权限修改root密码.然后以root帐户进行相关操作. 具体操作: 1.打开系统,用普通帐户登 ...
- Pinhole类声明和实现
针孔相机,带旋转,移动等功能. 类声明: #pragma once #ifndef __PINHOLE_HEADER__ #define __PINHOLE_HEADER__ #include &qu ...
- Bert不完全手册7. 为Bert注入知识的力量 Baidu-ERNIE & THU-ERNIE & KBert
借着ACL2022一篇知识增强Tutorial的东风,我们来聊聊如何在预训练模型中融入知识.Tutorial分别针对NLU和NLG方向对一些经典方案进行了分类汇总,感兴趣的可以去细看下.这一章我们只针 ...
- fast json 乱序问题解决过程
解决问题:保存到redis中的jsonstring在转回jsonObject的时候乱序: 解决方案:https://inlhx.iteye.com/blog/2312512 解决过程: 1 看fast ...
- Live2d Widget
写在最前 最早的时候看别人的博客很多都有一个可爱的看板娘,然后就找了教程给自己也整了一个.因为找到的教程都是稂莠不齐的,原作者自己说的也略显含糊(其实是我自己看不懂).总之秉承着一如既往的小白风格.把 ...
- github action 实现CI/CD
两种github action 打包.Net Core 项目docker镜像推送到阿里云镜像仓库 1.GitHub Actions 是什么? 大家知道,持续集成由很多操作组成,比如抓取代码.运行测试. ...