js焦点轮播图
汇集网上焦点轮播图的实现方式,自己试了下,不过鼠标悬浮停止动画和鼠标离开动画播放好像没生效,不太明白,最后两行代码中,为什么可以直接写stop和play。不用加括号调用函数么?求懂的大神指点!
所用知识点:
1.DOM操作
2.定时器
3.事件运用
4.Js动画
5.函数递归
6.无限滚动大法(可以用js实现一个假图的制作。不过说实话一直理解不了假图的作用原理)
<style>
* {
margin: 0;
padding: 0; } a {
text-decoration: none;
} body {
padding: 20px;
} #container {
width: 600px; /*这里600x400是图片的宽高*/
height: 400px;
border: 3px solid #333;
overflow: hidden; /*隐藏溢出的图片,因为图片左浮动,总宽度为4200*/
position: relative;
} #list {
width: 4200px; /*这里设置7张图片总宽度*/
height: 400px;
position: absolute; /*基于父容器container进行定位*/
z-index: 1;
} #list img {
float: left;
width:610px;
} #buttons {
position: absolute;
height: 10px;
width: 100px;
z-index: 2; /*按钮在图片的上面*/
bottom: 20px;
left: 250px;
} #buttons span {
cursor: pointer;
float: left;
border: 1px solid #fff;
width: 10px;
height: 10px;
border-radius: 50%;
background: #333;
margin-right: 5px;
} #buttons .on {
background: orangered; /*选中的按钮样式*/
} .arrow {
cursor: pointer;
display: none; /*左右切换按钮默认先隐藏*/
line-height: 39px;
text-align: center;
font-size: 36px;
font-weight: bold;
width: 40px;
height: 40px;
position: absolute;
z-index: 2;
top: 180px;
background-color: RGBA(0, 0, 0, .3);
color: #fff;
} .arrow:hover {
background-color: RGBA(0, 0, 0, .7);
} #container:hover .arrow {
display: block; /*当鼠标放上去容器上面就显示左右切换按钮*/
} #prev {
left: 20px;
} #next {
right: 20px;
}
</style>
<div id="container">
<div id="list" style="left: -600px;">
<img src="5.jpg" alt="1"/>
<img src="1.jpg" alt="1"/>
<img src="2.jpg" alt="2"/>
<img src="3.jpg" alt="3"/>
<img src="4.jpg" alt="4"/>
<img src="5.jpg" alt="5"/>
<img src="1.jpg" alt="5"/>
</div>
<div id="buttons">
<span index="1" class="on"></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<a href="javascript:void(0);" id="prev" class="arrow"><</a>
<a href="javascript:void(0);" id="next" class="arrow">></a></div>
<script>
window.onload = function(){
var container=document.getElementById('container');
var list=document.getElementById('list');
var buttons=document.getElementById('buttons').getElementsByTagName('span');
var next=document.getElementById('next');
var prev=document.getElementById('prev');
var index=1; var len=5;//图片的数量
var animated=false;//用于判断切换是否进行
var interval=3000; //自动播放定时器的秒数,3秒
var timer; //定时器
// 切换动画
function animate(offset){
animated=true; //切换进行中
var time=300; //位移总时间
var inteval=10; //位移间隔时间
var speed=offset/(time/inteval); //每次位移量
var newLeft=parseInt(list.style.left)+offset;
var go=function(){
if((speed<0&&parseInt(list.style.left)>newLeft)||(speed>0&&parseInt(list.style.left)>newLeft)){
list.style.left=parseInt(list.style.left)+speed+'px';
setTimeout(go,inteval);
}else{
animated=false;
list.style.left=newLeft+'px';
if(newLeft<-3000){
list.style.left=-600+'px';
}
if(newLeft>-600){
list.style.left=-3000+'px';
}
}
}
go();
/*var newLeft=parseInt(list.style.left)+offset;
list.style.left=newLeft+'px';
if(newLeft<-3000){
list.style.left=-600+'px';
}
if(newLeft>-600){
list.style.left=-3000+'px';
}*/
} // 为按钮添加样式
function showButton(){
for(var i=0;i<buttons.length;i++){
if(buttons[i].className=='on'){
buttons[i].className='';
break;
}
}
buttons[index-1].className='on';
}
next.onclick=function(){
if(animated){
return;
}
if(index==5){
index=1;
}else{
index+=1;
}
animate(-600);
showButton();
} prev.onclick=function(){
if(animated){
return;
}
if(index==1){
index=5;
}else{
index-=1;
}
animate(600);
showButton();
} // 通过循环为按钮添加点击事件
for(var i=0;i<buttons.length;i++){
buttons[i].onclick=function(){
if(animated){
return;
}
// 当继续点击当前按钮的时候,不进行切换
if(this.className == 'on'){
return;
}
// 通过获取按钮标签的自定义属性Index 得到索引值,再计算差值
var myIndex = parseInt(this.getAttribute('index'));
//真正的偏移量
var offset = -600*(myIndex-index);
animate(offset);
index=myIndex;
showButton();
}
// 自动播放
function play(){
timer=setTimeout(function(){
next.onclick();
play();
},interval)
}
function stop(){
clearTimeout(timer);
}
container.onmouseover=stop;
container.onmouseout=play; play();
}
}
</script>
<script>
window.onload = function(){
var container=document.getElementById('container');
var list=document.getElementById('list');
var buttons=document.getElementById('buttons').getElementsByTagName('span');
var next=document.getElementById('next');
var prev=document.getElementById('prev');
var index=; var len=;//图片的数量
var animated=false;//用于判断切换是否进行
var interval=; //自动播放定时器的秒数,3秒
var timer; //定时器
// 切换动画
function animate(offset){
animated=true; //切换进行中
var time=; //位移总时间
var inteval=; //位移间隔时间
var speed=offset/(time/inteval); //每次位移量
var newLeft=parseInt(list.style.left)+offset;
var go=function(){
if((speed<&&parseInt(list.style.left)>newLeft)||(speed>&&parseInt(list.style.left)>newLeft)){
list.style.left=parseInt(list.style.left)+speed+'px';
setTimeout(go,inteval);
}else{
animated=false;
list.style.left=newLeft+'px';
if(newLeft<-){
list.style.left=-+'px';
}
if(newLeft>-){
list.style.left=-+'px';
}
}
}
go();
/*var newLeft=parseInt(list.style.left)+offset;
list.style.left=newLeft+'px';
if(newLeft<-3000){
list.style.left=-600+'px';
}
if(newLeft>-600){
list.style.left=-3000+'px';
}*/
} // 为按钮添加样式
function showButton(){
for(var i=;i<buttons.length;i++){
if(buttons[i].className=='on'){
buttons[i].className='';
break;
}
}
buttons[index-].className='on';
}
next.onclick=function(){
if(animated){
return;
}
if(index==){
index=;
}else{
index+=;
}
animate(-);
showButton();
} prev.onclick=function(){
if(animated){
return;
}
if(index==){
index=;
}else{
index-=;
}
animate();
showButton();
} // 通过循环为按钮添加点击事件
for(var i=;i<buttons.length;i++){
buttons[i].onclick=function(){
if(animated){
return;
}
// 当继续点击当前按钮的时候,不进行切换
if(this.className == 'on'){
return;
}
// 通过获取按钮标签的自定义属性Index 得到索引值,再计算差值
var myIndex = parseInt(this.getAttribute('index'));
//真正的偏移量
var offset = -*(myIndex-index);
animate(offset);
index=myIndex;
showButton();
}
// 自动播放
function play(){
timer=setTimeout(function(){
next.onclick();
play();
},interval)
}
function stop(){
clearTimeout(timer);
}
container.onmouseover=stop;
container.onmouseout=play; play();
}
}
</script>
js焦点轮播图的更多相关文章
- 原生js焦点轮播图
原生js焦点轮播图主要注意这几点: 1.前后按钮实现切换,同时注意辅助图2.中间的button随着前后按钮对应切换,同时按button也能跳转到相应的index3.间隔调用与无限轮播.4.注意在动画时 ...
- 封装一个简单的原生js焦点轮播图插件
轮播图实现的效果为,鼠标移入左右箭头会出现,可以点击切换图片,下面的小圆点会跟随,可以循环播放(为了方便理解,没有补2张图做无缝轮播).本篇文章的主要目的是分享封装插件的思路. 轮播图我一开始是写成非 ...
- 原生js焦点轮播图的实现
继续学习打卡,武汉加油,逆战必胜!今日咱们主要探讨一下原生js写轮播图的问题, 简单解析一下思路: 1,首先写好css样式问题 2,考虑全局变量:自动播放的定时器,以及记录图片位置的角标Index 2 ...
- 使用JS实现轮播图的效果
其中的一些css样式代码就省略了,下面只把结构层html.行为层js的代码展示出来 ,看代码说事. 一.简单的轮播图 <div class="box" id="bo ...
- JS---案例---左右焦点轮播图(tb)
案例---左右焦点轮播图(tb) <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- JavaScript焦点轮播图
在慕课学习了JavaScript焦点轮播图特效,在此做一个整理. 首先是html结构,我用的是本地同文件夹下的三张图片,多出来的第一张(pic3副本)和最后一张图片(pic1副本)是为了实现无缝切换效 ...
- 原生js实现轮播图
原生js实现轮播图 很多网站上都有轮播图,但找到一个系统讲解的却很难,因此这里做一个简单的介绍,希望大家都能有所收获,如果有哪些不正确的地方,希望大家可以指出. 原理: 将一些图片在一行中平铺,然后计 ...
- js实现轮播图效果(附源码)--原生js的应用
1.js实现轮播图效果 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- js编写轮播图,广告弹框
1.轮播图 js编写轮播图,需要用到setInterval(计时器):先给一个div,里面放轮播图的图片,将轮播图的图片明明为相同样式的:如:banner1.jpg,banner2.jpg,banne ...
随机推荐
- JMeter学习笔记(六)-负载与监听
1. 场景设计 场景设计的原则:忠于用户实际操作,组合用户的各种操作到场景中来. JMeter场景主要通过线程组设置来完成的,对于复杂场景还需要与逻辑控制器配合完成. 2.场景设置 JMeter线程组 ...
- js正则匹配中文
alert(/[\u4e00-\u9fa5]{4}/.test("司徒正美"))//true alert(/[\u4e00-\u9fa5]{4}/.test("司正美&q ...
- mysql5.7启动slave报错 ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
原因:检查my.cnf,原来没指定relay_log,mysql默认产生的relay_log名被该server上的另一个mysql slave占用了. 解决方法:1.在my.cnf中添加 relay_ ...
- CoCos2D-X-2.1.5在Eclipse中导入HelloCpp项目搭建
1.前言 最新正在做一个校园增强现实的应用,虽然不知道cocos2d-x具体到最后能做成什么样子,但还是拿来试试,本文章仅从在Eclipse中采用复制一个新项目副本的方式来导入一个现成的HelloCp ...
- Spark: Best practice for retrieving big data from RDD to local machine
've got big RDD(1gb) in yarn cluster. On local machine, which use this cluster I have only 512 mb. I ...
- Spark的基本说明
1.关于Application 用户程序,一个Application由一个在Driver运行的功能代码和多个Executor上运行的代码组成(工作在不同的节点上). 又分成多个Job,每个Job由多个 ...
- Python2 获取两日期之间的每一天
import datetime def getEveryDay(begin_date,end_date): date_list = [] begin_date = datetime.datetime. ...
- maven-gpg-plugin:1.2:sign (sign-artifacts) on project jdbc-pool: Cannot obtain passphrase in batch mode或者是Plugin execution not covered by lifecycle configuration
本解决方案转自:http://blog.csdn.net/tangximing123/article/details/21179467 执行 Maven install 时报错: Failed to ...
- VBA学习笔记(7)--vba的数组函数
说明(2017.3.25): 1. split(str,"-")和join(arr,",")函数,用法跟其他语言差不多. 2. filter函数,filter( ...
- Head First 设计模式读书笔记(1)-策略模式
一.策略模式的定义 策略模式定义了算法族,分别封装起来,让它们之间可以互换替换,此模式让算法的变化独立使用算法的客户. 二.使用策略模式的一个例子 2.1引出问题 某公司做了一套模拟鸭子的游戏:该游戏 ...