js里用节点函数写js文字轮播代码怎么写动画效果怎么写

拒绝访问 |
| 百度云加速
请打开cookies.
此网站 () 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(3a967dee-ua98).
重新安装浏览器,或使用别的浏览器2012年11月 Web 开发大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。vuejs(8)
mobile(13)
,我们使用 tween算法实现了 ease-out移动效果, 其实更简洁的方法是使用 css3的 transition动画:
在 我们通过代码来移动一段距离的时候, 使用 transion动画;在手指移动的时候,不使用transition动画.
使用 transition实现的动画效果的轮播图js代码不足100行
~function () {
var lastPX = 0; // 上一次触摸的位置x坐标, 需要计算出手指每次移动的一点点距离
var movex = 0;
// 记录手指move的x方向值
var imgWrap = document.getElementById('imgWrap');
var startX = 0; // 开始触摸时手指所在x坐标
var endX = 0;
// 触摸结束时手指所在的x坐标位置
var imgSize = imgWrap.children.length - 2;
// 图片个数
var t1 = 0;
// 记录开始触摸的时刻
var t2 = 0;
// 记录结束触摸的时刻
var width = window.innerW
// 当前窗口宽度
var nodeList = document.querySelectorAll('#imgWrap img');
// 所有轮播图节点数组 NodeList
// 给图片设置合适的left值, 注意 querySelectorAll返回
NodeList, 具有 forEach方法
nodeList.forEach(function (node, index) {
node.style.left = (index - 1) * width + 'px';
* 移动图片到当前的 tIndex索引所在位置
* @param {number} tIndex 要显示的图片的索引
function toIndex(tIndex) {
var dis = -(tIndex * width);
// 动画移动
imgWrap.style.transform = 'translate3d(' + dis + 'px, 0, 0)';
imgWrap.style.transition = 'transform .2s ease-out';
// 索引到最后一张图片, 迅速切换索引到同一张图的初始位置
setTimeout(function () {
if (tIndex === imgSize) {
imgWrap.style.transform = 'translate3d(0, 0, 0)';
imgWrap.style.transition = 'none';
movex = 0;
if (tIndex === -1) {
imgWrap.style.transform = 'translate3d(' + width * (1 - imgSize) + 'px, 0, 0)';
imgWrap.style.transition = 'none';
movex = -width * (imgSize - 1);
* 处理各种触摸事件 ,包括 touchstart, touchend, touchmove, touchcancel
* @param {Event} evt 回调函数中系统传回的 js 事件对象
function touch(evt) {
var touch = evt.targetTouches[0];
var tar = evt.
var index = parseInt(tar.getAttribute('data-index'));
if (evt.type === 'touchmove') {
var di = parseInt(touch.pageX - lastPX);
endX = touch.pageX;
imgWrap.style.webkitTransform = 'translate3d(' + movex + 'px, 0, 0)';
imgWrap.style.transition = 'none'; // 移动时避免动画延迟
lastPX = touch.pageX;
if (evt.type === 'touchend') {
var minus = endX - startX;
t2 = new Date().getTime() - t1;
if (Math.abs(minus) & 0) { // 有拖动操作
if (Math.abs(minus) & width * 0.4 && t2 & 500) { // 拖动距离不够,返回!
toIndex(index);
} else { // 超过一半,看方向
console.log(minus);
if (Math.abs(minus) & 20) {
console.log('距离很短' + minus);
toIndex(index);
if (minus & 0) { // endX & startX,向左滑动,是下一张
toIndex(index + 1)
} else { // endX & startX ,向右滑动, 是上一张
toIndex(index - 1)
} else { //没有拖动操作
if (evt.type === 'touchstart') {
lastPX = touch.pageX;
startX = lastPX;
endX = startX;
t1 = new Date().getTime();
imgWrap.addEventListener('touchstart', touch, false);
imgWrap.addEventListener('touchmove', touch, false);
imgWrap.addEventListener('touchend', touch, false);
imgWrap.addEventListener('touchcancel', touch, false);
当切换到边界值的图时,应该等到切换动画效果之后再换到相同图内容的位置
所以: & 我们使用setTimeout延迟执行 无限循环播放替换图位置的操作
至于 css 和 html嘛, 还是原来的配方, 还是原来的味道~参见&
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:325705次
积分:3559
积分:3559
排名:第9736名
原创:96篇
评论:37条
阅读:4841
阅读:26420
(3)(1)(1)(8)(2)(2)(1)(2)(1)(2)(9)(4)(5)(5)(12)(25)(8)(3)(5)(1)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'& & &在团队带人,突然被人问到轮播图如何实现,进入前端领域有一年多了,但很久没自己写过,一直是用大牛写的插件,今天就写个简单的适合入门者学习的小教程。当然,轮播图的实现原理与设计模式有很多种,我这里讲的是用面向过程函数式编程去实现,相对于面向对象设计模式,代码难免会显得臃肿冗余。但没有面向对象的抽象却很适合新手理解与学习。已经在BAT的同学看到希望少喷点。另外可以多提意见。
轮播图的原理:
一系列的大小相等的图片平铺,利用CSS布局只显示一张图片,其余隐藏。通过计算偏移量利用定时器实现自动播放,或通过手动点击事件切换图片。
&&&&首先父容器container存放所有内容,子容器list存在图片。子容器buttons存放按钮小圆点。
1 &div id="container"&
&div id="list" style="left: -600"&
&img src="img/5.jpg" alt="1" /&
&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/5.jpg" alt="5" /&
&img src="img/1.jpg" alt="5" /&
&div id="buttons"&
&span index="1" class="on"&&/span&
&span index="2"&&/span&
&span index="3"&&/span&
&span index="4"&&/span&
&span index="5"&&/span&
&a href="javascript:;" id="prev" class="arrow"&&&/a&
&a href="javascript:;" id="next" class="arrow"&&&/a&
优化,无缝滚动。
当你从最后一张图切换回第一张图时,有很大空白,利用两张辅助图来填补这个空白。
这里补充下无缝滚动,直接看代码,复制最后一张图片放置第一张图片前,同时复制第一张图片放置最后一张图片的后面。并且,将第一张图片辅助图(实际上是实际显示的第5张图片隐藏起来,故设置style="left: -600")
1、对盒子模型,文档流的理解,绝对定位问题。
2、注意list的overflow:只显示窗口的一张图片,把左右两边的都隐藏起来。
3、确保buttons中每个span所在层置顶,将其设置为最顶端。(z-index:999)我这里设置为z-index:2
margin: 0;
padding: 0;
text-decoration: none;
padding: 20px;
#container {
position: relative;
width: 600px;
height: 400px;
border: 3px solid #333;
overflow: hidden;
position: absolute;
z-index: 1;
width: 4200px;
height: 400px;
#list img {
float: left;
width: 600px;
height: 400px;
#buttons {
position: absolute;
left: 250px;
bottom: 20px;
z-index: 2;
height: 10px;
width: 100px;
#buttons span {
float: left;
margin-right: 5px;
width: 10px;
height: 10px;
border: 1px solid #fff;
border-radius: 50%;
background: #333;
cursor: pointer;
#buttons .on {
background: orangered;
position: absolute;
top: 180px;
z-index: 2;
display: none;
width: 40px;
height: 40px;
font-size: 36px;
font-weight: bold;
line-height: 39px;
text-align: center;
color: #fff;
background-color: RGBA(0, 0, 0, .3);
cursor: pointer;
.arrow:hover {
background-color: RGBA(0, 0, 0, .7);
#container:hover .arrow {
display: block;
left: 20px;
right: 20px;
首先我们先实现出手动点击左右两个箭头切换图片的效果:
window.onload = function() {
var list = document.getElementById('list');var prev = document.getElementById('prev');
var next = document.getElementById('next');
function animate(offset) {
//获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
//且style.left获取的是字符串,需要用parseInt()取整转化为数字。
var newLeft = parseInt(list.style.left) +
list.style.left = newLeft + 'px';
prev.onclick = function() {
animate(600);
next.onclick = function() {
animate(-600);
运行后我们会发现,一直点击右箭头 ,会出现空白,而且,不能回到第一张图片。要点击左箭头才能回到第一张图片。
利用谷歌浏览器F12,原因是我们利用偏移量left来获取图片,当看到left值小于3600时,因为没有第8张图片就出现空白,所以这里我们需要对偏移量做一个判断。
在animate函数里加上这么一段:
if(newLeft&-3000){
list.style.left = -600 + 'px';
if(newLeft&-600){
list.style.left = -3000 + 'px';
好,运行一下,没问题了。轮播图,顾名思义,是自己会动的图片,这个时候我们需要用到浏览器的内置对象定时器。
对于定时器,有必要说明一下setInterval()跟setTimeout的区别了。简单来说,setInterval()执行多次,setTimeout()只执行一次。
更具体的用法可以点击链接查看区别:& &。
这里我们是用setInterval(),因为我们的图片需要循环滚动。插入下面
function play() {
timer = setInterval(function () {
prev.onclick()
运行,ok!
但是,当我们想仔细看某一张图片时候,要把图片停住,我们清楚定时器就可以了,这里用到&这个方法。
这里,我们需要对其DOM操作,需要获取整个轮播图区域;
var container = document.getElementById('container');
function stop() {
clearInterval(timer);
container.onmouseover =
container.onmouseout =
但这里,一个轮播图基本算完成了,有同学&会问,那么简单。看到图片下面的那一排小圆点没。我给你加功能了。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
这里是升级版:
var buttons = document.getElementById('buttons').getElementsByTagName('span');
var index = 1;
function buttonsShow() {
//这里需要清除之前的样式
for (var i = 0; i & buttons. i++) {
if (buttons[i].className == 'on') {
buttons[i].className = '';
//数组从0开始,故index需要-1
buttons[index - 1].className = 'on';
prev.onclick = function() {
index -= 1;
if (index & 1) {
index = 5;
buttonsShow();
animate(600);
next.onclick = function() {
//由于上边定时器的作用,index会一直递增下去,我们只有5个小圆点,所以需要做出判断
index += 1;
if (index & 5) {
index = 1;
buttonsShow();
animate(-600);
现在看起来正常多了吧,但我们想实现通过鼠标任意点击其中一个小圆点,切换到相应的图片,原理同样,我们还是需要通过偏移量去找到对应的图片。
for (var i = 0; i & buttons. i++) {
buttons[i].onclick = function () {
// 在浏览器的控制台打印一下,看看结果
console.log(i);
/* 偏移量获取:这里获得鼠标移动到小圆点的位置,用this把index绑定到对象buttons[i]上,去谷歌this的用法
/* 由于这里的index是自定义属性,需要用到getAttribute()这个DOM2级方法,去获取自定义index的属性*/
var clickIndex = parseInt(this.getAttribute('index'));
var offset = 600 * (index - clickIndex);
animate(offset); //存放鼠标点击后的位置,用于小圆点的正常显示
index = clickI
buttonsShow();
到这一步时,以为大功告成?你在控制台会发现打印出来的永远的是i=5。
错误原因:没有正确获取i值,使用闭包就可以了。你在高级程序设计第三版中76页,会看到这么一句话:
& & & & & & &对javascript来说,由for语句创建的变量i即使在for循环执行结束后,也依旧会存在于循环外部的执行环境中。&
就是说,js没有块级作用域这东西,(可能我C写多了,混淆了)。在第一次循环(从 i=0 到 4 这一过程)结束后,最后的 i 获取到的为&buttons.length 的值被
保存在for循环之外,最后鼠标点击任何一个小圆点时,自然访问的一直是 i=5 了。
正确代码如下:
for (var i = 0; i & buttons. i++) {
// 这里使用的是立即执行函数,
(function(i) {
buttons[i].onclick = function() {
var clickIndex = parseInt(this.getAttribute('index'));
var offset = 600 * (index - clickIndex);
animate(offset);
index = clickI
buttonsShow();
&有关闭包的知识我不展开来说,要说的又一大推,
大家可以参考下我很久之前的博客:
大家,可能发现了,这个轮播图有点奇怪,不中规中矩,它是向左切换的,改写一下:
function play() {
//将轮播图换成向右切换图片
timer = setInterval(function () {
next.onclick();
1 &!DOCTYPE html&
&meta charset="UTF-8"&
&title&&/title&
&style type="text/css"&
margin: 0;
padding: 0;
text-decoration: none;
padding: 20px;
#container {
position: relative;
width: 600px;
height: 400px;
border: 3px solid #333;
overflow: hidden;
position: absolute;
z-index: 1;
width: 4200px;
height: 400px;
#list img {
float: left;
width: 600px;
height: 400px;
#buttons {
position: absolute;
left: 250px;
bottom: 20px;
z-index: 2;
height: 10px;
width: 100px;
#buttons span {
float: left;
margin-right: 5px;
width: 10px;
height: 10px;
border: 1px solid #fff;
border-radius: 50%;
background: #333;
cursor: pointer;
#buttons .on {
background: orangered;
position: absolute;
top: 180px;
z-index: 2;
display: none;
width: 40px;
height: 40px;
font-size: 36px;
font-weight: bold;
line-height: 39px;
text-align: center;
color: #fff;
background-color: RGBA(0, 0, 0, .3);
cursor: pointer;
.arrow:hover {
background-color: RGBA(0, 0, 0, .7);
#container:hover .arrow {
display: block;
left: 20px;
right: 20px;
&script type="text/javascript"&
/* 知识点:
this用法 */
DOM事件 */
window.onload = function() {
var container = document.getElementById('container');
var list = document.getElementById('list');
var buttons = document.getElementById('buttons').getElementsByTagName('span');
var prev = document.getElementById('prev');
var next = document.getElementById('next');
var index = 1;
function animate(offset) {
//获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
//且style.left获取的是字符串,需要用parseInt()取整转化为数字。
var newLeft = parseInt(list.style.left) +
list.style.left = newLeft + 'px';
//无限滚动判断
if (newLeft & -600) {
list.style.left = -3000 + 'px';
if (newLeft & -3000) {
list.style.left = -600 + 'px';
function play() {
//重复执行的定时器
timer = setInterval(function() {
next.onclick();
function stop() {
clearInterval(timer);
function buttonsShow() {
//将之前的小圆点的样式清除
for (var i = 0; i & buttons. i++) {
if (buttons[i].className == "on") {
buttons[i].className = "";
//数组从0开始,故index需要-1
buttons[index - 1].className = "on";
prev.onclick = function() {
index -= 1;
if (index & 1) {
buttonsShow();
animate(600);
next.onclick = function() {
//由于上边定时器的作用,index会一直递增下去,我们只有5个小圆点,所以需要做出判断
index += 1;
if (index & 5) {
animate(-600);
buttonsShow();
for (var i = 0; i & buttons. i++) {
(function(i) {
buttons[i].onclick = function() {
这里获得鼠标移动到小圆点的位置,用this把index绑定到对象buttons[i]上,去谷歌this的用法
由于这里的index是自定义属性,需要用到getAttribute()这个DOM2级方法,去获取自定义index的属性*/
var clickIndex = parseInt(this.getAttribute('index'));
var offset = 600 * (index - clickIndex); //这个index是当前图片停留时的index
animate(offset);
index = clickI //存放鼠标点击后的位置,用于小圆点的正常显示
buttonsShow();
container.onmouseover =
container.onmouseout =
186 &/head&
188 &body&
&div id="container"&
&div id="list" style="left: -600"&
&img src="" alt="1" /&
&img src="" alt="1" /&
&img src="" alt="2" /&
&img src="" alt="3" /&
&img src="" alt="4" /&
&img src="" alt="5" /&
&img src="" alt="5" /&
&div id="buttons"&
&span index="1" class="on"&&/span&
&span index="2"&&/span&
&span index="3"&&/span&
&span index="4"&&/span&
&span index="5"&&/span&
&a href="javascript:;" id="prev" class="arrow"&&&/a&
&a href="javascript:;" id="next" class="arrow"&&&/a&
211 &/body&
213 &/html&
点击有惊喜
这里结合评论,补充下:
就算是初学者也要特别注意代码规范问题,上面的CSS实在太不规范了,难怪我带的人都不怎样,哈哈哈。。。
Css的书写顺序
CSS里最好不用或直接不用id来改变样式(我这里是id为了方便DOM操作)
这里推荐css规范的技术文章
前端前辈的博客: &
或者这位大神的Github:
以及的css编码规范:&
最后,我们完成了一个简单的轮播图,在我的 &&&里可以找到源码。觉得不错就star一下。
阅读(...) 评论()}

我要回帖

更多关于 js函数怎么写 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信