您的位置:首页 > Web前端 > JavaScript

js原生之焦点图转换加定时器实例

2016-12-12 00:00 639 查看
在jQuery之焦点图转换-左右的基础上,将jQuery代码改成js原生,并添加定时器(setInterval()和clearInterval())

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>焦点图转换--原生和定时器</title>
<link rel="stylesheet" href="css/reset.css">
<style type="text/css">
.pic-show{width: 480px;overflow: hidden;}
#pic{width: 1920px;height: 320px;position: relative;}
#pic img{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: 1px solid #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>
<div class="pic-show">
<div id="pic">
<img src="images/1.jpg" alt="">
<img src="images/2.jpg" alt="">
<img src="images/3.jpg" alt="">
<img src="images/4.jpg" alt="">
</div>
<img id="prev" src="images/slider-prev.png" alt="">
<img id="next" src="images/slider-next.png" alt="">
</div>
<ul id="list">
<li><a href="#" title="日落"></a></li>
<li><a href="#" title="钢琴"></a></li>
<li><a href="#" title="大海"></a></li>
<li><a href="#" title="秋色"></a></li>
</ul>
<p id="p">这是一段测试文字</p>
<script src="js/jquery-3.0.0.js"></script>
<script type="text/javascript">
var num=0;
function G(id){
return document.getElementById(id)
}
var pic = G('pic')
var next = G('next')
var prev = G('prev')
var p = G('p')
var list = G('list')
var arr = G('list').getElementsByTagName('a')
       //点击next
next.onclick=function(){
if(num<3){
num=num+1;
}
else{
num=0;
}
console.log(num);
var mlNum=num * -480+'px';
pic.style.marginLeft=mlNum;
for(var j=0;j<arr.length;j++){
arr[j].style.backgroundColor='#ccc';
}
arr[num].style.backgroundColor="#094a99";
event.preventDefault();

var txt=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);
var mlNum2=num * -480+'px';
pic.style.marginLeft=mlNum2;
for(var j=0;j<arr.length;j++){
arr[j].style.backgroundColor='#ccc';
}
arr[num].style.backgroundColor="#094a99";
event.preventDefault();
var txt=arr[num].getAttribute("title");
console.log(txt);
p.textContent=txt;
}
for(var i=0;i<arr.length;i++){
arr[i].index=i;//创建索引值
arr[i].onclick=function(event){  num=this.index;
var mlNum3=num * -480+'px';       pic.style.marginLeft=mlNum3;
for(var j=0;j<arr.length;j++){
arr[j].style.backgroundColor='#ccc';
}
this.style.backgroundColor='#094a99';
event.preventDefault();
var txt=this.getAttribute("title");
p.textContent=txt;
}
}
    //函数封装
function lunbo(){
if(num<3){
num=num+1;
}
else{
num=0;
}
console.log(num);
var mlNum=num * -480+'px';
pic.style.marginLeft=mlNum;
for(var j=0;j<arr.length;j++){
arr[j].style.backgroundColor='#ccc';
}
arr[num].style.backgroundColor="#094a99";
event.preventDefault();
var txt=arr[num].getAttribute("title");
console.log(txt);
p.textContent=txt;
}
    //通过定时器调用封装好的函数
var stop=setInterval(lunbo,1500);
    //鼠标放上和离开时定时器的状态
pic.onmouseenter=function(){
clearInterval(stop)
}
pic.onmouseleave=function(){
stop=setInterval(lunbo,1500)
}
</script>
</body>
</html>


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

您可能感兴趣的文章:

javascript+HTML5自定义元素播放焦点图动画
js淡入淡出焦点图幻灯片效果代码分享
js实现三张图(文)片一起切换的banner焦点图
JS+CSS实现淡入式焦点图片幻灯切换效果的方法
JS焦点图切换,上下翻转
javascript 封装的一个实用的焦点图切换效果
实用的js 焦点图切换效果 结构行为相分离
CSS+Js遮罩效果的TAB及焦点图片切换(推荐)
JavaScript暂停和继续定时器的实现方法
javascript中SetInterval与setTimeout的定时器用法
浅谈Node.js中的定时器
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  js 焦点图 定时器