87,921
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script language="javascript" type="text/javascript">
var scrollFun = function (dir) {
var box = document.getElementById("box");
var con = document.getElementById("con");
var boxWidth = box.offsetWidth;
var conWidth = con.offsetWidth;
var left = +con.style.left.replace("px", "");
if (dir === "left") {
if (left <= boxWidth - conWidth) {
return;
}
con.style.left = (left - 40) + "px";
}
else if (dir === "right") {
if (left === 0) {
return;
}
con.style.left = (left + 40) + "px";
}
};
window.onload = scrollFun;
</script>
<style type="text/css">
.box{ border:1px solid red; width:40px; height:50px; position:relative; overflow:hidden;}
.con{ position:absolute; top:20px; white-space:nowrap;}
</style>
</head>
<body>
<input type="button" onclick="scrollFun('left')" value="左" />
<input type="button" onclick="scrollFun('right')" value="右" />
<div id="box" class="box">
<div id="con" class="con" >滚动1滚动2滚动3滚动4滚动5滚动6滚动7滚动8滚动9</div>
</div>
</body>
</html>