想锻炼一下自己的原生js能力可以从写一个轮播图开始,轮播图的运用想必大家都知道吧,好了废话不多说,开始记笔记了,一些需要注意的点,我都在代码中标注了

首先是构造html:

<div id="container">
<div id="list" style="left:-600px">
<img src="img/4.jpg" alt="4">
<img src="img/1.jpg" alt="1">
<img src="img/2.jpg" alt="2">
<img src="img/3.jpg" alt="3">
<img src="img/4.jpg" alt="4">
<img src="img/1.jpg" alt="1">
</div>
<div id="buttons">
<span index=1 class="on"></span>
<span index=2></span>
<span index=3></span>
<span index=4></span>
</div>
<a id="prev" class="arrow"></a>
<a id="next" class="arrow"></a>
</div>

这里需要讲一下的是图片本来是4个,但是需要把第4个和第一个分别多加入到第一个位置和最后一个位置,(为何要这一步?是为了实现无缝播放,由于移动时,为了让用户感觉到第4个图片播放完之后,是第一个图片,必须将第4个图片放到第一个图片的前面)并且设置list的偏移量为-600px(-600px是由于设置时将图片4放置在list的第一个位置,而要显示的是图片1,此时图片1的位置是-600px)

然后设置css参数

*
{
margin: 0;
padding: 0;
text-decoration: none;
}
body
{
padding: 20px;
}
#container
{ position: relative;
overflow: hidden;
width: 500px; height: 500px; border: 3px solid rgb(247, 250, 203);
}/*容器宽高为图片的宽度和高度*/
#list
{ position: absolute;
z-index: 1;
width: 3000px; /*list的所有图片数和图片的乘积*/
height: 500px;
}
#list img
{
float: left;
width: 500px;/*规定的图片的宽度*/
}
#buttons
{
position: absolute;
z-index: 2;
bottom: 20px;
left: 250px;
width: 100px;
height: 10px;
}
#buttons span
{float: left;
width: 10px;
height: 10px;
margin-right: 5px; cursor: pointer;
border: 1px solid #fff;
border-radius: 50%;
background: rgb(141, 139, 139);
}
#buttons .on
{
background:pink;
}
.arrow
{font-size: 36px;
font-weight: bold;
line-height: 39px;
position: absolute;
z-index: 2;
top: 180px;
display: none;
width: 40px;
height: 40px;
cursor: pointer;
text-align: center;
color:pink;
background-color: rgb(229, 247, 194);
}
.arrow:hover
{
background-color:rgb(194, 212, 156) ;
}
#container:hover .arrow
{
display: block;
}
#prev
{
left: 20px;
}
#next
{
right: 20px;
}

最后是js逻辑:

 var  prev=document.getElementById("prev");
var next=document.getElementById("next");
var list=document.getElementById("list");
var buttons=document.getElementById("buttons").getElementsByTagName("span");
var index=1;
var timer;
var animated=false;
var container=document.getElementById("container"); function shownButton(){
for(var i=0;i<buttons.length;i++){
if(buttons[i].className=="on"){
buttons[i].className="";
break;
}
}
buttons[index-1].className="on";
}
function animate(offset){
var time=100;//根据图片宽度来,最好和inteval相除为整数,不然后面移动会出问题,
var inteval=10;
var speed=offset/(time/inteval);
animated=true;
var newLeft=parseInt(list.style.left)+offset;
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,inteval);//递归函数
}
else{
animated=false;
if(newLeft>-500){//当大于第一个图的位移量切换到第4张图
list.style.left=-2000+"px";
};
if(newLeft<-2000){//当小于最后一个图的位移量切换到第一张图
list.style.left=-500+"px";
}
}
}
go();
}
prev.onclick=function(){
if(!animated){
if(index==1){
index=4;//根据自己代码的index值进行修改
}
else{
index-=1;
}
shownButton();
animate(500);//传入图片宽度
}
}
next.onclick=function(){
if(!animated){
if(index==4){//根据自己代码的index值进行修改
index=1;
}
else{
index+=1;
}
shownButton();
animate(-500);//第一章图片宽度
}
}
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=-500*(myIndex-index);//偏移量根据实际情况修改
if(!animated){
animate(offset);
}
index=myIndex;
shownButton();
}
}
function play(){
timer=setInterval(function(){
next.onclick();
},2000); }
function stop(){
clearInterval(timer);
}
play();
container.onmouseover=stop;
container.onmouseout=play;

完整的代码可以去我的github下载,欢迎各位点星星和fork

https://github.com/narrow-gate/lunbo

原生js实现一个简单的轮播图的更多相关文章

  1. 原生js一行代码实现简易轮播图

    这是一个简易的js无限循环轮播图,只用了一行js代码就实现了无限循环,记录一下三目运算符的伟大! <!DOCTYPE html><html lang="en"&g ...

  2. JQuery手写一个简单的轮播图

    做出来的样式: 没有切图,就随便找了一些图片来实现效果,那几个小星星萌不萌. 这个轮播图最主要的部分是animate(),可以先熟悉下这个方法. 代码我放到了github上,链接:https://gi ...

  3. js 实现淘宝无缝轮播图效果,可更改配置参数 带完整版解析代码[slider.js]

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         本篇文章为您分析一下原生JS写淘宝无缝轮播图效果 需求分析: ...

  4. JS封装动画框架,网易轮播图,旋转轮播图

    JS封装动画框架,网易轮播图,旋转轮播图 1. JS封装运动框架 // 多个属性运动框架 添加回调函数 function animate(obj,json,fn) { clearInterval(ob ...

  5. 原生js用div实现简单的轮播图

    文章地址 https://www.cnblogs.com/sandraryan/ 原生js实现轮播图. 打开页面图片自动轮播,点击prev next按钮切换到上/下一张图片,点击1-5切换到对应图片. ...

  6. JS框架_(Bootstrap.js)实现简单的轮播图

    Bootstrap框架中 轮播(Carousel)插件是一种灵活的响应式的向站点添加滑块的方式 轮播图效果: <!DOCTYPE html> <html> <head&g ...

  7. js+css制作简单的轮播图带有定时功能

    用纯css和JavaScript代码制作带有定时轮播功能的轮播图 <!DOCTYPE html> <html> <head> <meta charset=&q ...

  8. jQuery之制作简单的轮播图效果

    [源代码] 链接:https://pan.baidu.com/s/1XpZ66D9fmSwWX3pCnGBqjA 密码:w104 [整体构思] 这个轮播图使用的是jQuery,所以Js的整体代量比较少 ...

  9. 学习笔记: js插件 —— SuperSlide 2 (轮播图插件,PC用)

    SuperSlide 2  轮播图插件,较老.但还好用. 适用于PC,是绑定到jquery上的方法: $.slide(); 如果在实际中找不到.slide方法,请检查jquery等.js文件的引入次序 ...

随机推荐

  1. Linux的JDK配置

    1.下载jdk-7u1-linux-i586.rpm2.cd 到 jdk-7u1-linux-i586.rpm 所在的目录3.su 获得 root 权限4.执行安装命令: rpm -ivh jdk-7 ...

  2. 关于时间:UTC/GMT/xST/ xDT

    UTC ,Coordinated Universal Time GMT ,Greenwich Mean Time GMT和UTC 一般来说是意义几近相同的, 不过 UTC Coordinated Un ...

  3. es 高级

    事务处理 _settings _refresh _flush 慢查询 存储模块 mmap local simplefs 缓存 IO 调节 热点线程 suggesters _suggest 端点 插件 ...

  4. 1. 怎么设置可以使得虚拟机里面既可以访问主机也可以访问局域网而且是静态ip

    方法1: Bridged方式(桥接): (1). 虚拟机网络适配器设置为桥接 (2). 主机设置静态ip (3). 虚拟机也设置静态ip且和宿主机在同一网段 Bridged方式: 在图1中Networ ...

  5. 1.深入分析_NIO性能分析

    首先还是看一个用3中方式copy文件的测试Demo 分别是:普通Stream文件copy,BuffferedStream进行Copy 和Channel(nio)进行文件Copy的代码和性能测试报告: ...

  6. charles抓包的安装,使用说明以及常见问题解决(windows)

    charles抓包的安装,使用说明以及常见问题解决(windows) https://blog.csdn.net/zhangxiang_1102/article/details/77855548

  7. beego api 服务允许跨域访问,解决前端访问报Access-Control-Allow-Origin问题

    背景: golang做了个简单服务,前端get请求拿数据,报错:No 'Access-Control-Allow-Origin' header is present on the requested ...

  8. SVM标记学习

    # -*- coding: utf-8 -*- """ Created on Mon Oct 1 09:32:37 2018 @author: ""& ...

  9. Spring boot 配置文件 加载顺序

    springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件 –file:./config/ – ...

  10. MySQL更新优化(转)

    通常情况下,当访问某张表的时候,读取者首先必须获取该表的锁,如果有写入操作到达,那么写入者一直等待读取者完成操作(查询开始之后就不能中断,因此允许读取者完成操作).当读取者完成对表的操作的时候,锁就会 ...