原生js焦点轮播图
原生js焦点轮播图主要注意这几点:
1、前后按钮实现切换,同时注意辅助图
2、中间的button随着前后按钮对应切换,同时按button也能跳转到相应的index
3、间隔调用与无限轮播。
4、注意在动画时要停止按钮,或者说上一个动画完毕下一个动画才能执行
5、另外在切换图片的时候,底部的Button动画效果,是从底部开始往上升的,要用到transform:scale()和transform-origin:0 100%两个转换属性,代码如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewpoint" content="width=device-width,initial-scale=1,user-scalable="no">
<title>20170101</title>
<style type="text/css">
a{text-decoration:none;color:#3DBBF5;}
.wrapper{width:750px;height:350px;background:#001032;margin:20px auto;text-align:center;box-shadow:0 0 12px 2px hsla(0,20%,30%,0.5);padding:10px 15px;position:relative;}
.effect{position:relative;cursor:pointer;}
.effect:hover{color:#02a0e9;}
.effect:before{width:100%;display:inline-block !important;position:absolute;height:1px;background:#02a0e9;transition:all 0.4s ease-in-out;-webkit-transition:all 0.4s ease-in-out;-moz-transition:all 0.4s ease-in-out;transform:scale(0,1);content:'';bottom:-5px;left:0;}
.effect:hover:before{transform:scale(1);-webkit-transform:scale(1);}
#lunBo{margin-top:20px;overflow:hidden;height:300px;width:750px;position:relative;}
#list{position:absolute;z-index:22;height:300px;width:5250px;}
#list img{float:left;}
#buttons { position: absolute; height: 20px; width: 150px; z-index: 99; bottom: 20px; left: 40%;}
span { cursor: pointer; float: left; width: 10px; height: 5px; background: #333; margin-right: 10px;}
.on { background: yellow;transition:all 0.4s ease-in-out;-webkit-transition:all 0.4s ease-in-out;-moz-transition:all 0.4s ease-in-out;transform:scale(1,4);-ms-transform:scale(1,4);-moz-transform:scale(1,4);-webkit-transform:scale(1,4);transform-origin:0% 0%;-webkit-transform-origin:0% 100%;-moz-transform-origin:0% 100%;}
.arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 100px; line-height:100px;position: absolute; z-index: 92; top: 30%; color: #fff;}
.arrow:hover { background-color: RGBA(0,0,0,.7);}
#lunBo:hover .arrow { display: block;}
#prev { left: 0px;}
#next { right: 0px;}
</style>
</head>
<body>
<div class="wrapper">
<a class="effect" href="#">2016完了,2017来了</a>
<div id="lunBo">
<div id="list" style="left:-750px;">
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856saeagzgsnwal15n5.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856z48mfrrr8u064rf6.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856tohfccjufvvaa3oy.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856m1bhxxx1d8jfnblb.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856e95yze236lvq7y2a.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856saeagzgsnwal15n5.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856z48mfrrr8u064rf6.jpg" alt=""/>
</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:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>
</div>
<script>
window.onload = function(){
var lunBo = document.getElementById('lunBo');
var list = document.getElementById('list');
var buttons = document.getElementById('buttons').getElementsByTagName('span');
//console.log(buttons);
var prev = document.getElementById('prev');
var next = document.getElementById('next');
var index = 1;
var animated = false;
var interval = 3000;
var timer;
//显示按钮的索引
function showButton(){
for(var i = 0 ; i < buttons.length ; i++){
if( buttons[i].className == 'on' ){
buttons[i].className = '';
break;
};
};
buttons[index - 1].className='on';
};
function play(){
timer = setTimeout(function () {
next.onclick();
play();
}, interval);
};
function stop(){
clearTimeout(timer);
};
//向前按钮
next.onclick = function () {
if (animated) {
return;
}
if (index == 5) {
index = 1;
}
else {
index += 1;
}
animate(-750);
showButton();
};
prev.onclick = function () {
if (animated) {
return;
}
if (index == 1) {
index = 5;
}
else {
index -= 1;
}
animate(750);
showButton();
};
//parseInt()转换为纯数值
function animate(offset){
animated = true;
var newLeft = parseInt(list.style.left) + offset; //目标值
var time = 300; //位移总时间为300
var interval = 10; //
var speed = offset/(time/interval); //每次位移量
function go(){
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,interval);
}else{
animated = false;
list.style.left = newLeft+ 'px'; //现在的位移
if( newLeft > -750){ //假的辅助图
list.style.left = -3750 + 'px';
}
if( newLeft < -3750){
list.style.left = -750 + 'px';
}
}
};
go();
};
//小按钮
for(var i=0;i < buttons.length;i++){
buttons[i].onclick = function(){
if(this.className == 'on'){
return;
};
var myIndex = parseInt(this.getAttribute('index'));
var offset = -750 * (myIndex - index);
animate(offset);
index = myIndex;
showButton();
}
}
lunBo.onmouseout = play;
lunBo.onmouseover = stop;
play();
}
</script>
</body>
</html>
有大量web前端开发工具及学习资料,可以搜群【 web前端学习部落22群 】进行下载,遇到学习问题也可以问群内专家以及课程老师哟
原生js焦点轮播图的更多相关文章
- 封装一个简单的原生js焦点轮播图插件
轮播图实现的效果为,鼠标移入左右箭头会出现,可以点击切换图片,下面的小圆点会跟随,可以循环播放(为了方便理解,没有补2张图做无缝轮播).本篇文章的主要目的是分享封装插件的思路. 轮播图我一开始是写成非 ...
- 原生js焦点轮播图的实现
继续学习打卡,武汉加油,逆战必胜!今日咱们主要探讨一下原生js写轮播图的问题, 简单解析一下思路: 1,首先写好css样式问题 2,考虑全局变量:自动播放的定时器,以及记录图片位置的角标Index 2 ...
- 原生js实现轮播图
原生js实现轮播图 很多网站上都有轮播图,但找到一个系统讲解的却很难,因此这里做一个简单的介绍,希望大家都能有所收获,如果有哪些不正确的地方,希望大家可以指出. 原理: 将一些图片在一行中平铺,然后计 ...
- 原生JS实现轮播图的效果
原生JS实现轮播图的效果: 只要缕清了全局变量index的作用,这个轮播图也就比较容易实现了:另外,为了实现轮这个效果,有几处clearInterval()必须写上.废话不多说,直接上代码,修复了几个 ...
- 使用原生js将轮播图组件化
代码地址如下:http://www.demodashi.com/demo/11316.html 这是一个轮播图组件,这里是代码地址,需要传入容器的id和图片地址,支持Internet Explor ...
- 用原生js封装轮播图
原生js封装轮播图 对于初学js的同学来说,轮播图还是一个难点,尤其是原生js封装轮播图代码,下面是我之前做的一个轮播图项目中封装好的一些代码,有需要的同学可以看一下,有什么不懂的可以看注释,注释看不 ...
- js焦点轮播图
汇集网上焦点轮播图的实现方式,自己试了下,不过鼠标悬浮停止动画和鼠标离开动画播放好像没生效,不太明白,最后两行代码中,为什么可以直接写stop和play.不用加括号调用函数么?求懂的大神指点! 所用知 ...
- 原生js封装轮播图
个人实际开发中用到的效果问题总结出来便于自己以后开发查看调用,如果也适用其他人请随意拿走勿喷就行! 原生js对于思路要求比较高,在js代码我都写有备注,足够理解并使用,即使是小白或者刚入行的程序员也比 ...
- 原生JS设计轮播图
一.效果预览: 由于只能上传2M以下的图片,这里只截取了自动切换的效果: 二.编写语言 HTML.CSS.原生JS 三.编写思路 (一)HTML部分 1..slide意为滑槽,里面存放所有图片: 2. ...
随机推荐
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【bzoj1004】 HNOI2008—Cards
http://www.lydsy.com/JudgeOnline/problem.php?id=1004 (题目链接) 题意 n张卡片,染成3种颜色,每种颜色只能染固定张数.给出一些洗牌方案,问染色方 ...
- Python学习总结 03 Plotly 学习总结
一 Plotly 简介 Plotly是另一个免费进行数据分析和绘制图表的APP,建立在d3.js上. Plotly图可下载为SVG,EPS或PNG格式,并简单地导入到Illustrator或者Phot ...
- 火车头dede采集接口,图片加水印,远程图片本地化,远程无后缀的无图片本地化
<?php /* [LocoySpider] (C)2005-2010 Lewell Inc. 火车采集器 DedeCMS 5.7 UTF8 文章发布接口 Update content: 图片加 ...
- Linux下查看chm文件
第一种方法.安装xchm,安装命令sudo apt-get install xchm,打开xchm,在终端下输入xchm. 第二种方法.安装kchmviewer,安装命令sudo apt-get in ...
- 特性 Attribute
特性就是一个类,必须是Attribute的子类 一般以Attribute结尾,然后在使用的时候,可以去掉这个结尾 可以在特性中声明字段.属性.方法.构造函数.委托.事件... [AttributeUs ...
- 使用vlc进行二次开发做自己的播放器
可参考: 使用vlc播放器做rtsp服务器 使用vlc播放器播放rtsp视频 web网页中使用vlc插件播放相机rtsp流视频 使用 https://github.com/ZeBobo5/Vlc.Do ...
- vim vi Ubuntu
在vi编辑模式下按退格键不能删除内容,按方向键不能上下左右移动?如果是则:1. 在vi里非编辑模式下按冒号进入到末行命令模式,然后输入set nocompatible,回车,然后在进入vi编辑模式,看 ...
- 将博客搬至CSDN
将博客搬至CSDN将博客搬至CSDN将博客搬至CSDN将博客搬至CSDN
- 20155217-杨笛-c与java
命是弱者的借口,运是强者的谦词 你有什么技能比大多数人(超过90%以上)更好? 针对这个技能的获取你有什么成功的经验? 与老师博客中的学习经验有什么共同之处? 其实我经常会去想自己有什么拿得出手的优点 ...