更好的 JQueryish 方式或 CSS 精灵
我有一个包含五张图像的水平菜单。这 5 个图像均处于活动状态和灰色状态。当特定图像处于活动状态时,其余 4 个图像需要变灰。对于其他图像也是如此。
我是用 jquery 做的,但代码也不是那么优化和良好。就像这样
$("document").ready(function(){
$("#imageidone").click(function() {
$("#imageidone").attr("src","/path to image_active");
$("#imageidtwo").attr("src","/path to image_grayed");
$("#imageidthree").attr("src","/path to image_grayed");
$("#imageidfour").attr("src","/path to image_grayed");
$("#imageidfive").attr("src","/path to image_grayed");
});
$("#imageidtwo").click(function() {
$("#imageidone").attr("src","/path to image_grayed");
$("#imageidtwo").attr("src","/path to image_active");
$("#imageidthree").attr("src","/path to image_grayed");
$("#imageidfour").attr("src","/path to image_grayed");
$("#imageidfive").attr("src","/path to image_grayed"); });
and so on for imageidthree, imageidfour, imageidfive
});
我怎样才能使用 CSS sprites 或更紧凑的 jqueryish 方式以更好的方式做到这一点,
谢谢
I have a horizontal menu of five images. All of these 5 images have active and grayed state. When a particular image is active the rest 4 need to be grayed out. Similarly for the other images.
I did it in jquery and that too the code is not so optimized and good. It is like this
$("document").ready(function(){
$("#imageidone").click(function() {
$("#imageidone").attr("src","/path to image_active");
$("#imageidtwo").attr("src","/path to image_grayed");
$("#imageidthree").attr("src","/path to image_grayed");
$("#imageidfour").attr("src","/path to image_grayed");
$("#imageidfive").attr("src","/path to image_grayed");
});
$("#imageidtwo").click(function() {
$("#imageidone").attr("src","/path to image_grayed");
$("#imageidtwo").attr("src","/path to image_active");
$("#imageidthree").attr("src","/path to image_grayed");
$("#imageidfour").attr("src","/path to image_grayed");
$("#imageidfive").attr("src","/path to image_grayed"); });
and so on for imageidthree, imageidfour, imageidfive
});
How can I do it in a better way using CSS sprites or a more compact jqueryish way,
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用循环:
Use a loop:
您可以使用 css sprites 来实现,
制作包含所有状态的大图像并在 css 中创建类,然后使用 jquery 您可以更改类名称。
由于您总是只加载一张图像,因此性能会很好。
You can do it css sprites,
Make a big image with all states and make classes in css and using jquery you can change the class name.
As you always load only one image , performance will be good.