javascript - 如何在此代码中从左向右滑动文本而不是图像?
问题描述
我在这里有用于滑动图像的代码,但我想为滑动文本而不是图像做同样的事情,但是当我尝试它时它对我不起作用,所以任何人都可以为我做它从左到右移动的文本滑块没有鼠标点击的图像滑块?并提前感谢
var mainImage = document.getElementById("mainImage");
//Create image array
var imageArray = ["Life is what happens to us while we are making other plans","Life is what happens to us while we are making other plans","Life is what happens to us while we are making other plans"];
//Set up array index
var imageIndex = 0;
function changeImage() {
mainImage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if(imageIndex >= imageArray.length) {
imageIndex = 0;
}
}
//Create function to cycle through images
mainImage.onclick = changeImage
//Call cycle function
var intervalHandle = setInterval(changeImage,3000);
mainImage.onclick = function () {
clearInterval(intervalHandle);
}
h1 {
text-align: center;
}
#mainImage {
padding-left: 350px;
padding-top: 40px;
}
//<img id="mainImage" src="images/D.png" />
解决方案
你需要使用HTML DOM innerHTML Property
.
创建一个有id="text"
变化的元素document.getElementById("text").innerHTML
var textElement = document.getElementById("text");
//Create image array
var imageArray = ["Life is what happens to us while we are making other plans","Life is what happens to us while we are making other plans2","Life is what happens to us while we are making other plans3"];
//Set up array index
var imageIndex = 0;
function changeImage() {
textElement.innerHTML = imageArray[imageIndex]
imageIndex++;
if(imageIndex >= imageArray.length) {
imageIndex = 0;
}
}
//Create function to cycle through images
textElement.onclick = changeImage
//Call cycle function
var intervalHandle = setInterval(changeImage,3000);
textElement.onclick = function () {
clearInterval(intervalHandle);
}
h1 {
text-align: center;
}
#mainImage {
padding-left: 350px;
padding-top: 40px;
}
<p id="text"></p>
推荐阅读
- python - 如何在 Python 套接字中接收和组装可变长度的字节数组?
- sip - 在 Ubuntu Pjsip PJproject 上配置//make 错误
- angular - Angular Nativescript 无法在惰性模块中导航
- python - 正确设置 DIR 时出现 Django TemplateDoesNotExist 问题
- mailchimp-api-v3.0 - Mailchimp 帖子列表返回 HTML 文档?
- git - 将 .git/config 中子模块的 https-address 更改为 ssh-address
- vba - 如何使用 Forn_onError 事件避免默认访问错误?
- android - 从子列表中删除对象已删除的 android
- yocto - libc.so.6 和 libc.so 都存在于 rootfs 中
- ios - 无法在 Visual Studio 中保存 .xcasset 文件