最近一直在学习JavaScript,然后看到旋转轮播图的案例,就尝试着用js做了一个简单的轮播图,因为无法显示动态效果,所以就放个截图:
这个效果是这样的:一共有7张图片,它们会自动地向左滑动,然后左右箭头也可以控制轮播图的左滑和右滑,同时,如果鼠标停在图片上,那么轮播图会停止自动滑动,当鼠标移开时,将会继续向左轮播。
首先,我这里封装了两个函数(因为暂且还没有学jQuery,所以用了封装函数的方法来实现),第一个函数是$函数,调用可以获取html中的元素,代码如下:
`function $(select){ if (typeof select != 'string') { console.log('传入的参数有误'); return null; } var firstChar = select.charAt(0); switch(firstChar){ case '#': return document.getElementById(select.substr(1)); break; case '.': if (document.getElementsByClassName){ return document.getElementsByClassName(select.substr(1)); } else { var result = []; var allElemnts = document.getElementsByTagName('*'); console.log(allElemnts); for (var i = 0; i < allElemnts.length; i++){ var e = allElemnts[i]; var className = e.className; var classArr = className.split(' '); for (var j = 0; j < classArr.length; j++){ var c = classArr[j]; if (c == select.substr(1)) { result.push(e); break; } } } return result; } break; default : return document.getElementsByTagName(select); } }`
第二个函数是一个动画函数,用json实现多条样式的动态改变,从而达到一个动画效果,代码如下: `
function animate(element, json, fun) { clearInterval(element.timer); function getStyle(element, styleName){ if(element.currentStyle){ //ie浏览器下 直接通过currentstyle来获取 //return element.currentStyle.heigh; return element.currentStyle[styleName]; }else{ var computedStyle = window.getComputedStyle(element,null); return computedStyle[styleName]; } } var isStop = false; element.timer = setInterval(function () { isStop = true; for (var key in json){ var target = json[key]; var current; if (key == 'opacity') { //当动画的类型为透明度时 获取的值应该是浮点类型 current = parseFloat(getStyle(element, key)) || 1; } else { //其他情况 用整数类型 current = parseInt(getStyle(element, key)) || 0; } var step = (target - current) / 10; if (key != 'opacity') { step = step > 0 ? Math.ceil(step) : Math.floor(step); } current += step; if (key == 'opacity') { if (Math.abs(target - current) > 0.01) { isStop = false; } else { current = target; } element.style.opacity = current + ''; } else { if (Math.abs(target-current) > Math.abs(step)) { isStop = false; } else { current = target; } if (key == 'zIndex'){ element.style.zIndex = Math.round(current); } else { element.style[key] = current + 'px'; } } } if (isStop) { clearInterval(element.timer); console.log('动画完成'); if (typeof fun == 'function') { fun(); } } }, 30); }`
接下来就要写html的部分了,因为只有几张图片,所以html的部分很简单:
<body> <div> <div> <ul> <li><a href="#"><img src="https://www.jb51.net/article/images/1.jpg"></a></li> <li><a href="#"><img src="https://www.jb51.net/article/images/2.jpg"></a></li> <li><a href="#"><img src="https://www.jb51.net/article/images/3.jpg"></a></li> <li><a href="#"><img src="https://www.jb51.net/article/images/4.jpg"></a></li> <li><a href="#"><img src="https://www.jb51.net/article/images/5.jpg"></a></li> <li><a href="#"><img src="https://www.jb51.net/article/images/6.jpg"></a></li> <li><a href="#"><img src="https://www.jb51.net/article/images/7.jpg"></a></li> </ul> </div> <div> <a href="javascript:;"></a> <a href="javascript:;"></a> </div> </div> </body>
css样式的部分也不多做叙述。
下面就是JS是部分啦,代码也很简单,理清楚就好
window.onload = function(){ //定位,并给图片设置大小透明度 var json = [{ width: 630, top: 23, left: 0, zIndex: 2, opacity: 0 },{ width: 630, top: 23, left: 0, zIndex: 3, opacity: 0 },{ width: 630, top: 23, left: 0, zIndex: 4, opacity: 0.6 },{ width: 730, top: 0, left: 125, zIndex: 5, opacity: 1 },{ width: 630, top: 23, left: 350, zIndex: 4, opacity: 0.6 },{ width: 630, top: 23, left: 350, zIndex: 3, opacity: 0 },{ width: 630, top: 23, left: 350, zIndex: 2, opacity: 0 }];
function refreshImageLocatin(index){ //默认情况下 第i张图对应第i个位置 //index=1时 第i个图对应i-1个位置 //也就是第i个图对应i-index的位置 var liArr = $('li'); for(var i = 0; i < liArr.length; i++){ var li = liArr[i]; var locationIndex = i - index; console.log('i='+i); console.log('index='+index); console.log('locationIndex='+locationIndex); if(locationIndex < 0){ locationIndex += 7; } var locationData = json[locationIndex]; animate(li, locationData, null); } } refreshImageLocatin(0); var index = 0; $('#next').onclick = function(){ index++; if(index == 7){ index = 0; } refreshImageLocatin(index); } $('#prev').onclick = function(){ index--; if(index < 0){ index = 6; } refreshImageLocatin(index); } var nextImage = $('#next').onclick; var contentBox = $('.content')[0]; //自动播放 var timer = setInterval(nextImage, 3000); //当鼠标移动到图片上,停止播放 contentBox.onmouseover = function(){ clearInterval(timer); } contentBox.onmouseout = function(){ timer = setInterval(nextImage ,3000) } }
以上就是旋转轮播图的全部内容了,给自己的学习过程做个简单的记录,也顺便巩固一下。
您可能感兴趣的文章: