Created
August 21, 2014 06:30
-
-
Save rainyjune/94ed1aaf1cdaa4bf679e to your computer and use it in GitHub Desktop.
图片无缝滚动
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Js图片无缝滚动</title> | |
<style type="text/css"> | |
body{ | |
background:gray; | |
} | |
#wrap{ | |
width:810px; | |
height:200px; | |
border:1px solid black; | |
position:relative; | |
/* | |
left:50%; | |
top:50%; | |
margin-left:-410px; | |
margin-top:-250px; | |
*/ | |
margin-left: auto; | |
margin-right: auto; | |
background:#F5E0E3; | |
overflow:hidden; | |
} | |
#wrap ul{ | |
margin:0px; | |
padding:0px; | |
position:absolute; | |
top:0px; | |
left:0px; | |
} | |
#wrap ul li{ | |
list-style:none; | |
float:left; | |
margin:5px 10px; | |
} | |
#wrap ul li img{ | |
width:250px; | |
height:180px; | |
cursor:pointer; | |
} | |
.direction{ | |
width:104px; | |
margin:50px auto; | |
} | |
.direction img{ | |
border:1px dotted pink; | |
cursor:pointer; | |
} | |
.active{ | |
border:2px solid gray; | |
} | |
</style> | |
<script type="text/javascript"> | |
window.onload = initAll; | |
function initAll(){ | |
var ul = document.getElementById("wrap").getElementsByTagName("ul")[0]; | |
var lis = ul.getElementsByTagName("li"); | |
var btn1 = document.getElementById("button1"); | |
var btn2 = document.getElementById("button2"); | |
var imgs = ul.getElementsByTagName("img"); | |
var speed = 3; | |
var time = null; | |
var activeImg = null; | |
ul.innerHTML += ul.innerHTML; | |
var ulWidth = (lis[0].offsetWidth+20)*lis.length + "px"; | |
ul.style.width = ulWidth; | |
function moveFunction() { | |
ul.style.left = ul.offsetLeft - speed + "px"; | |
if(ul.offsetLeft <= -ul.offsetWidth/2){ | |
ul.style.left ="0px"; | |
}else if(ul.offsetLeft >=0){ | |
ul.style.left = -ul.offsetWidth/2 + "px"; | |
} | |
} | |
time = setInterval(moveFunction,30); | |
btn1.onmouseover = function(){ | |
speed = 3; | |
}; | |
btn2.onmouseover = function(){ | |
speed = -3; | |
}; | |
ul.onmouseover = function(e) { | |
clearInterval(time); | |
e = e || window.event; | |
var source = e.target || e.srcElement; | |
var sourceTagName = source.tagName; | |
console.log("source:", source.tagName); | |
if (sourceTagName.toLowerCase() == "img") { | |
activeImg = source; | |
activeImg.className = "active"; | |
} | |
if (e.stopPropagation) { | |
e.stopPropagation(); | |
} else { | |
e.cancelBubble = true; | |
} | |
return false; | |
}; | |
ul.onmouseout = function() { | |
time = setInterval(moveFunction,30); | |
if (activeImg) { | |
activeImg.className = "none"; | |
activeImg = null; | |
} | |
}; | |
} | |
</script> | |
</head> | |
<body> | |
<div class="direction"><img src="/jscss/demoimg/201204/left.png" alt="" id="button1"/><img src="/jscss/demoimg/201204/right.png" alt="" id="button2"/></div> | |
<div id="wrap"> | |
<ul> | |
<li><img src="http://www.codefans.net/jscss/demoimg/wall1.jpg" alt="pic one" /></li> | |
<li><img src="http://www.codefans.net/jscss/demoimg/wall2.jpg" alt="pic two" /></li> | |
<li><img src="http://www.codefans.net/jscss/demoimg/wall3.jpg" alt="pic three" /></li> | |
<li><img src="http://www.codefans.net/jscss/demoimg/wall4.jpg" alt="pic four" /></li> | |
</ul> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment