javascript焦点图自动播放
这次是完整版,网页点开就能自动播放
- <!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;
- }
- .pto4 {
- width: 400px;
- height: 200px;
- background: #f2ee36;
- 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: 172px;
- width: 56px;
- 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>
- </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>
- <li class="pto pto4">four</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>
- <span></span>
- </div>
- </div>
- </body>
- </html>
下面的是js代码,函数的定义都有注释,不明白的可以看前面的单个焦点图的随笔,图片是img文件夹下
- <script>
- function $(id) {
- return typeof id === "string" ? document.getElementById(id) : id;
- }
- var btnleft = $("btn1");
- var btnright = $("btn2");
- //1.先做按钮特效
- //定义自定义函数用于按钮
- function onBtn(one, two) {
- one.style.background = two;
- }
- //当鼠标移动至左边按钮调用onBtn函数
- btnleft.onmouseenter = function() {
- onBtn(this, "url(img/onleft.gif) no-repeat");
- }
- //同理
- btnleft.onmouseleave = function() {
- onBtn(this, "url(img/left.png) no-repeat");
- }
- //当鼠标移动至右边按钮调用onBtn函数
- btnright.onmouseenter = function() {
- onBtn(this, "url(img/onright.gif) no-repeat");
- }
- //同理
- btnright.onmouseleave = function() {
- onBtn(this, "url(img/right.png) no-repeat");
- }
- //2.设置图片,小框同时移动
- //定义变量
- var pto = $("amain").getElementsByTagName("li");
- var cirBtn = $("circleBtn").getElementsByTagName("span");
- var index = 0;
- var timer = null;
- var div = $("main");
- //设置定时器timer
- //timer = setInterval(autoPlayRight, 2000);
- //设置自动函数
- function autoPlayRight() {
- if (index < pto.length - 1) {
- index++;
- } else {
- index = 0;
- }
- //调用清除图片函数
- clearPto();
- //调用显示图片函数,代入参数index
- showPto(index);
- //调用清除小框函数
- clearBtn();
- //调用显示小框函数,代入参数index
- showBtn(index);
- }
- //定义清除图片的函数
- function clearPto() {
- for (var i = 0; i < pto.length; i++) {
- pto[i].style.display = "none";
- }
- }
- //定义显示图片的函数
- function showPto(x) {
- for (var i = 0; i < pto.length; i++) {
- if (i == x) {
- pto[i].style.display = "block";
- }
- }
- }
- //定义清除小框的函数
- function clearBtn() {
- for (var i = 0; i < cirBtn.length; i++) {
- cirBtn[i].className = "";
- }
- }
- //定义显示小框的函数
- function showBtn(y) {
- for (var i = 0; i < cirBtn.length; i++) {
- if (i == y) {
- cirBtn[i].className = "light";
- }
- //这里重要了,如果不把返回值赋值给index,鼠标离开小框,
- //自动循环会执行上一次的循环,而不是本次鼠标离开时,显示下一张图片
- index = y;
- }
- }
- //3.设置鼠标点击事件
- btnright.onclick = autoPlayRight;
- btnleft.onclick = btnLeft;
- function btnLeft() {
- if (index == 0) {
- index = pto.length - 1;
- } else {
- index--;
- }
- //调用清除图片函数
- clearPto();
- //调用显示图片函数,代入参数index
- showPto(index);
- //调用清除小框函数
- clearBtn();
- //调用显示小框函数,代入参数index
- showBtn(index);
- }
- //4.设置鼠标移动至焦点图上时候停止自动播放
- //把定时器放入自定义函数方便调用
- function start() {
- timer = setInterval(autoPlayRight, 2000);
- }
- //把清除定时器放入自定义函数便于调用
- function stop() {
- clearInterval(timer);
- }
- //鼠标进入焦点图则停止自动播放
- div.onmouseenter = stop;
- //鼠标离开则开始自动
- div.onmouseleave = start;
- //设置当前只有一个定时器
- if (timer) {
- clearInterval(timer);
- timer = null;
- }
- //设置网页点开时就自动播放
- start();
- //5.设置图片随小框变化
- for (var i = 0; i < cirBtn.length; i++) {
- cirBtn[i].id = i;
- cirBtn[i].onmouseenter = function() {
- clearInterval(timer);
- //调用清除图片函数
- clearPto();
- //调用显示图片函数,代入参数index
- showPto(this.id);
- //调用清除小框函数
- clearBtn();
- //调用显示小框函数,代入参数index
- showBtn(this.id);
- }
- }
</script>
javascript焦点图自动播放的更多相关文章
- javascript焦点图自动缓冲滚动
html中调用的js库,之前的随笔中有写,就不细说了,不明白的可以留言给我 <!DOCTYPE html> <html> <head> <meta chars ...
- 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焦点图左右按钮简单自动轮播
这里把css和html合在一块写了,这块代码只是布局和样式不是重点 <!DOCTYPE html> <html> <head> <meta charset=& ...
- javascript焦点图(能够自己主动切换 )
/* 思路总结: 1.实现图片滚动的function.鼠标经时候获取当前li的index.设置ndex自己主动递增的函数.实现淡入淡出效果的函数 2.整个实现效果一传递index为主线 3.我的编写代 ...
- 使用 iscroll 实现焦点图无限循环
现在大家应该都看到过焦点图轮播的效果,这个效果是什么样我就不截图了.昨天做练习,练习要求是使用iscroll实现焦点图的无限循环滚动,并且当手指触摸焦点图后,停止焦点图的循环滚动.第一次接触iscro ...
- jQuery可自动播放动画焦点图插件Koala
Koala是一款简单而实用的jQuery焦点图幻灯片插件,焦点图不仅可以在播放图片的时候让图片有淡入淡出的动画效果,而且图片可以自动播放.该jQuery焦点图的每一张图片都可以设置文字描述,并浮动在图 ...
- JavaScript焦点轮播图
在慕课学习了JavaScript焦点轮播图特效,在此做一个整理. 首先是html结构,我用的是本地同文件夹下的三张图片,多出来的第一张(pic3副本)和最后一张图片(pic1副本)是为了实现无缝切换效 ...
随机推荐
- CodeForces 712D Memory and Scores
$dp$,前缀和. 记$dp[i][j]$表示$i$轮结束之后,两人差值为$j$的方案数. 转移很容易想到,但是转移的复杂度是$O(2*k)$的,需要优化,观察一下可以发现可以用过前缀和来优化. 我把 ...
- 详细介绍Java垃圾回收机制
垃圾收集GC(Garbage Collection)是Java语言的核心技术之一,之前我们曾专门探讨过Java 7新增的垃圾回收器G1的新特性,但在JVM的内部运行机制上看,Java的垃圾回收原理与机 ...
- hibernate中的sql语句
hibernate的hql查询语句总结 在这里通过定义了三个类,Special.Classroom.Student来做测试,Special与Classroom是一对多,Classroom与Stud ...
- php 问答
1,如何设置长生命期的session ? 将 session.cookie_lifetime ,session.gc_maxlifetime 的时间设置长一点. 2,为什么初始化session的时候报 ...
- F4IF_FIELD_VALUE_REQUEST 和 F4IF_INT_TABLE_VALUE_REQUEST的不同
F4IF_FIELD_VALUE_REQUEST 和 F4IF_INT_TABLE_VALUE_REQUEST的不同 F4IF_FIELD_VALUE_REQUEST主要功能是将表里的字段对应的sea ...
- Request for the permission of type异常
调用wcf调用的时候引发一个错误,错误信息如下: <Message>Request for the permission of type 'System.Configuration.Con ...
- 用border做一个移动端常见的返回按钮
第一步 .hs1{ float: left; .mt(.25rem); .ml(.12rem); width: .3rem; height: .3rem; border-top: 2px solid ...
- xshell 注册码
Xshell 5 注册码: 101210-450789-147200Xftp 5 注册码:101210-450789-147200 Xmanager 5 注册码:101210-450789-14720 ...
- fork()子进程与waitpid()
#!/usr/bin/perl use warnings; use strict; use POSIX ":sys_wait_h"; $SIG{CHLD} = sub{ my $p ...
- python多进程,以及进程池并发
模拟多进程 #!/usr/bin/env python#-*- coding:utf-8 -*-import timefrom multiprocessing import Process def s ...