使用方法:
可能很多人对轮播图感兴趣,下面奉上本人的 原生 js 轮播代码
复制 js 到页面的最底部,样式在 css 里改,js 基本不用动,这个是用 原生写的 轮播图,样式可以写自己喜欢的样式,什么都不用改,只改变样式就行,页面结构的id 要与js的相对应li随便加。li 随便加的意思就是说你可以加无数个图片。每个li 里装一个图片,或者是其他什么元素,

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{padding:0;margin:0;}
#parent{
width:50%;
height:200px;
border:1px solid red;
margin:0 auto;
} #imgBox{
width:500px;
height:200px;
position:relative;/*必须有===*/
overflow:hidden;/*必须有===*/
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
ul{
/*动画就是改变ul的left值的效果,所以一定要有定位*/
position:absolute;/*必须有*/
left:0;/*必须有*/ }
li{list-style: none;/*必须有*/
height:200px;/*必须有*/
width:500px;/*必须有*/
float:left;/*必须有*/
}
.span{
display:inline-block;
width:20px;
height:20px;
line-height:20px;
text-align: center;
border-radius:50%;
color:white;
}
</style>
</head>
<body>
<div id="parent">
<div id="imgBox">
<ul id='ul'>
<li style='background:red;'>第一个</li>
<li style='background:yellow;'>第二个</li>
<li style='background:pink;'>第三个</li>
<li style='background:green;'>第四个</li>
<li style='background:blue;'>第五个</li>
<li style='background:blue;'>第6个</li>
<li style='background:green;'>第7个</li>
<li style='background:blue;'>第8个</li>
<li style='background:green;'>第9个</li>
<li style='background:blue;'>第10个</li>
</ul>
</div>
<a href="#" id="prev">向左</a>
<a href="#" id="next">向右</a>
<!-- 放小圆点的盒子-->
<div id="arcBox">
<!-- 小圆点的位置 -->
</div>
</div>
<script>
var chefElement = {
bgColor1:'#cccccc',//小圆点的背景颜色
bgColor2:'red',//获得焦点的小圆点的背景颜色
animationSpeed:1, //每多少毫秒移动一次
minSpeed:5,//每次移动的距离
stopTime:5000, //动画停留的时间,毫秒为单位 包含移动所花费的时间 //获取页面元素
prev:document.getElementById('prev'),
next:document.getElementById('next'),
parent:document.getElementById('parent'),
ul:document.getElementById('ul'),
li:document.getElementById('ul').getElementsByTagName('li'),
arc:document.getElementById("arcBox"),
liWidth:document.getElementById('ul').getElementsByTagName('li')[0].offsetWidth,
type:true,
nextTimer:null,
prevTimer:null,
parent_n:null
}; //初始化小圆点/指定放图片的盒子 ul 的宽度
var elemSpan = (function(){
chefElement.ul.style.width = chefElement.liWidth*chefElement.li.length+'px';
for(var i = 0;i<chefElement.li.length;i++){
chefElement.li[i].index = i;
var span = document.createElement('span');
span.className = 'span';
span.index = i;
span.style.background = chefElement.bgColor1;
span.innerHTML = i+1;
chefElement.arc.appendChild(span);
}
var objSpan = chefElement.arc.getElementsByTagName('span'); //创建完以后第一个小圆点显示指定的颜色
objSpan[0].style.background = chefElement.bgColor2;
return objSpan;
})(); //给每个小圆点添加事件
chefElement.arc.onmouseover = function(ev){
var ev = ev || window.event;
var target = ev.target || ev.srcElement;
if(target.nodeName.toLowerCase() == "span"){
if(chefElement.type){
showImg(target.index);
changeBackgroundColor(target)
chefElement.type = true;
}
}
} //改变小圆点的背景颜色
function changeBackgroundColor(obj){
for(var i = 0;i<elemSpan.length;i++){
elemSpan[i].style.background = chefElement.bgColor1;
}
elemSpan[obj.index].style.background = chefElement.bgColor2;
} //根据参数显示对应的图片。
function showImg(inde){
var this_li = chefElement.li[0].index; //把第一个元素放到最后面。
if(inde>this_li){
var x = inde-this_li;
for(var y = 0;y<x;y++){
chefElement.ul.appendChild(chefElement.li[0]);
}
} //把最后一个元素放到第一的位置
if(inde<this_li){
var x_x = this_li-inde;
for(var g = 0;g<x_x;g++){
chefElement.ul.insertBefore(chefElement.li[chefElement.li.length-1],chefElement.li[0]);
}
}
} chefElement.prev.onclick = function(){
if(chefElement.type){
clearInterval(chefElement.prevTimer);
chefElement.ul.insertBefore(chefElement.li[chefElement.li.length-1],chefElement.li[0]);
chefElement.liWidth = chefElement.li[0].offsetWidth;
chefElement.ul.style.left = '-'+chefElement.liWidth+'px';
chefElement.prevTimer = setInterval(pre,chefElement.animationSpeed);
chefElement.type = false;
changeBackgroundColor(chefElement.li[0]);
}
};
next.onclick = function(){
if(chefElement.type){
chefElement.liWidth = 0;
clearInterval(chefElement.nextTimer);
chefElement.nextTimer = setInterval(nex,chefElement.animationSpeed);
chefElement.type = false;
changeBackgroundColor(chefElement.li[1]);
}
}; //next动画函数
function nex(){
chefElement.ul.style.left = '-'+chefElement.liWidth+ 'px';
chefElement.liWidth += chefElement.minSpeed ;
if(chefElement.liWidth >= chefElement.li[0].offsetWidth){
clearInterval(chefElement.nextTimer);
chefElement.ul.appendChild(chefElement.li[0]);
chefElement.ul.style.left = 0;
chefElement.type = true;
}
} //prev动画函数
function pre(){
chefElement.ul.style.left = '-'+chefElement.liWidth+'px';
chefElement.liWidth -= chefElement.minSpeed ;
if(chefElement.liWidth <= -1){
chefElement.ul.style.left = 0;
clearInterval(chefElement.prevTimer);
chefElement.type = true;
}
} chefElement.parent.onmouseover = function(){
clearInterval(chefElement.parent_n);
};
chefElement.parent.onmouseout = function(){
chefElement.parent_n = setInterval(next.onclick,chefElement.stopTime);
}; //动画播放
chefElement.parent_n = setInterval(next.onclick,chefElement.stopTime);
</script>
</body>
</html>

原生 js 左右切换轮播图的更多相关文章

  1. jQuery与原生js实现banner轮播图

    jQuery与原生js实现banner轮播图: (jq需自己加载)(图片需自己加载) <!DOCTYPE html> <html> <head> <meta ...

  2. 原生JS实现简易轮播图

    原生JS实现简易轮播图(渐变?) 最近做网页总是会用到轮播图,我就把之前写的轮播图单独拿出来吧,如果有...如果真的有人也需要也可以复制去用用啊..哈~.. window.onload = funct ...

  3. 自己用原生JS写的轮播图,支持移动端触屏滑动,面向对象思路。分页器圆点支持click和mouseover。

    自己用原生javascript写的轮播图,面向对象思路,支持移动端手指触屏滑动.分页器圆点可以选择click点击或mouseover鼠标移入时触发.图片滚动用的setInterval,感觉setInt ...

  4. 自己用原生JS写的轮播图,支持移动端触摸滑动,分页器圆点可以支持mouseover鼠标移入和click点击,高手看了勿喷哈

    自己用原生JavaScript写的轮播图,分页器圆点按钮可支持click点击,也可支持mouseover鼠标悬浮触发,同时支持移动端触摸滑动,有兴趣的友友可以试试哈,菜鸟一枚,高手看了勿喷,请多多指正 ...

  5. photoSlider-html5原生js移动开发轮播图-相册滑动插件

    简单的移动端图片滑动切换浏览插件 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css" hre ...

  6. 原生JS实现旋转木马轮播图特效

    大概是这个样子: 首先来简单布局一下(emm...随便弄一下吧,反正主要是用js来整的) <!DOCTYPE html> <html lang="en"> ...

  7. 原生js写简单轮播图方式1-从左向右滑动

    轮播图就是让图片每隔几秒自动滑动,达到图片轮流播放的效果.轮播图从效果来说有滑动式的也有渐入式的,滑动式的轮播图就是图片从左向右滑入的效果,渐入式的轮播图就是图片根据透明度渐渐显示的效果,这里说的是实 ...

  8. 【原生JS】层叠轮播图

    又是轮播?没错,换个样式玩轮播. HTML: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  9. 原生js实现的轮播图,易用+可多用

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

随机推荐

  1. UIColor+Hex

    #import <UIKit/UIKit.h> @interface UIColor (Hex) + (UIColor *)colorWithHex:(long)hexColor;+ (U ...

  2. .net学习笔记---lambda表达式(自执行方法)

    http://www.cnblogs.com/jesse2013/p/happylambda.html#b034 lambda表达式 http://www.cnblogs.com/OceanEyes/ ...

  3. C/C++学习笔记----指针的理解

    指针是C/C++编程中的重要概念之一,也是最容易产生困惑并导致程序出错的问题之一.利用指针编程可以表示各种数据结构,通过指针可使用主调函数和被调函数之间共享变量或数据结构,便于实现双向数据通讯:指针能 ...

  4. WindowManagerPolicy的后缀 解释

    转自:http://blog.csdn.net/hunanwy/article/details/8563090 Ti,called from the input thread. Input threa ...

  5. 重温WCF之WCF传输安全(十三)(1)前期准备之证书制作(转)

    转载地址:http://www.cnblogs.com/lxblog/archive/2012/09/12/2682372.html 一.WCF中的安全方式 说到安全就会涉及到认证,消息一致性和机密性 ...

  6. ASP.NET Web Api 服务器端变了,客户端该如何修改请求(转载)

    转载地址:http://www.cnblogs.com/fzrain/p/3558765.html 前言 一旦我们将API发布之后,消费者就会开始使用并和其他的一些数据混在一起.然而,当新的需求出现时 ...

  7. Object.create 函数 (JavaScript)

    创建一个具有指定原型且可选择性地包含指定属性的对象. 语法 Object.create(prototype, descriptors) 参数 prototype 必需.  要用作原型的对象.  可以为 ...

  8. golang heap container balance request

    package mainimport ( "container/heap" "fmt" "log" "math/rand" ...

  9. 数据结构之图 Part2 - 3

    十字链表 简单的说就是邻接表和逆邻接表的合体,解决了原邻接表或者逆邻接表出度和入度的计算无法兼得的问题. using System; using System.Collections.Generic; ...

  10. C 和 C++ 混合代码 cmath编译出错

    最近在网上下载了 Triangle 库,准备在程序中调用来三角化生成网格,但出现了很多错误,如下: 1>  triangle.c1>d:\program files\visualstudi ...