首页 > 其他分享 >原生js贼强轮播图

原生js贼强轮播图

时间:2023-02-14 15:38:54浏览次数:52  
标签:原生 style curr 轮播 oLi js let currentIndex backgroundColor



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}

body {
overflow: hidden;
}

#container {
width: 100%;
min-height: 100px;
}

.p {
font-size: 40px;
color: transparent;
text-align: center;
font-weight: bold;
background: linear-gradient(60deg, pink 10%, orange, red, green 30%, blue, black);
mix-blend-mode: color;
-webkit-background-clip: text;
text-shadow: -4px 1px 2px #ffffff;
}

.warp {
width: 400px;
height: 200px;
border: 1px solid #333333;
margin: 70px auto;
overflow: hidden;
position: relative;
}

ul {
list-style: none;
}

.trans {
transition: margin-top 1s ease;
}

ul li {
width: 400px;
height: 200px;
float: left;
}

ul li img {
width: 100%;
height: 100%;
}

ol {
position: absolute;
list-style: none;
top: 180px;
left: 160px;
}

ol li {
margin-right: 5px;
float: left;
width: 18px;
height: 5px;
border: 1px solid #ccc;
background-color: white;
border-radius: 10px;
cursor: pointer;
}
.left{
position: absolute;
display: none;
width: 30px;
height: 40px;
top: 80px;
transform: rotate(90deg);
cursor: pointer;
opacity: 0.5;
}
.left img {
width: 100%;
height: 100%;
}
.left:hover{
opacity: 1;
}
.right{
position: absolute;
display: none;
width: 30px;
height: 40px;
top: 80px;
right: 0;
transform: rotate(270deg);
cursor: pointer;
opacity: 0.5;
}
.right img {
width: 100%;
height: 100%;
}
.right:hover{
opacity: 1;
}
</style>
</head>
<body>
<div id="container">
<p class="p">宇宙无敌上天遁地轮播图</p>
<div class="warp">
<span class="left"><img src="../../img/arrow_bottom.png"></span>
<span class="right"><img src="../../img/arrow_bottom.png"></span>
<ul>
<li><img src="../../img/xhc1.jpg"></li>
<li><img src="../../img/xhc3.jpg"></li>
<li><img src="../../img/xhc4.jpg"></li>
<li><img src="../../img/xhc5.jpg"></li>
<li><img src="../../img/xhc1.jpg"></li>
</ul>
<ol>
<li index="1"></li>
<li index="2"></li>
<li index="3"></li>
<li index="4"></li>
</ol>
</div>
</div>
<script>
(function() {
let warp = document.querySelector('.warp');
let oUl = document.querySelector('ul');
let oLi = document.querySelectorAll('ol li');
let allLi = document.querySelectorAll('ul li').length-1;
let left = document.querySelector('.left');
let right = document.querySelector('.right');
let currentIndex = 1;
let T = null;
console.log(oLi);
oLi[0].style.backgroundColor = '#333333';
T = setInterval(begin, 2000);
//鼠标移入移出
warp.addEventListener('mouseenter', () => {
left.style.display = 'inline-block';
right.style.display = 'inline-block';
clearInterval(T);
T = null;
});
warp.addEventListener('mouseleave', () => {
left.style.display = 'none';
right.style.display = 'none';
T = setInterval(begin, 2000);
});
//li 点击跳转事件
oLi.forEach((li,index)=>{
li.onclick = function(){
oUl.style.marginTop = -200 * index + 'px';//index是点击ol的角标
oUl.className = 'trans';
currentIndex=index;
for(let i = 0; i<li.parentNode.children.length; i++){
oLi[i].style.backgroundColor = '#ffffff';
}
li.style.backgroundColor = '#333333';
}
})

//左右箭头点击跳转
let curr = 0;
left.onclick = function(){
console.log(currentIndex);
if(currentIndex<1){
currentIndex = 4
}
curr=currentIndex;
curr--;
console.log(curr);
oUl.style.marginTop = -200 * curr + 'px';
currentIndex=curr;
for(let i = 0; i<oLi[0].parentNode.children.length; i++){
oLi[i].style.backgroundColor = '#ffffff';
}
oLi[curr].style.backgroundColor = '#333333';
}
right.onclick = function(){
if(currentIndex>=3){
currentIndex = -1
}
curr=currentIndex;
curr++;
oUl.style.marginTop = -200 * curr + 'px';
currentIndex=curr;
for(let i = 0; i<oLi[0].parentNode.children.length; i++){
oLi[i].style.backgroundColor = '#ffffff';
}
oLi[curr].style.backgroundColor = '#333333';
console.log(currentIndex);
}

//轮播方法
function begin() {
if (currentIndex > allLi) {
oUl.className = '';
oLi[0].style.backgroundColor = '#333333';
oLi[currentIndex - 2].style.backgroundColor = '#ffffff';
oUl.style.marginTop = 0 + 'px';
currentIndex = 1;
} else if (currentIndex < allLi) {
if(currentIndex===0){
currentIndex=1;
}
oUl.className = 'trans';
oUl.style.marginTop = -200 * currentIndex + 'px';
oLi[currentIndex].style.backgroundColor = '#333333';
oLi[currentIndex - 1].style.backgroundColor = '#ffffff';
currentIndex++;
} else if (currentIndex === allLi) {
oUl.style.marginTop = -200 * currentIndex + 'px';
oLi[0].style.backgroundColor = '#333333';
oLi[allLi-1].style.backgroundColor = '#ffffff';
currentIndex++;
}
}
})()
</script>
</body>
</html>

来找我玩啊

原生js贼强轮播图_javascript


标签:原生,style,curr,轮播,oLi,js,let,currentIndex,backgroundColor
From: https://blog.51cto.com/u_15964288/6056955

相关文章

  • jQuery对象和JS对象区别与转换jQuery时间绑定&入口函数&样式控制
    jQuery对象和JS对象区别与转换JQuery对象和JS对象区别与转换1.JQuery对象在操作时,更加方便。2.JQuery对象和js对象方法不通用的3.两者相互转换jq......
  • 云原生周刊 | 开源领导者应该如何应对碎片化挑战?
    LinuxFundation发布了一份关于开源开发中的碎片化问题的报告《实现全球协作:开源领导者如何应对碎片化挑战》,该报告由华为在美国的研发部门Futurewei赞助。报告指出,虽然......
  • 云原生时代顶流消息中间件Apache Pulsar部署实操-上
    @目录安装运行时Java版本推荐LocallyStandalone集群启动验证部署分布式集群部署说明初始化集群元数据部署BookKeeper部署BrokerAdmin客户端和验证TieredStorage(层级存储......
  • JS 大文件上传下载解决方案
    ​前言一、SpringMVC简介1.1、SpringMVC引言为了使Spring有可插入的MVC架构,SpringFrameWork在Spring基础上开发SpringMVC框架,从而在使用Spring进行WEB开发时可以......
  • JSON化发送数据
    let msg = JSON.stringify({            'ClientName': name,            'City': City,            'ClientAddress': ad......
  • Python爬虫:从js逆向了解西瓜视频的下载链接的生成
    文章内容在csdn上,链接为:Python爬虫:从js逆向了解西瓜视频的下载链接的生成参考代码为:importrequestsfromcrawlers.userAgentimportuseragentfromlxmlimportetre......
  • 从 Newtonsoft.Json 迁移到 System.Text.Json
    一.写在前面System.Text.Json是.NETCore3及以上版本内置的Json序列化组件,刚推出的时候经常看到踩各种坑的吐槽,现在经过几个版本的迭代优化,提升了易用性,修复了各种......
  • JQuery对象和JS对象的区别与转换
    JQuery对象和JS对象区别与转换1JQuery对象在操作时更加方便2JQuery对象和js对象方法不通用的3两者相互转换jq转js:jq对象[索引]或jq对象,get......
  • JMeter java模拟多用户高并发请求测试(json,form表单)
     1.情景展示在实际开发过程中,需要进行测试的时候,往往需要进行压力测试,或者高并发情况下,同时对一张表数据进行修改、读取操作,程序会不会出现多个用户取出的数据一致,或者......
  • node.js 定时任务/重复任务
    文章目录​​git​​​​参数​​​​定时任务​​​​每天0点执行一次数据统计任务​​​​每半个小时执行一次数据统计任务​​​​重复任务​​​​指定执行时间氛围​​......