排列这些图像时如何使用z索引(或者应该使用z索引)
How do I use z-index (or should I) when arranging these images?
我当前使用.onload
在打开浏览器时随机排列图像。加载页面时,图像会随机"扩散"到页面上。当单击它们时,它们会被带到屏幕上的特定坐标。第一张图片是一个回收站。我希望这张图片是中心图片,所有其他图片都在里面(或者在这种情况下是"后面")。我应该为每个单独的图像设置z索引吗?这是我所拥有的:
任何建议都很有用!
<html>
<head>
<style>
body{
padding: 0;
margin: 0;
}
div{
position: fixed;
top: 0;
left: 300;
width: 35%;
height: 35%;
float: left;
transition: width 5s, top 5s, left 5s;
}
div:hover{
width: 100%;
}
</style>
</head>
<body>
<div class="div">
<a target="_blank">
<img src="curbside%20collection%20empty%20yellow%20bin.png" alt= width=100% height=100%>
</a>
</div>
<div class="div">
<a target="_blank">
<img src="Bomb.png" alt= width=50% height=50%>
</a>
</div>
<script>
document.body.onload = function() {
var mycolours = [];
var elements =
document.getElementsByTagName("div");
for(var i = 0; i < elements.length; i++){
elements[i].style.backgroundColor =
mycolours[i%mycolours.length]; // remainder operator magic
elements[i].style.top = Math.random() * innerHeight;
elements[i].style.left = Math.random() * innerWidth;
elements[i].onclick = function(){
console.log(this.style.top);
if(this.style.top === "300px"){
this.style.top = Math.random() * window.innerHeight;
this.style.left = Math.random() * window.innerWidth;
}
else{
this.style.top = 300;
this.style.left = 500;
}
}
}
}
</script>
</body>
</html>
我建议使用class
的,而不是必须设置每个图像的z-index
。这样,你就可以设置图像类的属性,比如说,你的图像类都有一个特定的z-index
,然后你也可以为回收箱设置另一个类
我也认为你目前使用class
是错误的。
为了在CSS中选择具有类的DOM元素,您应该使用.
(句点)作为前缀。您使用CSS的方式是选择div
DOM元素
即。
#div { //properties of the element with "div" as the ID }
.div { //properties of all elements with the "div" class }
div { //properties of <div> elements }
请记住,一个类可以应用于DOM内部的多个元素,但一个ID只能应用于DOM内的一个元素。
相关文章:
- 名称输入的索引
- 在jQuery中获取表的行索引
- 测试索引值是否等于某个数字的倍数
- 循环遍历数组中的特定索引
- 按照选项卡索引的顺序循环一个jQuery选择
- 在JavaScript中通过索引从对象数组中获取值
- 如何删除除冒号、数字和'上午'或者'下午'
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- 在索引.html和应用.js [node.js] 之间共享变量
- 如何“;过滤器”;或者以其他方式重构该数据
- 如何为高图中的区域线创建z索引
- 下拉列表在使用z索引放置在前面后停止工作
- 减去两个索引不同但值相同的整数
- 如何通过所选索引(AngularJS)在模态弹出窗口中显示数据
- Javascript:根据对象的嵌套数组中的值,在数组中查找对象的索引
- 用javascript查询lucene索引
- 使用它更新集合中的嵌套数组's索引
- 如何将ng选项的索引作为angularJs中的值传递给模型
- 将页面元素索引到 JSON 对象?或者jQuery选择器它每次
- 排列这些图像时如何使用z索引(或者应该使用z索引)