{"id":314,"date":"2011-07-14T07:18:38","date_gmt":"2011-07-14T07:18:38","guid":{"rendered":"http:\/\/tech.avant.net\/q\/?p=314"},"modified":"2012-12-25T22:39:37","modified_gmt":"2012-12-25T22:39:37","slug":"css-opacity","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/css-opacity\/","title":{"rendered":"css opacity in javascript"},"content":{"rendered":"<p>I want register JavaScript events to toggle CSS opacity on selectable images.<\/p>\n<p>For example, given a <em>div<\/em> with a list of images like the following,<\/p>\n<pre class=\"sh_html\">\r\n&lt;div id=&quot;foo_images&quot;&gt;\r\n   &lt;img src=&quot;foo.jpg&quot; \/&gt;\r\n   &lt;img src=&quot;bar.jpg&quot; \/&gt;\r\n   &lt;img src=&quot;baz.jpg&quot; \/&gt;\r\n   ...\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>I would like these images to be 50% transparent but 80% visible during a mouseover. I would also like these images to be selectable, i.e., an image should become fully visible after clicking it.<\/p>\n<p>I would like to do this in JavaScript without having to modify the html image list.<\/p>\n<p>We could add transparency in the stylesheet, as well as the mouseover effect, e.g.,<\/p>\n<pre class=\"sh_css\">\r\n#foo_images img {\r\n   filter: alpha(opacity=50);\r\n   opacity: 0.5;\r\n}\r\n#foo_images img:hover {\r\n   filter: alpha(opacity=80);\r\n   opacity: 0.8;\r\n}\r\n<\/pre>\n<p>In practice, it is highly recommended to keep all presentation elements in the stylesheet, but for the sake of example we can adjust an objects transparency using JavaScript, e.g.,<\/p>\n<pre class=\"sh_javascript\">\r\n  \/\/set obj transparency to 50%\r\n  obj.style.opacity = 0.5; \/\/non-IE browsers\r\n  obj.style.filter = 'alpha(opacity=50)'; \/\/IE\r\n<\/pre>\n<p>Using this approach, we can register events with JavaScript so that the transparency will change as the cursor hovers over, e.g.,<\/p>\n<pre class=\"sh_javascript\">\r\nobj.onmouseover = function() {\r\n  obj.style.opacity = 0.8;\r\n  obj.style.filter = 'alpha(opacity=80)';\r\n}\r\nobj.onmouseout = function() {\r\n  obj.style.opacity = 0.5;\r\n  obj.style.filter = 'alpha(opacity=50)';\r\n}\r\n<\/pre>\n<p>This would obviously be a lot of repeated code so we can use JavaScript closures, e.g.,<\/p>\n<pre class=\"sh_javascript\">\r\nfunction opacityC(obj, value) {return function() {\r\n  obj.style.opacity = value\/100;\r\n  obj.style.filter = 'alpha(opacity=' + value + ')';\r\n}}\r\nobj.onmouseover = opacityC(obj,80);\r\nobj.onmouseout = opacityC(obj,50);\r\n<\/pre>\n<p>Next, to make the images selectable we can assign <em>onclick<\/em> events. We&#8217;ll need to toggle between selected and not-selected, so we&#8217;ll dynamically add a <em>selected<\/em> attribute to the image object. We can use the same closure approach to assign a function to the <em>onclick<\/em> event, e.g.,<\/p>\n<pre class=\"sh_javascript\">\r\nfunction toggleSelectC(obj) {return function() {\r\n  if (obj.selected != 'selected') {\r\n    opacityC(obj, 100)();\r\n    obj.onmouseout = opacityC(obj, 100);\r\n    obj.style.border = '1px solid black';\r\n    obj.selected = 'selected';\r\n  } else {\r\n    obj.onmouseout = opacityC(obj, 50);\r\n    obj.style.border = '1px solid white';\r\n    obj.selected = '';\r\n  }\r\n}}\r\n\r\nimage1.onclick = toggleSelectC(image1);\r\nimage2.onclick = toggleSelectC(image2);\r\n<\/pre>\n<p>Finally, we will loop through all of the images in the <em>div<\/em>, set the initial transparency and add event handlers, e.g.,<\/p>\n<pre class=\"sh_javascript\">\r\n  var obj = document.getElementById('foo_images');\r\n  var images = obj.getElementsByTagName(&quot;img&quot;);\r\n  for (var i = 0; i &lt; images.length; i++) {\r\n    var img = images[i];\r\n    opacityC(img,50)();\r\n    img.onmouseover = opacityC(img, 80);\r\n    img.onmouseout = opacityC(img, 50);\r\n    img.onclick = toggleSelectC(img);\r\n  }\r\n<\/pre>\n<p>Putting this all together, you would have the following HTML and JavaScript:<\/p>\n<pre class=\"sh_javascript\">\r\n&lt;div id=&quot;foo_images&quot;&gt;\r\n   &lt;img src=&quot;foo.jpg&quot; \/&gt;\r\n   &lt;img src=&quot;bar.jpg&quot; \/&gt;\r\n   &lt;img src=&quot;baz.jpg&quot; \/&gt;\r\n&lt;\/div&gt;\r\n&lt;script type=&quot;text\/javascript&quot; defer&gt;&lt;!--\r\n\r\n\/**\r\n * closure to adjust an objects transparency\r\n *\/\r\nfunction opacityC(obj, value) {return function() {\r\n  obj.style.opacity = value\/100;\r\n  obj.style.filter = 'alpha(opacity=' + value + ')';\r\n}}\r\n\r\n\/**\r\n * closure to toggle selected\/non-selected style\r\n *\/\r\nfunction toggleSelectC(obj) {return function() {\r\n  if (obj.selected != 'selected') {\r\n    opacityC(obj, 100)();\r\n    obj.onmouseout = opacityC(obj, 100);\r\n    obj.style.border = '1px solid black';\r\n    obj.selected = 'selected';\r\n  } else {\r\n    opacityC(obj, 50)();\r\n    obj.onmouseout = opacityC(obj, 50);\r\n    obj.style.border = '1px solid white';\r\n    obj.selected = '';\r\n  }\r\n}}\r\n\r\n\/**\r\n * register all selectable images\r\n *\/\r\nfunction registerSelectableImages(obj) {\r\n  var images = obj.getElementsByTagName(&quot;img&quot;);\r\n  for (var i = 0; i &lt; images.length; i++) {\r\n    var img = images[i];\r\n    opacityC(img,50)();\r\n    img.style.border = '1px solid white';\r\n    img.onmouseover = opacityC(img, 80);\r\n    img.onmouseout = opacityC(img, 50);\r\n    img.onclick = toggleSelectC(img);\r\n  }\r\n}\r\nregisterSelectableImages(document.getElementById('foo_images'));\r\n--&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>Applying the above code to the three sample images will produce the following results:<\/p>\n<div id=\"foo_images\">\n<img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/foo-150x150.jpg\" alt=\"\" title=\"foo\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-321\" srcset=\"https:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/foo-150x150.jpg 150w, https:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/foo-300x300.jpg 300w, https:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/foo.jpg 450w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/bar-150x150.jpg\" alt=\"\" title=\"bar\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-319\" srcset=\"https:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/bar-150x150.jpg 150w, https:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/bar-300x300.jpg 300w, https:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/bar.jpg 450w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/baz-150x150.jpg\" alt=\"\" title=\"baz\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-320\" srcset=\"https:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/baz-150x150.jpg 150w, https:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/baz-300x300.jpg 300w, https:\/\/tech.avant.net\/q\/wp-content\/uploads\/2011\/07\/baz.jpg 450w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/>\n<\/div>\n<p><br style=\"clear: both;\" \/><\/p>\n<p><script type=\"text\/javascript\" defer><!--\nfunction opacityC(obj, value) {return function() {\n  obj.style.opacity = value\/100;\n  obj.style.filter = 'alpha(opacity=' + value + ')';\n}}\nfunction toggleSelectC(obj) {return function() {\n  if (obj.selected != 'selected') {\n    opacityC(obj, 100)();\n    obj.onmouseout = opacityC(obj, 100);\n    obj.style.border = '1px solid black';\n    obj.selected = 'selected';\n  } else {\n    opacityC(obj, 50)();\n    obj.onmouseout = opacityC(obj, 50);\n    obj.style.border = '1px solid white';\n    obj.selected = '';\n  }\n}}\nfunction registerSelectableImages(obj) {\n  var images = obj.getElementsByTagName(\"img\");\n  for (var i = 0; i < images.length; i++) {\n    var img = images[i];\n    opacityC(img,50)();\n    img.style.border = '1px solid white';\n    img.onmouseover = opacityC(img, 80);\n    img.onmouseout = opacityC(img, 50);\n    img.onclick = toggleSelectC(img);\n  }\n}\nregisterSelectableImages(document.getElementById('foo_images'));\n--><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I want register JavaScript events to toggle CSS opacity on selectable images. For example, given a div with a list of images like the following, &lt;div id=&quot;foo_images&quot;&gt; &lt;img src=&quot;foo.jpg&quot; \/&gt; &lt;img src=&quot;bar.jpg&quot; \/&gt; &lt;img src=&quot;baz.jpg&quot; \/&gt; &#8230; &lt;\/div&gt; I would like these images to be 50% transparent but 80% visible during a mouseover. I would [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,11,9],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/314"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=314"}],"version-history":[{"count":10,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/314\/revisions"}],"predecessor-version":[{"id":719,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/314\/revisions\/719"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}