龙空技术网

JavaScript 图片横向滚动轮播

寒笛过霜天 100

前言:

此时各位老铁们对“js无缝滚动轮播图”大约比较关注,看官们都想要分析一些“js无缝滚动轮播图”的相关内容。那么小编同时在网摘上收集了一些关于“js无缝滚动轮播图””的相关知识,希望咱们能喜欢,各位老铁们快快来了解一下吧!

实例1: 无缝连续滚动

<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><style type="text/css">*{margin: 0;padding: 0;list-style: none;}div{width: 1000px;height: 130px;margin: 100px auto;border: 1px solid #000;position: relative;overflow: hidden; /* 防止滚动的图片溢出 */}ul{width: 2000px;position: absolute;left: 0;top: 0;}li{float: left;}img{display: block;}</style></head><body><div><ul><li><img src="img/1.png"/></li><li><img src="img/2.png"/></li><li><img src="img/3.png"/></li><li><img src="img/4.png"/></li><li><img src="img/5.png"/></li><!--我们在复制一套出来--><li><img src="img/1.png"/></li><li><img src="img/2.png"/></li><li><img src="img/3.png"/></li><li><img src="img/4.png"/></li><li><img src="img/5.png"/></li></ul></div><script type="text/javascript">var ul = document.getElementsByTagName("ul")[0];setInterval(function(){    if(getStyle(ul,"left")===-1000){    ul.style.left = "0px";//执行这一行代码需要多长时间???    }    ul.style.left = getStyle(ul,"left") - 1 + "px";},10)function getStyle(obj,attr){    if(window.getComputedStyle){    return parseInt(window.getComputedStyle(obj)[attr]);    }else{    return parseInt(obj.currentStyle[attr]);    }}</script></body></html>

图片规格: 200px 130px

优化代码

<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><style type="text/css">*{margin: 0;padding: 0;list-style: none;}div{width: 1000px;height: 130px;margin: 100px auto;border: 1px solid #000;position: relative;overflow: hidden;}ul{width: 2000px;position: absolute;left: 0;top: 0;}li{float: left;}img{display: block;}</style></head><body><div><ul><li><img src="img/1.png"/></li><li><img src="img/2.png"/></li><li><img src="img/3.png"/></li><li><img src="img/4.png"/></li><li><img src="img/5.png"/></li></ul></div><script type="text/javascript">    var oUl = document.getElementsByTagName("ul")[0];    oUl.innerHTML += oUl.innerHTML //复制一份图像    var timer = null;    move();    oUl.onmouseover = function(){    clearInterval(timer);    }    oUl.onmouseout = function(){    move();		}function move(){    timer = setInterval(function(){    if(getStyle(oUl,"left")===-1000){    oUl.style.left = "0px";//执行这一行代码需要多长时间???    }    oUl.style.left = getStyle(oUl,"left") - 1 + "px";    },10)}function getStyle(obj,attr){    if(window.getComputedStyle){    return parseInt(window.getComputedStyle(obj)[attr]);    }else{    return parseInt(obj.currentStyle[attr]);    }}</script></body></html>

实例2: 横向跳动轮播

<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><style type="text/css">*{margin: 0;padding: 0;list-style: none;}div{width: 1000px;height: 130px;margin: 100px auto;border: 1px solid #000;position: relative;overflow: hidden; /* 防止图片溢出 */}ul{width: 2000px;position: absolute;left: 0;top: 0;}li{float: left;}img{display: block;}</style></head><body><div><ul><li><img src="img/1.png"/></li><li><img src="img/2.png"/></li><li><img src="img/3.png"/></li><li><img src="img/4.png"/></li><li><img src="img/5.png"/></li><!--我们在复制一套出来--><li><img src="img/1.png"/></li><li><img src="img/2.png"/></li><li><img src="img/3.png"/></li><li><img src="img/4.png"/></li><li><img src="img/5.png"/></li></ul></div><script type="text/javascript">var ul = document.getElementsByTagName("ul")[0];var timer = null;var num = 0;setInterval(function(){  if(getStyle(ul,"left")===-1000){  ul.style.left = "0px";  num = 0;}num--;setTimeout(function(){    timer = setInterval(function(){    if(getStyle(ul,"left")==200*num){    clearInterval(timer);    }else{    ul.style.left = getStyle(ul,"left") - 5 + "px";    }    },10)    },100)},2000)function getStyle(obj,attr){if(window.getComputedStyle){return parseInt(window.getComputedStyle(obj)[attr]);}else{return parseInt(obj.currentStyle[attr]);}}</script></body></html>

图片规格: 200px 130px

标签: #js无缝滚动轮播图