使用next按钮显示z-Index幻灯片
z-Index slide show using the next button
所以我必须编写一个按钮,使用z-index转到幻灯片中的下一张图片。我很难让它工作,我觉得好像我做错了什么。它的计数必须为0 1 2 0 1 2
<!DOCTYPE html>
<html lang = "en">
<head>
<title>Lab 5, Part 1</title>
<meta charset = "utf-8"/>
<script type = "text/javascript">
function Next() {
document.getElementById('anime1').style.zIndex = 0;
document.getElementById('anime2').style.zIndex = 1;
document.getElementById('anime3').style.zIndex = 2;
}
</script>
<style type = "text/css">
.anime1 {position: absolute;
top: 150px; left: 250px; z-index: 10;}
.anime2 {position: absolute;
top: 200px; left: 300px; z-index: 15;}
.anime3 {position: absolute;
top: 250px; left: 350px; z-index: 20;}
</style>
</head>
<body>
<h1 style= "text-align: center">Lab 5, Part 1</h1>
<p>
<div class="slideshow">
<img class = "anime1" id = "anime1" height = "300"
width = "450" src = "http://images5.fanpop.com/image/photos/29300000/Megurine-Luka-megurine-luka-29391390-1680-1050.jpg"
alt = "First Image"/>
<img class = "anime2" id = "anime2" height = "300"
width = "450" src = "http://orig06.deviantart.net/a28f/f/2015/079/9/a/hinata_final_lr_by_artgerm-d8me6vb.jpg"
alt = "Second Image"/>
<img class = "anime3" id = "anime3" height = "300"
width = "450" src = "http://images6.fanpop.com/image/photos/35700000/Hatsune-Miku-snowangel_-35736242-1600-1200.jpg"
alt = "Third Image"/>
</p>
<input type="button" value="Next" onclick="Next();">
</body>
</html>
我在网上到处找,看是否有什么可以帮助我,但我找不到任何东西
如果您愿意采用更程序化的方法,您可以使用数组来保存顺序并迭代它来设置z索引。
使用这个方法你可以
-
pop()
表示数组末尾的项,unshift()
表示数组开头的项,或者 -
shift()
表示数组开始的项,push()
表示数组结束的项。
它允许你轻松地处理任意数量的元素,同时保持你的代码DRY。
我冒昧地制作了一个后退按钮和下一个按钮,向您展示当以这种方式接近它时是多么容易。我还概括了类名,并在演示中使用了不同的占位符图像。
(function(){ // keep it safe
var slideshow = document.querySelector('.slideshow'); // store the parent
var controls = slideshow.querySelector('.controls'); // store the controls
var els = slideshow.querySelectorAll('.slide'); // store the slides
var order = Object.keys(els); // store the order
var cn; // make the class holder
// assign a click handler to the parent
controls.onclick = function(e) {
// if the class is back or next, store it, otherwise stop here
if(!(cn = (e.target.className.match(/back|next/)||[false])[0])) return;
// if back clicked, move the last element to the beginning
if(cn === "back") order.unshift(order.pop());
// if next clicked, move the first element to the end
if(cn === "next") order.push(order.shift());
// iterate the order, set the z-index of each element sequentially
for(var i in order) els[order[i]].style.zIndex = i;
}
})();
.slides { position: relative; margin-top: 5px; }
.slide { position: absolute; }
.slide2 { top: 25px; left: 25px; }
.slide3 { top: 50px; left: 50px; }
<div class="slideshow">
<div class="controls">
<button class="back">Back</button>
<button class="next">Next</button>
</div>
<div class="slides">
<img class="slide slide1" src="http://placehold.it/150x150/f9fd42/fff">
<img class="slide slide2" src="http://placehold.it/150x150/42f9fd/fff">
<img class="slide slide3" src="http://placehold.it/150x150/fd42f9/fff">
</div>
</div>
进一步阅读
- Array.prototype.pop() <一口><子> (MDN) 子>一口>
- Array.prototype.unshift() <一口><子> (MDN) 子>一口>
- Array.prototype.shift() <一口><子> (MDN) 子>一口>
- Array.prototype.push() <一口><子> (MDN) 子>一口>
- Don't Repeat Yourself (Wikipedia)
您是否正在寻找这样的东西:https://jsfiddle.net/5L7jk73g/
var cnt = 0;
function Next() {
if (cnt == 0) {
document.getElementById('anime1').style.zIndex = 0;
document.getElementById('anime2').style.zIndex = 1;
document.getElementById('anime3').style.zIndex = 2;
cnt++;
} else if (cnt == 1) {
document.getElementById('anime1').style.zIndex = 1;
document.getElementById('anime2').style.zIndex = 2;
document.getElementById('anime3').style.zIndex = 0;
cnt++;
} else {
document.getElementById('anime1').style.zIndex = 2;
document.getElementById('anime2').style.zIndex = 0;
document.getElementById('anime3').style.zIndex = 1;
cnt = cnt - 2;
}
}
相关文章:
- Emberjs应用程序加载在除Index之外的所有路由上
- 幻灯片滚动javascript不起作用
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 分析高度属性时出现意外值{{specs.height}}.index.html
- 主干模板:index.jst.eco到index.jst.ejs
- 创建具有可变长度幻灯片显示的幻灯片
- 将 jQuery 代码添加到 Index.aspx 页面
- 通过幻灯片更改事件停止使用jquery的音频
- AngularJS-需要在index.html页面中访问来自服务的数据
- 更改基于它的Select OPTION's INDEX(Jquery)
- 不能在同一页上进行多个jquery幻灯片切换
- 我可以使此幻灯片图像自动播放吗?
- 尝试创建一个具有z-index的堆叠菜单'使用HTML5/CSS3/JS
- JS幻灯片与CSS背景颜色变化
- 带有计数器、缩略图、进度条和淡入淡出效果的简单jQuery幻灯片
- jQuery图像幻灯片,在停止后淡出到第一张图像
- 删除“;上一个“;以及“;下一个“;基于当前幻灯片
- JQuery幻灯片,可点击编号位置
- 使用next按钮显示z-Index幻灯片
- 为什么幻灯片不能使用索引数组或index array.src