首先引入js运动框架

 function getStyle(obj,name){
if(obj.currentStyle){
return obj.currentStyle[name];
} else{
return getComputedStyle(obj,false)[name];
}
} function startMove(obj, json, fnEnd) {
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var bStop = true;
for (var attr in json) {
var cur = 0;
if (attr == "opacity") {
cur = Math.round(parseFloat(getStyle(obj, attr)) * 100);
} else {
cur = parseInt(getStyle(obj, attr))
}
var speed = (json[attr] - cur) / 10;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if (cur !== json[attr]) {
bStop = false;
};
if (attr == "opacity") {
obj.style.opacity = (speed + cur) / 100;
obj.style.filter = 'alpha(opacity:' + (speed + cur) + ')';
} else {
obj.style[attr] = cur + speed + 'px';
}
}
if (bStop) {
clearInterval(obj.timer);
if (fnEnd) fnEnd();
}
}, 30)
}

然后写轮播小案例

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>淘宝轮播</title>
<style>
ul,
li {
list-style: none;
margin: 0;
padding: 0;
} #wrap {
width: 400px;
height: 225px;
margin: 0 auto;
position: relative;
overflow: hidden;
} li {
float: left;
} #tips li {
margin: 5px;
border: 1px solid #f60;
width: 20px;
height: 20px;
line-height: 20px;
text-align: center;
color: white;
cursor: pointer;
} .active {
background: #f60;
} img {
vertical-align: top;
width: 400px;
} #content {
width: 2400px;
position: absolute;
left: -1200px;
} #content li {
float: left;
} #tips {
position: absolute;
right: 20px;
bottom: 5px;
}
</style>
</head> <body>
<div id="wrap">
<ul id="content">
<li><img src="img3/1.jpg" alt=""></li>
<li><img src="img3/2.jpg" alt=""></li>
<li><img src="img3/3.jpg" alt=""></li>
<li><img src="img3/4.jpg" alt=""></li>
<li><img src="img3/5.jpg" alt=""></li>
<li><img src="img3/6.jpg" alt=""></li>
</ul>
<ul id="tips">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
<script src="move.js"></script>
<script>
var wrap = document.getElementById('wrap');
var content = document.getElementById('content');
var tips = document.getElementById('tips');
var aLi = tips.getElementsByTagName('li');
var now = 0;
//var
for (var i = 0; i < aLi.length; i++) {
aLi[0].className = 'active'; //把初始状态定义好
content.style.left = 0 +'px';
aLi[i].index = i; //自定义属性
aLi[i].onclick = function() {
now = this.index;
play();
}
} function play() {
for (var j = 0; j < aLi.length; j++) {
aLi[j].className = '';
}
aLi[now].className = 'active'; //this.index = now; //反过来写就不对了大兄弟
//content.style.left = -400 * this.index + 'px';
startMove(content, {
left: -400 * now, //你还真别说,json格式就是这么写的
});
} function autoPlay() {
now++;
if (now == aLi.length) {
now = 0;
}
play();
} var timer = setInterval(autoPlay, 2000);
wrap.onmouseover = function(){ //这里如果把事件绑定到ul上的话,那么鼠标移入,下面对饮的li会不起作用,
clearInterval(timer); //因为li的层级比较高,所以应该把事件绑定到大的div上
}
wrap.onmouseout = function(){
timer = setInterval(autoPlay,2000);
//setInterval(autoPlay,2000); 不能这么写,凡是开的定时器,必须得赋值,要不然总会多开一个定时器,导致速度加快
}
</script>
</body> </html>

最终效果如下图,可实现鼠标翻页,鼠标悬停后停止轮播,

原生Javascript实现图片轮播效果的更多相关文章

  1. 原生js实现图片轮播效果

    思路:设置父容器(一定宽度,一定高度,相对定位,子容器超出部分进行隐藏),子容器图片并排(浮动,绝对定位,每次点击进行相应的左或右偏移量) 1.html: <!DOCTYPE html> ...

  2. 超实用的JavaScript代码段 Item3 --图片轮播效果

    图片轮播效果 图片尺寸 统一设置成:490*170px; 一.页面加载.获取整个容器.所有放数字索引的li及放图片列表的ul.定义放定时器的变量.存放当前索引的变量index 二.添加定时器,每隔2秒 ...

  3. jQuery个性化图片轮播效果

    jQuery个性化图片轮播效果 购物产品展示:图片轮播器<效果如下所示> 思路说明: 每隔一段时间,实现图片的自动切换及选项卡选中效果,鼠标划入图片动画停止,划出或离开动画开始 两个区域: ...

  4. JQ 实现轮播图(3D旋转图片轮播效果)

    轮播图效果如下: 代码: <!DOCTYPE html> <html xmlns="/www.w3.org/1999/xhtml"> <head> ...

  5. 原生js实现图片轮播思路分析

    一.复习原生js实现图片轮播 1.要点 自动轮播 点击小圆圈按钮,显示相应图片 点击左右箭头,实现向前向后轮播图片 2.实现思路 <div id="container"> ...

  6. js图片轮播效果实现代码

    首先给大家看一看js图片轮播效果,如下图 具体思路: 一.页面加载.获取整个容器.所有放数字索引的li及放图片列表的ul.定义放定时器的变量.存放当前索引的变量index 二.添加定时器,每隔2秒钟i ...

  7. CSS3图片轮播效果

    原文:CSS3图片轮播效果 在网页中用到图片轮播效果,单纯的隐藏.显示,那再简单不过了,要有动画效果,如果是自己写的话(不用jquery等),可能要费点时间.css3的出现,让动画变得不再是问题,而且 ...

  8. js实现淘宝首页图片轮播效果

    原文:http://ce.sysu.edu.cn/hope2008/Education/ShowArticle.asp?ArticleID=10585 <!DOCTYPE html> &l ...

  9. ios图片轮播效果

    代码地址如下:http://www.demodashi.com/demo/11959.html ImageCarousel 简单封装的图片轮播器 内存过大由于我加载的图片分辨率较高(4k) 文件目录 ...

随机推荐

  1. IL(Intermediate Language)

    释义: IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的程序代码并不是CPU ...

  2. WCF websocket

    WebSocket, like as TCP, is a bi-directional, full-duplex communication channel over a single TCP con ...

  3. hadoop配置文件的加载机制

    hadoop通过Configuration类来保存配置信息 1.通过Configuration.addResource()来加载配置文件 2.通过Configuration.get***()来获取配置 ...

  4. CSS3 基础知识

    CSS3 基础知识1.边框    1.1 圆角  border-radius:5px 0 0 5px;    1.2 阴影  box-shadow:2px 3px 4px 5px rgba(0,0,0 ...

  5. django-filter 使用Filter来筛选你的数据

    django-filter Django-filter is a generic, reusable application to alleviate writing some of the more ...

  6. 简单说pyglet.event

    emitter,就是pyglet.event.EventDispatcher的子类, 负责发出事件的消息,并且规定了响应消息的函数名. class Consumer(pyglet.event.Even ...

  7. 【iOS开发】单例模式设计(ARC & MRC)

    适用于ARC & MRC // 帮助实现单例设计模式 // .h文件的实现 #define SingletonH(methodName) + (instancetype)shared##met ...

  8. http、TCP/IP协议与socket之间的区别

    http.TCP/IP协议与socket之间的区别     网络由下往上分为:  www.2cto.com   物理层--                       数据链路层-- 网络层--   ...

  9. [POJ] 3461 Oulipo [KMP算法]

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23667   Accepted: 9492 Descripti ...

  10. LeetCode_Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...