简体   繁体   English

使用 javascript/jQuery 实现多路切换的最佳方法是什么?

[英]What is the best way to implement multiway toggle using javascript/jQuery?

I have a div that can display 3 images (in the background) each indicating the 'state' of some variable: ie, partial, full and none.我有一个 div 可以显示 3 个图像(在背景中),每个图像都指示某个变量的“状态”:即部分、完整和无。 For each of these states I have images: partial.gif, full.gif and none.gif (ie, these are background images of that div)对于这些状态中的每一个,我都有图像:partial.gif、full.gif 和 none.gif(即,这些是该 div 的背景图像)

Need: Circular queue like toggling effect for changing the images in this order partial -> full -> none -> partial需要:类似于切换效果的循环队列,用于按此顺序更改图像部分 -> 完整 -> 无 -> 部分

So if the current image is 'partial.gif' and the user clicks the div the background image changes to the next one in the sequence ie, full.gif (and if it is currently full.gif it changes to none.gif and that to partial.gif and so on).因此,如果当前图像是“partial.gif”并且用户单击 div,则背景图像将更改为序列中的下一个,即 full.gif(如果它当前是 full.gif,它会更改为 none.gif 并且到 partial.gif 等等)。

Naive solution: have a bunch of if/else's or switch-case and check the current one (image) and then decide based on array look up which is the next one.天真的解决方案:拥有一堆 if/else 或 switch-case 并检查当前一个(图像),然后根据数组查找决定下一个。 Is this the best way of doing it?这是最好的方法吗? Can I leverage jQuery's toggle function somehow?我可以以某种方式利用 jQuery 的切换 function 吗?

(PS: It need not be restricted to images, but could also be for different background color sequences etc., I'd like to know what it is a good 'generic' way of doing it ie, The example may be specific for background images but if I changed part of that code for background-color or font it should still work. I don't want it to be purely generic, but just enough so it is easy to modify for other attributes. If not, that's fine too. Just a thought:) (PS:它不必限于图像,但也可以用于不同的背景颜色序列等,我想知道这是一种很好的“通用”方式,即,该示例可能特定于背景图像,但如果我更改了背景颜色或字体的部分代码,它仍然可以工作。我不希望它纯粹是通用的,但足够了,因此很容易修改其他属性。如果没有,那也很好。 只是一个想法:)

http://api.jquery.com/toggle-event/ http://api.jquery.com/toggle-event/

To be precise http://api.jquery.com/toggle-event/#example-0 does exactly what you wanted...准确地说http://api.jquery.com/toggle-event/#example-0完全符合您的要求...

$("#div1").toggle(
  function() {
    $(this).css("background-image","url(full.png)")
  },   
  function() {
    $(this).css("background-image","url()")
  },   
  function() {
    $(this).css("background-image","url(partial.png)")
  }
});

UPDATE fn.toggle was removed from jQuery UPDATE fn.toggle 已从 jQuery 中删除

Here are relevant posts以下是相关帖子

As long as it's a CSS-based solution (where you can just switch classes), you could do something like this (untested code):只要它是基于 CSS 的解决方案(您可以在其中切换类),您就可以执行以下操作(未经测试的代码):

$('#element').click(function() {
  // get current css class value.
  var class = $(this).attr('class');

  // determine/increment number.
  var nextNumber = parseInt(class.charAt(class.length - 1)) + 1;

  // if too high, reset to first one.
  if (nextNumber > 3) {
    nextNumber = 1;
  }

  // remove old class and add new class.
  $(this).removeClass(class).addClass('my_class' + nextNumber);
});

Assumption being made here that you only have one CSS class applied to the element at a time.这里假设您一次只有一个 CSS class 应用于元素。 But if that's not the case, I'm sure you can find a workaround/tweak for this.但如果不是这种情况,我相信您可以找到解决方法/调整此问题。

And this is just generic enough where you can swap out your CSS class definitions without impacting the script functionality.这足够通用,您可以在不影响脚本功能的情况下更换 CSS class 定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在javascript中切换点击事件的最佳方法是什么? - What is the best way to toggle a click event in javascript? AJAX Cart - 在Javascript / jQuery中实现的最佳方式 - AJAX Cart - Best way to implement in Javascript/jQuery 使用JavaScript或Jquery在浏览器中存储本地值的最佳方法是什么? - what is the best way to store local values in browser using Javascript or Jquery? <a>使用 JavaScript/jQuery 获取标签的绝对链接的最佳方法是什么?</a> - What is the best way to get the absolute link of an <a> tag using JavaScript/jQuery? 使用javascript / jquery,更新设置图像的src的最佳方法是什么? - Using javascript/jquery, what is the best way to update the src of a set images? 使用javascript或jquery用GET参数组成链接的最佳方法是什么 - What is the best way to compose a link with GET parameters using javascript or jquery 在 JavaScript 中简化对象的最佳方法是什么(最好使用 Jquery) - What is the best way to simplify Objects in JavaScript (perferably using Jquery) 使用Prototype或jQuery实现.lastChild的最佳方法 - Best way to implement .lastChild using Prototype or jQuery 使用JavaScript实现状态检查的最佳方法 - Best way to implement status check using javascript (使用jQuery)实现增加和减小页面字体大小的按钮的最佳方法是什么? - What's the best way (using jQuery) to implement buttons that increase and decrease the font-size of the page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM