直接在 Javascript 幻灯片上添加文本

Adding Text Right On Javascript Slide Show

本文关键字:添加 文本 幻灯片 Javascript      更新时间:2023-09-26

我尝试在javascript幻灯片上添加一些文本,但不知何故它不起作用。文本显示在顶部,幻灯片在文本下方循环播放。

如何使文本直接显示在图像上? 请看一看

<html>
<head>
<script type="text/javascript">
<!--
var image1=new Image()
image1.src="firstcar.gif"
var image2=new Image()
image2.src="secondcar.gif"
var image3=new Image()
image3.src="thirdcar.gif"
//-->
</script>
</head>
<body>
<div style="width:100px; height:56px">
    <div style="width:100px; height:56px; z-index:2">Tring to add some text on the top
        <div style="width:100px; height:56px; z-index:1">
              <img src="firstcar.gif" name="slide" width="100" height="56" />
        </div>
    </div>
</div>
<script>
<!--
var step=1
function slideit(){
if (!document.images)
return
document.images.slide.src=eval("image"+step+".src")
if (step<3)
step++
else
step=1
setTimeout("slideit()",1500)
}
slideit()
//-->
</script>
</body>
</html>

如果你想使用 z-index 你可能需要位置:绝对

或者,您可以将 img 的高度降低到小于文本的div 容器

试试这个:

 <body>
 <div style="width:100px; height:56px">
 <div style="position:absolute; z-index:2">Tring to add some text on the top
</div>
 <div style="position: absolute;width:100px; height:56px; z-index:1">
          <img src="firstcar.gif" name="slide" width="100" height="56" />
    </div>
</div>
</body>

将要添加的文本移动到图像元素下方的元素中,为其提供绝对定位,为包含的div 提供相对位置,然后使用上、左、右或下定位文本。

.HTML:

<div class="container">
    <img src="firstcar.gif" name="slide" width="100" height="56" />
    <span>Added Text Here</span>
</div>

.CSS:

div.container {
    position: relative;
}
span {
    position: absolute;
    top: 0;
}