【发布时间】:2018-08-31 09:16:55
【问题描述】:
对不起,如果它太愚蠢了。 我想用纯 JavaScript 自己制作一个图像滑块。所以我使用了以下代码。我是 javascript 的初学者
<div class="w3-div" id="img-div" style="width:100%; height: 480px; border: 4px solid black;">
<!-- i'm not making image position absolute because if i make it absolute it is not fitting in the div morever the fixed header is not working--->
<img src="assets/pics/1.jpg" id="header" class="w3-image" style="width: 100%; height:473px; padding-bottom: 0px;">
</div>
<script type="text/javascript">
window.onload = function() {
setInterval(changepic, 10000);
}
function changepic() {
//alert("changing pic ");
var p = document.getElementById('header');
p.style.position = "absolute";
//i know that style.width returns string but what else i can do?
//loop to slide out the img from div
for (var i = 0; i <= document.getElementById('img-div').style.width; i++) {
p.style.left = i + "px";
//p.style.right= i+"px";
}
// loop to slide in the image in div
for (var i = document.getElementById('img-div').style.width; i >= 0; i--) {
//p.style.left = i+"px";
p.style.right = i + "px";
}
//put random image over here or just relocate the first image
p.style.position = "relative";
p.style.left = "0px";
}
</script>
【问题讨论】:
-
for 循环也有语法错误w3schools.com/js/js_loop_for.asp,你只是在改变'header'元素的样式。
标签: javascript html css image dom