js原生之焦点图转换加定时器实例
在jQuery之焦点图转换-左右的基础上,将jQuery代码改成js原生,并添加定时器(setInterval()和clearInterval())
<!doctypehtml> <htmllang="en"> <head> <metacharset="UTF-8"> <title>焦点图转换--原生和定时器</title> <linkrel="stylesheet"href="css/reset.css"> <styletype="text/css"> .pic-show{width:480px;overflow:hidden;} #pic{width:1920px;height:320px;position:relative;} #picimg{display:block;float:left;} .pic-show>img{display:block;cursor:pointer;opacity:0.8;position:absolute;top:142px;left:30px;} .pic-show>img:last-child{display:block;position:absolute;left:414px;} ul{width:120px;height:18px;position:absolute;top:280px;left:185px;} li{float:left;width:20px;height:18px;margin-left:5px;} a{display:block;width:20px;height:18px;text-decoration:none;border:1pxsolid#ccc;border-radius:50%;background-color:#ccc;opacity:0.6;} a:hover{background-color:#094a99;} .aCss{background-color:#094a99;} p{width:480px;text-align:center;} </style> </head> <body> <divclass="pic-show"> <divid="pic"> <imgsrc="images/1.jpg"alt=""> <imgsrc="images/2.jpg"alt=""> <imgsrc="images/3.jpg"alt=""> <imgsrc="images/4.jpg"alt=""> </div> <imgid="prev"src="images/slider-prev.png"alt=""> <imgid="next"src="images/slider-next.png"alt=""> </div> <ulid="list"> <li><ahref="#"title="日落"></a></li> <li><ahref="#"title="钢琴"></a></li> <li><ahref="#"title="大海"></a></li> <li><ahref="#"title="秋色"></a></li> </ul> <pid="p">这是一段测试文字</p> <scriptsrc="js/jquery-3.0.0.js"></script> <scripttype="text/javascript"> varnum=0; functionG(id){ returndocument.getElementById(id) } varpic=G('pic') varnext=G('next') varprev=G('prev') varp=G('p') varlist=G('list') vararr=G('list').getElementsByTagName('a') //点击next next.onclick=function(){ if(num<3){ num=num+1; } else{ num=0; } console.log(num); varmlNum=num*-480+'px'; pic.style.marginLeft=mlNum; for(varj=0;j<arr.length;j++){ arr[j].style.backgroundColor='#ccc'; } arr[num].style.backgroundColor="#094a99"; event.preventDefault(); vartxt=arr[num].getAttribute("title"); console.log(txt); p.textContent=txt; } //点击prev prev.onclick=function(){ if(num>0){ num=num-1; } else{ num=3; } console.log(num); varmlNum2=num*-480+'px'; pic.style.marginLeft=mlNum2; for(varj=0;j<arr.length;j++){ arr[j].style.backgroundColor='#ccc'; } arr[num].style.backgroundColor="#094a99"; event.preventDefault(); vartxt=arr[num].getAttribute("title"); console.log(txt); p.textContent=txt; } for(vari=0;i<arr.length;i++){ arr[i].index=i;//创建索引值 arr[i].onclick=function(event){num=this.index; varmlNum3=num*-480+'px';pic.style.marginLeft=mlNum3; for(varj=0;j<arr.length;j++){ arr[j].style.backgroundColor='#ccc'; } this.style.backgroundColor='#094a99'; event.preventDefault(); vartxt=this.getAttribute("title"); p.textContent=txt; } } //函数封装 functionlunbo(){ if(num<3){ num=num+1; } else{ num=0; } console.log(num); varmlNum=num*-480+'px'; pic.style.marginLeft=mlNum; for(varj=0;j<arr.length;j++){ arr[j].style.backgroundColor='#ccc'; } arr[num].style.backgroundColor="#094a99"; event.preventDefault(); vartxt=arr[num].getAttribute("title"); console.log(txt); p.textContent=txt; } //通过定时器调用封装好的函数 varstop=setInterval(lunbo,1500); //鼠标放上和离开时定时器的状态 pic.onmouseenter=function(){ clearInterval(stop) } pic.onmouseleave=function(){ stop=setInterval(lunbo,1500) } </script> </body> </html>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!