Note that there are some explanatory texts on larger screens.

plurals
  1. POModify jQuery javascript to trigger on :checked instead of :hover — image swap
    primarykey
    data
    text
    <p>I have a great piece of jQuery that swaps an image when the user hovers on an image. Check out the code below to see it.</p> <p>It uses <code>mouseenter</code> and <code>mouseleave</code>.</p> <p>I would like to modify it so that the image swap triggers when a specific input/label is <code>:checked</code>.</p> <p>You can see my demo page here:</p> <p><a href="http://test.davewhitley.com/not-wp/static_folder/index.php" rel="nofollow">http://test.davewhitley.com/not-wp/static_folder/index.php</a></p> <p>To give you some background, I have 4 inputs/labels. When one is checked, a group of images will be swapped from black and white to their color version. I have two files for every image (image_s.jpg and image_o.jpg).</p> <p>I am using the <code>:checked</code> pseudo class to do some filtering. When an input is checked, some images remain full <code>opacity</code> and other reduce to <code>opacity:0.1</code></p> <p>I want only the images that remain full opacity after checking the input to be color.</p> <p>So basically I want to say in javascript: Whenever an input is <code>:checked</code> within <code>#container</code> swap <code>_s.jpg</code> with <code>_o.jpg</code>.</p> <p>Any help would be great.</p> <p><strong>UPDATE 1:</strong> <em>To clarify:</em> There is no image swap happening in the demo. The <code>opacity</code> is just changed when an input is checked. In addition to the opacity change, I would like an image swap. All of the images would be black and white by default, and when an input is selected, the selected images would change from black and white to color (by using an image swap).</p> <p><strong>UPDATE 2:</strong> To put it simply, I would like all of the images to be black and white until the user clicks on one of the filter labels (print, web, typefaces, etc.) When a label is clicked, the non-filtered images will lower in opacity and the filtered images that remain full opacity will swap to a color image.</p> <p><strong>UPDATE 3:</strong> I can't seem to get the below answers to work for me. I am willing to abandon the input/:checked/pseudo-class filtering technique if it gets the job done. Also, I working test would help me a lot (JSfiddle).</p> <p>Here is the image swap javascript:</p> <pre><code> $('.imgover').each(function() { var std = $(this).attr("src"); var hover = std.replace("_s", "_o"); $(this).clone().insertAfter(this).attr('src', hover).removeClass('imgover').siblings().css({ position:'absolute' }); $(this).mouseenter(function() { $(this).stop().fadeTo(200, 0); }).mouseleave(function() { $(this).stop().fadeTo(200, 1); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload