我创建了一个div,使其可以使用<p>text</p>
滚动,如下所示:
代码有2个可滚动按钮,一旦滚动到最后就会改变颜色。
<!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>
<title></title>
<style rel="stylesheet" type="text/css">
#scroller
{
border: 1px solid black;
height: 80px;
width: 400px;
overflow-y: hidden;
overflow-x: hidden;
}
a
{
display:block;
padding:10px;
background-color:red;
width:25px;
position:relative;
top:20px;
}
div
{
float:left;
}
#type
{
position:absolute;
top: 100px;
left: 250px
}
#left
{
background-color:blue;
}
</style>
<script type="text/javascript">
var PixelPerInterval = 1;
var stop = false;
var t;
var scroll_width;
function scroll_left()
{
document.getElementById('right').style.backgroundColor='red';
document.getElementById('scroller').scrollLeft-=PixelPerInterval;
if(!stop)
t = setTimeout("scroll_left()",10);
else
stop = false;
scrX = document.getElementById('scroller').scrollLeft;
divwidth=document.getElementById('scroller').clientWidth;
pwidth=document.getElementById('cont').clientWidth;
type.innerHTML = scrX;
if ((scrX) <= 0)
//type.innerHTML = scrX + ' left';
document.getElementById('left').style.backgroundColor='blue';
}
function scroll_right()
{
document.getElementById('left').style.backgroundColor='red';
document.getElementById('scroller').scrollLeft+=PixelPerInterval;
if(!stop)
t = setTimeout("scroll_right()",10);
else
stop = false;
scrX = document.getElementById('scroller').scrollLeft;
divwidth=document.getElementById('scroller').clientWidth;
pwidth=document.getElementById('cont').clientWidth;
type.innerHTML = scrX;
if ((divwidth+scrX) >= pwidth)
//type.innerHTML = scrX + ' right';
document.getElementById('right').style.backgroundColor='blue';
}
function stop_scroll() {
stop = true;
}
</script>
</head>
<body>
<div><a id="left" onmouseover="scroll_left();" onmouseout="stop_scroll();" href="#">Left</a></div>
<div id="scroller">
<p id="cont" style="width:700px;">
alot of textalot of textalot of textalot of textalot of textalot of textalot of textalot of textalot of text<br/>
alot of textalot of textalot of textalot of textalot of textalot of textalot of textalot of textalot of text<br/>
alot of textalot of textalot of textalot of textalot of textalot of textalot of textalot of textalot of text<br/>
alot of textalot of textalot of textalot of textalot of textalot of textalot of textalot of textalot of text<br/>
alot of textalot of textalot of textalot of textalot of textalot of textalot of textalot of textalot of text<br/>
</p>
</div>
<div><a id="right" onmouseover="scroll_right();" onmouseout="stop_scroll();" href="#">Right</a></div>
<p id="type">asd</p>
</body>
添加图片时,它们不会向右溢出而只会向下溢出。
非常感谢你的帮助。
答案 0 :(得分:1)
这是一个与你纯粹在CSS中做的评论有关的答案,它不是CSS 3,只是CSS 2.1 你走了:
HTML:
<div id="wrapper">
<div id="container">
<img src="http://whatscookingmexico.com/wp-content/uploads/2007/09/pear1.jpg" alt="" />
<img src="http://whatscookingmexico.com/wp-content/uploads/2007/09/pear1.jpg" alt="" />
<img src="http://whatscookingmexico.com/wp-content/uploads/2007/09/pear1.jpg" alt="" />
</div>
</div>
CSS:
#wrapper {
width: 200px;
border: 1px solid #333;
overflow: auto;
}
#container {
white-space: nowrap;
}