首先引入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. JavaScript获取元素样式

    原生的JavaScript获取写在标签内部的样式很简单: <div class="test" id="test" style="width:10 ...

  2. Sqlserver统计语句

    --查看被缓存的查询计划 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED st.text AS [SQL] , cp.cacheobjtype , c ...

  3. html5响应式布局

    1.media控制布局 <link type="text/css" rel="stylesheet" href="css04.css" ...

  4. 关于百度鹰眼中 xcode 7 编译报错问题

    请把 这个地方改为 YES 否则demo 不能运行

  5. Struts2 技术全总结 (正在更新)

    背景:Struts1是一个高度成熟的框架,运行效率高,但其致命缺陷在于与JSP/Servlet的耦合非常紧密,因而导致了一些严重问题.其次,Struts1与Servlet API的严重耦合,使应用难以 ...

  6. VC:CString用法整理(转载)

    1.CString::IsEmpty BOOL IsEmpty( ) const; 返回值:如果CString 对象的长度为0,则返回非零值:否则返回0. 说明:此成员函数用来测试一个CString ...

  7. 三个重要的游标sp_cursoropen

    請問這三個存諸過程的作用是什么﹖ sp_cursoropen, sp_cursorfetch, sp_cursorclose API 服务器游标实现  SQL Server OLE DB 提供程序. ...

  8. java.lang.NoSuchMethodError: main Exception in thread "main"

    java.lang.NoSuchMethodError: main Exception in thread "main" 一般是主函数出问题 检查核对一下 public stati ...

  9. Oracle left查询案例

    )) summoney from( select t2.ano,d.dmoney from ( select t1.*,c.cno from( select a.ano,b.bno from t_a ...

  10. Log4net 配置详解

    首先需在config文件的<configSections>节点中增加子节点配置. <configSections> <section name="log4net ...