javascript焦点图左右按钮简单自动轮播
这里把css和html合在一块写了,这块代码只是布局和样式不是重点
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
font-size: 12px;
} .photo {
width: 400px;
height: 200px;
margin: 50px;
position: relative;
} .main {
width: 400px;
height: 200px;
position: relative;
} .main1 li {
width: 400px;
height: 200px;
list-style-type: none;
} .pto {
position: absolute;
left: 0;
top: 0;
} .pto1 {
width: 400px;
height: 200px;
background: red;
} .pto2 {
width: 400px;
height: 200px;
background: pink;
display: none;
} .pto3 {
width: 400px;
height: 200px;
background: blue;
display: none;
} .btn {
width: 30px;
height: 30px;
position: absolute;
} .btn1 {
left: 0;
top: 85px;
background: url("img/left.png");
} .btn2 {
right: 0;
top: 85px;
background: url("img/right.png");
} .circleBtn {
position: absolute;
top: 170px;
right: 182px;
width: 42px;
height: 10px;
zoom: 1;
} .circleBtn span {
width: 10px;
height: 10px;
margin: 0 2px;
float: left;
cursor: pointer;
background: url("img/cir.png");
} .circleBtn .light {
background: url("img/oncir.gif");
}
</style>
<script type="text/javascript" src="jiaodiantujs.js">
</script> </head> <body>
<div class="photo" id="main">
<div class="main">
<ul class="main1" id="amain">
<li class="pto pto1">one</li>
<li class="pto pto2">two</li>
<li class="pto pto3">three</li>
</ul>
</div> <span class="btn btn1" id="btn1"></span>
<span class="btn btn2" id="btn2"></span>
<!--
<div class="circleBtn" id="circleBtn">
<span class="light"></span>
<span></span>
<span></span>
</div>
-->
</div>
</body> </html>
下面是Javascript代码,主要是通过左右两个按钮来控制图片左右切换
 function $(id) {
     return typeof id === "string" ? document.getElementById(id) : id;
 }
 window.onload = function() {
     //pto变量为所展示的图片集和
     var pto = $("amain").getElementsByTagName("li");
     //btnleft和right为两个按钮
     var btnleft = $("btn1");
     var btnright = $("btn2");
     //div变量为设置停止图片显示所需
     var div = $("main");
     //idBtn为当前图片的id
     var idBtn = 0;
     //setInterval函数所需的函数名timer
     var timer = null;
     //自定义一个函数用于按钮变换
     function changeBtn(one, two) {
         one.style.background = two;
     }
     //鼠标移动至左按钮时,套用自定义函数
     btnleft.onmouseenter = function() {
         //this表示当前btnleft的背景
         changeBtn(this, "url(img/onleft.gif) no-repeat");
     }
     //鼠标离开至左按钮时,套用自定义函数
     btnleft.onmouseout = function() {
         //this同理
         changeBtn(this, "url(img/left.png) no-repeat");
     }
     //鼠标移动至右按钮时,套用自定义函数
     btnright.onmouseenter = function() {
         changeBtn(this, "url(img/onright.gif) no-repeat");
     }
     //同理
     btnright.onmouseout = function() {
         changeBtn(this, "url(img/right.png) no-repeat");
     }
     //clearPto函数表示隐藏图片
     function clearPto() {
         for (var i = 0; i < pto.length; i++) {
             pto[i].style.display = "none";
         }
     }
     //showPto函数表示显示当前图片
     function showPto(y) {
         //局部变量y,为当前鼠标点击的id
         for (var i = 0; i < pto.length; i++) {
             //console.log(y);
             //当点击的id和图片id,也就是y相等时候,显示图片
             if (i == y) {
                 pto[i].style.display = "block";
             }
         }
     }
     //鼠标左击绑定函数
     btnleft.onclick = function() {
         //当idBtn为0 ,左击则,下一张为2,所以把长度2 赋值给idBtn
         if (idBtn == 0) {
             idBtn = pto.length - 1;
         } else {
             //同理,当不为0时,所显示的为小于当前所以要--
             idBtn--;
         }
         //套用函数
         clearPto();
         //if语句结束,把idBtn代入函数中
         showPto(idBtn);
     }
     btnright.onclick = function() {
         //当idBtn小于长度,则左右为下一张,所以idBtn++
         if (idBtn < pto.length - 1) {
             idBtn++;
         } else {
             //超出长度,则下一张为0,所以把0赋值
             idBtn = 0;
         }
         clearPto();
         showPto(idBtn);
     }
     //判断语句,设置当前只有一个timer函数执行
     if (timer) {
         clearInterval(timer);
         timer = null;
     }
     //鼠标离开div,则执行函数
     div.onmouseout = start;
     //鼠标移到div,则停止自动轮播
     div.onmouseover = stop;
     start();
     //setInterval函数详情可以自行百度,不多解释
     function start() {
         timer = setInterval(function() {
             btnright.onclick();
         }, 2000);
     }
     function stop() {
         clearInterval(timer);
     }
 }
javascript焦点图左右按钮简单自动轮播的更多相关文章
- javascript焦点图自动播放
		这次是完整版,网页点开就能自动播放 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ... 
- javascript焦点图(根据图片下方的小框自动播放)
		html和css就不详细说明了,也是简单布局,通过定位把跟随图片的小框,定位到图片下方 <!DOCTYPE html> <html> <head> <meta ... 
- javascript焦点图之缓冲滚动无缝切换
		在用于实现无缝切换四张图,所以设置了6个图片就是 4,0,1,2,3,4,0 <!DOCTYPE html> <html> <head> <meta char ... 
- javascript焦点图之垂直滚动
		html代码布局,需要用到定位,不细说了 <!DOCTYPE html> <html lang="en"> <head> <meta ch ... 
- javascript焦点图自动缓冲滚动
		html中调用的js库,之前的随笔中有写,就不细说了,不明白的可以留言给我 <!DOCTYPE html> <html> <head> <meta chars ... 
- javascript焦点图(能够自己主动切换 )
		/* 思路总结: 1.实现图片滚动的function.鼠标经时候获取当前li的index.设置ndex自己主动递增的函数.实现淡入淡出效果的函数 2.整个实现效果一传递index为主线 3.我的编写代 ... 
- 8款超绚丽的jQuery焦点图动画
		随着前端技术和浏览器技术的不断发展,人们开始对网页视觉效果的要求越来越高.我们经常会在页面中看到很多炫酷的图片焦点图播放控件,有些甚至是大屏的焦点图占用大片的页面空间,从而吸引用户的眼球.本文要分享的 ... 
- 分享10款常用的jQuery焦点图插件
		爱编程一直在收集整理编程相关的知识和解决方案,今天小编为大家带来10款非常常用的jquery焦点图插件. 1.jQuery可自动播放动画的焦点图插件 之前我们已经分享过很多非常实用的jQuery焦点图 ... 
- 西山居首页jQuery焦点图代码
		西山居首页jQuery焦点图代码是一款带文字描述,左右箭头,索引按钮,自动轮播切换的jQuery特效代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div style ... 
随机推荐
- 前端知识点一HTML相关知识点
			1.浏览器页面有哪三层构成,分别是什么,作用是什么? 构成:结构层.表示层.行为层 分别是:HTML.CSS.JavaScript 作用:HTML实现页面结构,CSS完成页面的表现与风格,JavaSc ... 
- (一)初步了解python
			python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. Py ... 
- hdu1027
			#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const ... 
- Mac OS启动服务优化高级篇(launchd tuning)
			Mac下的启动服务主要有三个地方可配置:1,系统偏好设置->帐户->登陆项2,/System/Library/StartupItems 和 /Library/StartupItems/3, ... 
- event system
			事件的概念 简单来说, 就是应用程序感兴趣的应用内部或者外部的活动结果. 在Qt中, 使用QEvent 抽象这些活动. 事件驱动模型 事件驱动模型现在在计算机很多领域都有使用. 例如 BSD sock ... 
- Math.round(),Math.ceil(),Math.floor()的区别
			1.Math.round():根据"round"的字面意思"附近.周围",可以猜测该函数是求一个附近的整数,看下面几个例子就明白. 小数点后第一位<5 正 ... 
- Byte Array to Hexadecimal String
			Lookup Text: 23,879.41 (20.8X faster) Sentence: 1.15 (23.9X faster) /// <summary> /// Hex stri ... 
- 去掉word冗余格式 java正则表达式
			word转换html时,会留下很多格式,有些格式并不是我们所需要的,然而这些格式比真正的文章内容还要多,严重影响页面的加载速度,因此就需要找个一个好的解决方案把这些多余的格式个去掉.网上有很多去除wo ... 
- android源码查看所有分支切换分支
			cd .repo/manifests git branch -a repo init -b android-4.1.2_r1 repo sync 
- sybase 修改用户密码
			命令行输入 isql -Usa -P123456 将sa口令设置为NULL(当前口令为"123456"): sp_password '123456',NULL,sa 
