gpt4 book ai didi

javascript - 创建带有可滚动图片的 div 容器时出现问题

转载 作者:行者123 更新时间:2023-11-28 19:02:06 26 4
gpt4 key购买 nike

我创建了一个 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>


添加图片时,它们不会向右溢出,只会向下溢出。非常感谢您的帮助。

最佳答案

这是与您关于纯粹使用 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;
}

工作示例:http://jsfiddle.net/pdJ6r/2/

关于javascript - 创建带有可滚动图片的 div 容器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5259827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com