html5图片无限循环播放,原生js实现无限循环轮播图效果
知识要点
1.实现无限循环的原理:
以偏移的距离来判断是否跳回第一张和最后一张
也可以利用循环判断图片的当前索引值
var newLeft=parseInt(list.style.left)+offset;//当前的偏移量+下一次的偏移量=新的偏移量
list.style.left=newLeft+"px";//当前的偏移值=新的偏移值
//以偏移的距离来判断是否跳回第一张和最后一张
if(newLeft>-600){
list.style.left=-3000+"px";
}
if (newLeft
list.style.left=-600+"px";
}
2.当前图片轮播的圆点变色显示:
因为每次点击index+1 所以当前的index-1就是button的索引
//添加on前先清空on
for(var i=0;i
if(buttons[i].className=="on"){
buttons[i].className="";
break;
}
}
buttons[index-1].className="on";
3.实现动画滚动效果:
原理就是把每次的偏移量分为多次完成比如一次600px那就分为10次每次偏移60px
就要用到setTimeout(go,10);//10毫秒再次调用go函数,一直到不满足条件就停
var newLeft=parseInt(list.style.left)+offset;//当前的偏移量+下一次的偏移量=新的偏移量
var time=300;//位移总时间
var interval=10;//位移间隔时间
//动画效果自定义公式: 每次位移的距离=单次偏移距离/位移次数
var speed=offset/(time/interval);
//递归函数 直到不满足条件(跳到辅助图)
//递归就是把600偏移量分为多次完成偏移
function go(){
//speed<0 并且 当前偏移量>下一次偏移量 就是向左偏移 || 反之向右偏移
if ((speed<0 &&parseInt(list.style.left)>newLeft) || (speed>0 &&parseInt(list.style.left)
list.style.left=parseInt(list.style.left)+speed+"px";//每次位移的值
setTimeout(go,interval);//10毫秒再次调用go函数
}else{
animated=false;
list.style.left=newLeft+"px";//当前的偏移值=新的偏移值
if(newLeft>-600){
list.style.left=-3000+"px";
}
if (newLeft
list.style.left=-600+"px";
}
}
}
4.点击圆点按钮执行动画:
原理获取当前的按钮位置再获取要点击的按钮的位置
用(点击的——当前的)*-600=需要跳转的正负距离(向左或向右)
for(var i=0;i
buttons[i].οnclick=function(){
if(this.className=="on"){
return;
}
//要点击的index属性的值 转换为整数
var myIndex=parseInt(this.getAttribute("index"));
//偏移量=-600*(要点击的位置-当前所在的位置),当前的位置就是index循环所得
var os=-600*(myIndex-index);
//切换完成后,把点击的index位置变成当前的index位置
index=myIndex;
showButton();
if(!animated){
animate(os);
}
}
}
5.自动播放:
给外层容器加个onmouseover事件再调用setInterval方法
//给方法定义全局变量是因为停止的时候要使用
function play(){
timer=setInterval(function(){
next.onclick();
},3000);
}
clearInterval(timer)
完整代码
注:图片链接本地替换一下
demo
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:none;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
#container{width: 600px; height: 400px; overflow: hidden; position: relative; }
#list{width: 4200px; height: 400px; position: absolute; z-index: 1;}
#list img{float: left;}
#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;}
<
>
//在页面加载完后立即执行多个函数方案。
function addloadEvent(func){
var oldοnlοad=window.onload;
if(typeof window.onload !="function"){
window.οnlοad=func;
}
else{
window.οnlοad=function(){
if(oldonload){
oldonload();
}
func();
}
}
}
//在页面加载完后立即执行多个函数方案结束。
addloadEvent(lbt);
//轮播图动画切换原理
function lbt(){
var container=document.getElementById("container");
var prev=document.getElementById("prev");
var next=document.getElementById("next");
var list=document.getElementById("list");
var buttons=document.getElementById("buttons").getElementsByTagName("span");
var imgs=list.getElementsByTagName("img");
var index=1;
var animated=false;
var timer;
//当前图片轮播的圆点变色显示,原理:index数值是跟随list滑动次数递增的,第一次index=1,所以第一个button的索引值就是0。
//for循环是添加on样式之前要清空之前的on。
function showButton(){
for(var i=0;i
if(buttons[i].className=="on"){
buttons[i].className="";
break;
}
}
buttons[index-1].className="on";
}
//圆点变色显示 结束。
function animate(offset){
animated=true;
var newLeft=parseInt(list.style.left)+offset;//当前的偏移量+下一次的偏移量=新的偏移量
var time=300;//位移总时间
var interval=10;//位移间隔时间
//动画效果自定义公式: 每次位移的距离=单次偏移距离/位移次数
var speed=offset/(time/interval);
//递归函数 直到不满足条件(跳到辅助图)
//递归就是把600偏移量分为多次完成偏移
function go(){
//speed<0 并且 当前偏移量>下一次偏移量 就是向左偏移 || 反之向右偏移
if ((speed<0 &&parseInt(list.style.left)>newLeft) || (speed>0 &&parseInt(list.style.left)
list.style.left=parseInt(list.style.left)+speed+"px";//每次位移的值
setTimeout(go,interval);//10毫秒再次调用go函数
}else{
animated=false;
list.style.left=newLeft+"px";//当前的偏移值=新的偏移值
if(newLeft>-600){
list.style.left=-3000+"px";
}
if (newLeft
list.style.left=-600+"px";
}
}
}
go();
}
//自动播放3秒执行一次next.onclick
function play(){
timer=setInterval(function(){
next.onclick();
},3000);
}
function stop(){
clearInterval(timer);
}
//执行所有函数
next.οnclick=function(){
if(index==5){
index=1;
}else{
index+=1;
}
showButton();
if(!animated){
animate(-600);
}
}
//执行所有函数
prev.οnclick=function(){
if(index==1){
index=5;
}else{
index-=1;
}
showButton();
if(!animated){
animate(600);
}
}
//点击圆点按钮 偏移
for(var i=0;i
buttons[i].οnclick=function(){
if(this.className=="on"){
return;
}
//要点击的index属性的值 转换为整数
var myIndex=parseInt(this.getAttribute("index"));
//偏移量=-600*(要点击的位置-当前所在的位置),当前的位置就是index循环所得
var os=-600*(myIndex-index);
//切换完成后,把点击的index位置变成当前的index位置
index=myIndex;
showButton();
if(!animated){
animate(os);
}
}
}
container.οnmοuseοver=stop;
container.οnmοuseοut=play;
play();
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
html5图片无限循环播放,原生js实现无限循环轮播图效果相关推荐
- 基于原生JS写的异形轮播图--效果如网易云、QQ音乐播放器中轮播图
css部分 <style>#box{height:500px;width:1000px;position: relative;margin:100px auto;overflow: hid ...
- 原生JS实现韩雪冬轮播图
分享一个用原生JS实现的韩雪冬轮播图,效果如下: 实现代码如下: <!DOCTYPE html> <html><head lang="en">& ...
- 2021年原生JS实现韩雪冬轮播图
<!DOCTYPE html> <html><head lang="en"><meta charset="UTF-8" ...
- html轮播台袋效果,使用html+js+css 实现页面轮播图效果(实例讲解)
html 页面 轮播图效果 < > css页面 carousel.css #main{ width: 655px; height: 361px; position: relative; } ...
- html中多个图片轮播代码怎么写,Html5如何快速在页面中写出多个轮播图效果
我们在做项目的过程中,有时候客户需求要求你在同一个页面中,写几个不同样式的轮播图效果,那么如何快速实现呢?(要知道若果你每个轮播图都要用原生javascript写的话,会很麻烦,代码也不够简洁) 这里 ...
- vue如何使用原生js写动画效果_原生js写一个无缝轮播图插件(支持vue)
轮播图插件(Broadcast.js) 前言:写这个插件的原因 前段时间准备用vue加上网易云的nodejs接口,模拟网易云音乐移动端.因为想自己写一遍所有的代码以及加固自己的flex布局,所以没有使 ...
- html怎么引轮播图插件,原生js写一个无缝轮播图插件(支持vue)
轮播图插件(Broadcast.js) 前言:写这个插件的原因 前段时间准备用vue加上网易云的nodejs接口,模拟网易云音乐移动端.因为想自己写一遍所有的代码以及加固自己的flex布局,所以没有使 ...
- 【原生js】详解轮播图之无缝滚动
前言:轮播图,是网站首页中最常见的一种图片切换特效,作为前端开发者,我相信很多开发者都直接调用了JQuery中的封装好的方法实现图片轮播,省事简单.所以我今天想介绍一下原生js代码实现的图片轮播. 结 ...
- 原生js实现触摸滚动轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
最新文章
- linux下文件夹函数,如何从linux上的文件夹中找到一个函数
- Windows Android SDK下载安装,配置,异常问题解决教程
- 纯干货-多场景下大屏可视化应用(文中含可视化源代码链接)
- 双绞线传输距离_光纤传输有哪些特点 光纤传输原理介绍【图文】
- 文献学习(part8)--A community detection algorithm based on graph compression...
- python中ipo模型有_python ipo模型是指什么?_后端开发
- Google Chrome浏览器可能在您不知情的情况下破坏了您的测试
- 小米4刷CM13系统
- 教你用命令行扩展VHD的大小
- C语言 fread 函数 - C语言零基础入门教程
- 复现monodepth2之KITTI数据集准备
- 【情报分享1234】来自海莲花组织的道歉,然后再给你扔了个恶意文档
- 每天一个小技巧(新建桌面)
- srp——点光源阴影的一些坑总结
- css3图片倾斜3d动画效果
- python 12306查询不到车次_(经典!!!详细解析!!!)python实现12306余票查询
- HTTP超文本传输协议详解
- funnyface表情包制作神奇
- 学习 lt MATLAB gt 心得,lt;lt;MATLAB可视化大学物理学gt;gt;使大学物理更具体,更有趣。 - 物理 - 小木虫 - 学术 科研 互动社区...
- pyqt5 源码 eric 记录