使用 jQuery 更改重叠图像的 z-index
我有三个矩形图像重叠在对角线上,第一个在左上角完全可见,第二个位于中心,部分被第一个隐藏,最后一个位于右下角,部分被第二个隐藏。 我希望单击的图像位于其他图像之上。我试图通过使用 jQuery 操作 z-index 来实现这一点,但它似乎不起作用。实际上似乎 jQuery 甚至没有被识别。
这是我的测试代码:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js">
$(document).ready(function () {
$('#launch').click(function () {
alert('Ok');
});
$('.bottom-pic').click(function () {
$(this).css('z-index' : '1');
$('.bottom-pic').not(this).css('z-index' : '0');
});
});
</script>
<style type="text/css">
#pictures {
width: 650px;
height: 300px;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
margin-bottom: 50px;
position: relative;
}
#top {
top: 0;
left: 0;
position: absolute;
z-index: 1;
}
#center {
top: 60px;
left: 200px;
position: absolute;
z-index: 1;
}
#bottom {
top: 150px;
left: 400px;
position: absolute;
z-index: 0;
}
</style>
</head>
<body>
<div id="pictures">
<img src="bottom-pic.jpg" id="top" class="bottom-pic">
<img src="bottom-pic.jpg" id="center" class="bottom-pic">
<img src="bottom-pic.jpg" id="bottom" class="bottom-pic">
</div>
<input type="button" value="Try" id="launch">
</body>
</html>
当我单击“启动”按钮时,什么也没有发生。
I have three rectangular images overlapped within a diagonal line, the first fully visible on the top-left corner, the second in the center partially hidden by the first and the last in the bottom-right corner partially hidden by the second.
I want the image which is clicked to go on top of the others. I'm trying to achieve this by manipulating z-index with jQuery but it doesn't seem to work. It actually seems like jQuery isn't even recognized.
This is my test code:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js">
$(document).ready(function () {
$('#launch').click(function () {
alert('Ok');
});
$('.bottom-pic').click(function () {
$(this).css('z-index' : '1');
$('.bottom-pic').not(this).css('z-index' : '0');
});
});
</script>
<style type="text/css">
#pictures {
width: 650px;
height: 300px;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
margin-bottom: 50px;
position: relative;
}
#top {
top: 0;
left: 0;
position: absolute;
z-index: 1;
}
#center {
top: 60px;
left: 200px;
position: absolute;
z-index: 1;
}
#bottom {
top: 150px;
left: 400px;
position: absolute;
z-index: 0;
}
</style>
</head>
<body>
<div id="pictures">
<img src="bottom-pic.jpg" id="top" class="bottom-pic">
<img src="bottom-pic.jpg" id="center" class="bottom-pic">
<img src="bottom-pic.jpg" id="bottom" class="bottom-pic">
</div>
<input type="button" value="Try" id="launch">
</body>
</html>
Still when I click the "launch" button nothing even happens.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我刚刚意识到你犯了一个小错误,你使用相同的标签来加载查询和声明你的内联 javascript...你不能这样做。您必须使用两个单独的脚本标签。
注意新的脚本标签:
...
答案的其余部分仍然适用..也就是说,您必须删除 #top、#middle 和 #bottom 中的 z-index ....
Okay i just realized that you made small mistake you are using the SAME tag for both loading query and DECLARING your inline javascript... you can't do that. You have to use two separate script tags.
NOTE the new script tag:
...
The rest of the answer still applies.. Namely you have to get rid of the z-index in #top, #middle, and #bottom....
我建议您从 css 中删除所有 z 索引。然后创建一个名为“.veryHighZ-Index”的类,并将该类添加到单击的图像中。从之前单击的图像中删除该类...
我稍微修改了您的代码,但是这里的代码应该可以工作。
简而言之,将以下代码添加到您的 css 中
,并将此代码添加到 javascript 函数中
I recommend you remove all the z-indexes from your css. Then create a class called '.veryHighZ-Index, for example, and add this class to the clicked image & remove the class from the previously clicked image...
I modified your code a little bit, but this code here should work.
In short, add the following code to your css
and this code to the javascript functions
您遇到了 JavaScript 语法错误。在您的 css 命令中,将 : 替换为 ,
另外,您需要将自定义脚本包装在其自己的 javascript 标签中,而不是 jqueryimport 标签内
You are having a javascript syntax error. In your css commands, replace the : with ,
also, you need to wrap your custom script in its own javascript tag, not inside the jqueryimport tag
按如下方式更改脚本:
这应该可行。
Change you scripts as follows:
That should work.
这里只是一些建议
我显然需要弄清楚如何包含代码示例 - 单击“{}”按钮或添加 4 个空格似乎不起作用!
just some suggestions here
I obviously need to work out how to include code samples - clicking the "{}" button or adding 4 spaces doesn't seem to work!