原生Javascript实现图片轮播效果
首先引入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实现图片轮播效果的更多相关文章
- 原生js实现图片轮播效果
思路:设置父容器(一定宽度,一定高度,相对定位,子容器超出部分进行隐藏),子容器图片并排(浮动,绝对定位,每次点击进行相应的左或右偏移量) 1.html: <!DOCTYPE html> ...
- 超实用的JavaScript代码段 Item3 --图片轮播效果
图片轮播效果 图片尺寸 统一设置成:490*170px; 一.页面加载.获取整个容器.所有放数字索引的li及放图片列表的ul.定义放定时器的变量.存放当前索引的变量index 二.添加定时器,每隔2秒 ...
- jQuery个性化图片轮播效果
jQuery个性化图片轮播效果 购物产品展示:图片轮播器<效果如下所示> 思路说明: 每隔一段时间,实现图片的自动切换及选项卡选中效果,鼠标划入图片动画停止,划出或离开动画开始 两个区域: ...
- JQ 实现轮播图(3D旋转图片轮播效果)
轮播图效果如下: 代码: <!DOCTYPE html> <html xmlns="/www.w3.org/1999/xhtml"> <head> ...
- 原生js实现图片轮播思路分析
一.复习原生js实现图片轮播 1.要点 自动轮播 点击小圆圈按钮,显示相应图片 点击左右箭头,实现向前向后轮播图片 2.实现思路 <div id="container"> ...
- js图片轮播效果实现代码
首先给大家看一看js图片轮播效果,如下图 具体思路: 一.页面加载.获取整个容器.所有放数字索引的li及放图片列表的ul.定义放定时器的变量.存放当前索引的变量index 二.添加定时器,每隔2秒钟i ...
- CSS3图片轮播效果
原文:CSS3图片轮播效果 在网页中用到图片轮播效果,单纯的隐藏.显示,那再简单不过了,要有动画效果,如果是自己写的话(不用jquery等),可能要费点时间.css3的出现,让动画变得不再是问题,而且 ...
- js实现淘宝首页图片轮播效果
原文:http://ce.sysu.edu.cn/hope2008/Education/ShowArticle.asp?ArticleID=10585 <!DOCTYPE html> &l ...
- ios图片轮播效果
代码地址如下:http://www.demodashi.com/demo/11959.html ImageCarousel 简单封装的图片轮播器 内存过大由于我加载的图片分辨率较高(4k) 文件目录 ...
随机推荐
- C# 将窗口移动到指定位置
private void button1_Click(object sender, EventArgs e) { Frm f = new Frm(); f.StartPosition = FormSt ...
- html中行内元素与块级元素的区别。
在标准文档流里面,块级元素具有以下特点 1.总是在新行上开始,占据一整行: 2.高度,行高以及外边框和内边距都可以控制: 3.宽度始终是与游览器宽度一样,与内容无关: 4.它可以容纳内联元素和其他块级 ...
- discuzx3.2伪静态
首先,我们要新建一个名为.htaccess的文件,文件名为空白,这点很重要.很多人无法新建这个文件,在这里教大家如何新建没有名字的文件. 新建一个TXT文本,名字先默认.然后打开这个文本,然后把我们的 ...
- CUICatalog: Invalid asset name supplied:
[UIImage imageNamed:name];但是这个name却是空的,所以就报了这个错了. 解决方法,在项目中搜索UIImage imageNamed:,然后打印看看所谓的name是否为空.找 ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- jquery.BannerRotator.js
项目地址:https://github.com/snipertulip/BannerRotator 演示地址:http://snipertulip.github.io/BannerRotator/de ...
- The Lead Game Add problem to Todo list Problem code: TLG
'''def count_lead(first, second): if first > second: return 1, first - second elif first == secon ...
- 接入SDK
管理提醒: 本帖被 fm2010 设置为精华(2014-11-12) http://www.cocoachina.com/bbs/read.php?tid-239087.html 本帖属于Co ...
- Uva220 Othello
Othello Othello is a game played by two people on an 8 x 8 board, using disks that are white on on ...
- Android ListView SimpleAdapter支持Bitmap类型图片显示
// 处理simpleAdapter中包括bitmap类型 adapter.setViewBinder(new ViewBinder() { public boolean setViewValue(V ...