[Pro] How to give SVGs a rollover (hover) color

To better Retina optimize my site, I have added a splattering of SVGs here and there. Some I have made into clickable buttons, such as the gray “DIGI-LINK” icon you see on the right side of the photo shown on the following page in my site:

I’d like to make it change to RED when the user does a mouseover (HOVER). The following discussion makes it seem possible:

I am just not sure what the best way to do it within Freeway is.
Do I add “onclick” markup via the Extended Attributes dialog?

Also, since most people have moved away from IE6 and IE7 these days, I am thinking only of IE8 (to a small extend) and higher when it comes to compatibility. Are there any caveats to adding a hover color to SVGs?

Thanks,

James Wages


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I neglected to mention that I am using the SVGimage action (version 0.2) which has a checkbox for IE8.

–James


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

If you view the HTML for that page in my website, you will see the following code pertains to that particular SVG:

<div id="DLicon"><img src="dlicon.png" border=0 width=57 height=62 alt="" style="float:left" data-svg="DigiLink_Icon.svg" title=""></div>

I am using the SVGimage action, which you can download here:

http://actionsforge.com/actions/svgimage

And once again, my question is how should I implement a HOVER COLOR on this SVG in Freeway?

Thanks,

James Wages


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Hi James,

I know - you like to read a related answer and solution, no matter how expensive.

If you’re after Retina ready, I’d think of “twice the size - keeping the resolution” and check the HighRes option in inspector.

While the SVG is basically very old, its browser support is preserved for modern versions. IE8 is not part of those modern browsers and I don’t think that it will be longer part of the web browser family (5 years old and since died XP off).

From the constructional viewpoint, it only makes sense if it is hosted in a flexible parent container (scalable).

Personally I’d not go the action-way, preferring the “crowbar” native code-savy one. This is just because I don’t trust anything - especially if a fallback is “jpeg”. A .svg file is (for me) 99% outlined stuff which would require gif or png IMO.

This is all I know (in clean words nothing), but we had a discussion about this these days - a the upshot was:

“I think I prefer the png-way”.

Cheers

Thomas


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thomas,

My site shows up just fine in IE8 as it is now, SVGs and all. The SVGimage action provides PNG fallback, which I have found works vey well in many browsers. I only mentioned IE8 and browser compatibility in terms of “the solution” to provide a hover color (rollover color).

And so, I am just curious, after reading that StackOverflow thread, if there is an easy line of code that would work the HOVER magic for me.

–James Wages


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

You can set the conversion to any format you like. There’s no requirement that you use JPEG here, just as in any other image in Freeway.

Walter

On Jul 14, 2014, at 4:54 AM, Thomas Kimmich wrote:

Personally I’d not go the action-way, preferring the “crowbar” native code-savy one. This is just because I don’t trust anything - especially if a fallback is “jpeg”. A .svg file is (for me) 99% outlined stuff which would require gif or png IMO.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

And so, I am just curious, after reading that StackOverflow thread, if there is an easy line of code that would work the HOVER magic for me

I think that you have to embed the SVG to expose the object for changes with JS or CSS to work.

David


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

You can also alter the callback function that the SVG Action uses. Currently, it sets up the object like this:

document.observe('dom:loaded', function(evt){
  if(! navigator.userAgent.toLowerCase().match('msie')){
    $$('img[data-svg]').each(function(elm){
      var svg = new Element('object', {
        type:'image/svg+xml', 
        data: elm.readAttribute('data-svg'), 
        width: elm.readAttribute('width'),
        height: elm.readAttribute('height')
      });
      elm.insert({after: svg});
      svg.insert(elm.remove());
    });
  }
});

If you write your own version of this, you can do anything you want to the svg element right here:

document.observe('dom:loaded', function(evt){
  if(! navigator.userAgent.toLowerCase().match('msie')){
    $$('img[data-svg]').each(function(elm){
      var svg = new Element('object', {
        type:'image/svg+xml', 
        data: elm.readAttribute('data-svg'), 
        width: elm.readAttribute('width'),
        height: elm.readAttribute('height')
      });
      elm.insert({after: svg});
      svg.insert(elm.remove());
      // your code here, use 'svg' as your handle to the object
    });
  }
});

Walter

On Jul 14, 2014, at 11:06 AM, DeltaDave wrote:

And so, I am just curious, after reading that StackOverflow thread, if there is an easy line of code that would work the HOVER magic for me

I think that you have to embed the SVG to expose the object for changes with JS or CSS to work.

David


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I don’t follow.

By opening the SVGimage action in TextWrangler I can see the code, and the code I see does not resemble “document.observe(‘dom:loaded’…” code given above.

Walter, is your code above supposed to be used somewhere on the page INSTEAD of using the SVGimage action?

And to add a red HOVER color, one would need only add something like the following into the section labeled “// your code here”??:

<button onclick=svg.style.fill=“red”;>

–James Wages


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

You’d need to make your own version of the hosted script, which is linked in to your page from my CDN (content delivery network). For starters, what I would do is create your own “fork” of the base Action. Then download the script that the Action links to from svg-shim | FreewayPro CDN

For a test, publish your example page to a subfolder on your server, then upload the script to the same folder. Open the script and the HTML page directly from your server, using Transmit and TextWrangler or BBEdit or whatever editor you have hanging around there. Once you have both files open in your editor, make the following changes:

  1. Change the URL of the script in the HTML page to be just the filename (relative path to that file in the same folder). Now you can save and close the HTML page in your editor – no further changes will be needed there.
  2. In the script, locate the line that I detailed for you. In that spot where I wrote “your code goes here”, add a line like this: console.log(svg);. Make sure that this is on its own line, not in the same line as the // comment flag.
  3. Visit the test page in your browser, and while the Developer tools are open (and the Console tab is selected) reload the page. You should see a new line in the console for each of the SVGs on your page.

Once you’ve done this part, you’ll need to deconstruct the StackOverflow script, and see if you can adapt its code to your needs. Remember, you already have a reference to the SVG element, so you would not need to duplicate any part of the code that uses document.querySelectAll() to get those references. You can delete your console.log() statements at this point, they were just there to prove that you had things working up to that point.

Finally, to deploy this in a simpler and non-destructive manner, you will need to post your modified script on the CDN, and create your own version of the Action that points to the modified script rather than the original. If you sign up for an account on CDNAdmin, and send me a note off-list, I will promote you to “author” and let you add your own scripts there.

Walter

On Jul 15, 2014, at 2:07 AM, JDW wrote:

I don’t follow.

By opening the SVGimage action in TextWrangler I can see the code, and the code I see does not resemble “document.observe(‘dom:loaded’…” code given above.

Walter, is your code above supposed to be used somewhere on the page INSTEAD of using the SVGimage action?

And to add a red HOVER color, one would need only add something like the following into the section labeled “// your code here”??:

<button onclick=svg.style.fill=“red”;>

–James Wages


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walter, thank you.

I followed your previous post precisely. And here is what I have now in TextWrangler (linked to ‘svg-shim.js’ which resides in the same directory that is home to my HTML, CSS and graphics files):

document.observe('dom:loaded', function(evt){
  if(! navigator.userAgent.toLowerCase().match('msie')){
    $$('img[data-svg]').each(function(elm){
      var svg = new Element('object', {
        type:'image/svg+xml', 
        data: elm.readAttribute('data-svg'), 
        width: elm.readAttribute('width'),
        height: elm.readAttribute('height')
      });
      elm.insert({after: svg});
      svg.insert(elm.remove());
      // new code here, use 'svg' as the handle to the object
      var s = document.getElementById("DigiLink_Icon.svg");
      s.style.fill = "FF0000";  
    });
  }
});

But Console (in CHROME) coughs up the following error:

Uncaught TypeError: Cannot read property 'style' of null 

With regard to getElementById, I’ve tried changing double quotes to single, eliminating quotes, as well as using other references like ‘svg’ and ‘svg_img’ and ‘data-svg’. But I still get the same error.

Within the HTML, within the tag, we have the following DIV reference to the SVG:

<div id="DLicon"><img src="dlicon.png" border=0 width=57 height=62 alt="" style="float:left" data-svg="DigiLink_Icon.svg">
</div>

However, when I used the DIV’s ID, the error vanishes, but there is no Red HOVER color on mouseover:

var s = document.getElementById("DLicon");

Here’s my test page:

http://visionsecurity.jp/svgtest/


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Still scratching my head on this.

I tried adding an ID to the HTML:

<div id="DLicon"><img src="dlicon.png" border=0 width=57 height=62 alt="" style="float:left" data-svg="DigiLink_Icon.svg" id="ttt">
</div>

And then I changed the appropriate line in the svg-shim.js file accordingly:

var s = document.getElementById("ttt");

No error on the browser Console, but no red Hover color on mouseover either.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

On Jul 16, 2014, at 1:33 AM, JDW wrote:

Walter, thank you.

I followed your previous post precisely. And here is what I have now in TextWrangler (linked to ‘svg-shim.js’ which resides in the same directory that is home to my HTML, CSS and graphics files):

[code]
document.observe(‘dom:loaded’, function(evt){
if(! navigator.userAgent.toLowerCase().match(‘msie’)){
$$(‘img[data-svg]’).each(function(elm){
var svg = new Element(‘object’, {
type:‘image/svg+xml’,
data: elm.readAttribute(‘data-svg’),
width: elm.readAttribute(‘width’),
height: elm.readAttribute(‘height’)
});
elm.insert({after: svg});
svg.insert(elm.remove());
// new code here, use ‘svg’ as the handle to the object

At this point in the code, you do not need to re-acquire a reference to the svg. You already have it, because the script just made it. So the next line is irrelevant and should be removed. Also, getElementById requires that you pass it an actual ID, you have passed the filename, and the function will not work in this case.

The SVG element is being dynamically created inside the code block we are currently within, and the “handle” to that SVG is the local variable svg. This generated SVG element does not have an ID at all.

Critically important thing to understand: that variable is not available in the global context (that’s what a local variable is – i.e.: not global). Outside of the loop that begins on this line: $$('img[data-svg]').each(function(elm){, that variable does not exist, so you have to do all of the following while your variable is available.

 var s = document.getElementById("DigiLink_Icon.svg");

Change s in the next line to svg (see the variable we created two lines above?) and you should get a red background. But critically, this item will always have a read background if you do this.

 s.style.fill = "FF0000";  

If you want the item to respond to a mouseover with a red background, then you will need to attach a handler function to the SVG to respond to that event. Actually, you’ll need to attach two – one to handle the mouseover and one to handle the mouseout (to restore the color).

svg.observe('mouseover', function(evt){
	this.style.fill = 'f00';
});
svg.observe('mouseout', function(evt){
	this.style.fill = 'fff';
});

You would insert these functions directly in place of the line svg.style.fill = "#ff0000".

});
}
});
[/code]

But Console (in CHROME) coughs up the following error:

Uncaught TypeError: Cannot read property ‘style’ of null

Yup, this is entirely expected, because you don’t actually have a reference to the item at this point.

With regard to getElementById, I’ve tried changing double quotes to single, eliminating quotes, as well as using other references like ‘svg’ and ‘svg_img’ and ‘data-svg’. But I still get the same error.

See above. You are trying to get a reference to an item that is being dynamically created, and does not exist outside of the tight loop where that creation is occurring.

Within the HTML, within the tag, we have the following DIV reference to the SVG:

<div id="DLicon"><img src="dlicon.png" border=0 width=57 height=62 alt="" style="float:left" data-svg="DigiLink_Icon.svg">
> </div>

However, when I used the DIV’s ID, the error vanishes, but there is no Red HOVER color on mouseover:

var s = document.getElementById(“DLicon”);

Here’s my test page:

SVG Test

You are very close here, push forward just a little further, and I will be mailing you a propeller-beanie very soon!

Walter


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Hi Walter,

I’ve not had the time to review your previous post until today.

You suggested I change variable “s” to “svg” and then you said that such would cause the SVG to become red all the time. But in fact, it never becomes red at all when I use the following code:

      // new code here, use 'svg' as the handle to the object
      var svg = document.getElementById("DigiLink_Icon.svg");
      svg.style.fill = "FF0000";  

Using the above code results in the following error in Chrome’s Console:

Uncaught TypeError: Cannot read property 'style' of null 

And even if I swap out the “svg.style.fill” line with the following code, I still get the “null” error:

      // new code here, use 'svg' as the handle to the object
      var svg = document.getElementById("DigiLink_Icon.svg");
      svg.observe('mouseover', function(evt){
        this.style.fill = 'f00';
        });
      svg.observe('mouseout', function(evt){
        this.style.fill = 'fff';
        }); 

So it seems obvious that the “DigiLink_Icon.svg” reference within getElementById is problematic. But I’ve tried “svg” and other variations within those parenthesis, but to no avail.

Does this mean I need to getElement using something other than ID?

Thanks,

James Wages


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

The svg is being dynamically created by the script, and it does not have an id. As such, a) you don’t need to look for it by id, and b) you already have a reference to it at that line of the script. Remove the whole getElementById bit entirely. It’s not needed and it doesn’t work. Instead, substitute the variable ‘svg’, which already exists, and already points to the SVG.

Walter

On Jul 22, 2014, at 6:55 PM, “JDW” email@hidden wrote:

Hi Walter,

I’ve not had the time to review your previous post until today.

You suggested I change variable “s” to “svg” and then you said that such would cause the SVG to become red all the time. But in fact, it never becomes red at all when I use the following code:

     // new code here, use 'svg' as the handle to the object
>      var svg = document.getElementById("DigiLink_Icon.svg");
>      svg.style.fill = "FF0000";  

Using the above code results in the following error in Chrome’s Console:

Uncaught TypeError: Cannot read property 'style' of null 

And even if I swap out the “svg.style.fill” line with the following code, I still get the “null” error:

     // new code here, use 'svg' as the handle to the object
>      var svg = document.getElementById("DigiLink_Icon.svg");
>      svg.observe('mouseover', function(evt){
>        this.style.fill = 'f00';
>        });
>      svg.observe('mouseout', function(evt){
>        this.style.fill = 'fff';
>        }); 

So it seems obvious that the “DigiLink_Icon.svg” reference within getElementById is problematic. But I’ve tried “svg” and other variations within those parenthesis, but to no avail.

Does this mean I need to getElement using something other than ID?

Thanks,

James Wages


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walter, I had tried eliminating that entire “getElementById” line before, but when I do that, nothing happens.

More specifically, when I use the following code, nothing ever becomes red:

document.observe('dom:loaded', function(evt){
  if(! navigator.userAgent.toLowerCase().match('msie')){
    $$('img[data-svg]').each(function(elm){
      var svg = new Element('object', {
        type:'image/svg+xml', 
        data: elm.readAttribute('data-svg'), 
        width: elm.readAttribute('width'),
        height: elm.readAttribute('height')
      });
      elm.insert({after: svg});
      svg.insert(elm.remove());
      // new code here, use 'svg' as the handle to the object
      svg.style.fill = "FF0000";
    });
  }
});

And when I use the following alternate code, still nothing becomes red, even when I mouse-over the SVG:

document.observe('dom:loaded', function(evt){
  if(! navigator.userAgent.toLowerCase().match('msie')){
    $$('img[data-svg]').each(function(elm){
      var svg = new Element('object', {
        type:'image/svg+xml', 
        data: elm.readAttribute('data-svg'), 
        width: elm.readAttribute('width'),
        height: elm.readAttribute('height')
      });
      elm.insert({after: svg});
      svg.insert(elm.remove());
      // new code here, use 'svg' as the handle to the object
      svg.observe('mouseover', function(evt){
        this.style.fill = 'f00';
        });
      svg.observe('mouseout', function(evt){
        this.style.fill = 'fff';
        }); 
    });
  }
});

In other words, the only thing that deleting the “getElementById” line does is eliminate the error message in Chome’s Console. Eliminating that line does not magically kick start the RED. As such, there is clearly something else at work that is preventing the SVG from being colored.

Have a look yourself at my test page here:

http://visionsecurity.jp/svgtest/

The only way I know to color it red (and permanently so) is to hack the code of the SVG itself in TextWrangler, replacing all the “FILL” references with “FF0000”. If I could accomplish such a hack dynamically with JavaScript, then such would be a solution for my mouseovers. But the solution you and I have been discussing is simply not working for reasons unknown to me.

–James Wages


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

(•_•)


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

bump


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

After 2.5 years, there’s no better time than the present to reopen an old thread, especially one that was left with no answer at all!

As everyone can read in my earlier post (stamped “23 Jul 2014, 12:16 pm”), a code-hack of “svg-shim.js” does NOT work to make SVG’s change color on mouseover.

So you folks don’t need to re-read this entire thread, let me just say that I am trying to achieve 2 things:

  1. Put a clickable link on an SVG.
  2. Change the color of the SVG on mouseover (i.e., a rollover).

That’s it. SOOOOO SIMPLE. Yet so stinking hard to get to work.

Yesterday I spent all afternoon exploring what might be possible using CSS, rather than hacking that"svg-shim.js" . I found the following site a good read:

https://svgontheweb.com

Since Freeway’s SVGimage action uses the tag when Freeway generates the page HTML, I focused on how to manipulate that.

Here’s what I did:

(1) New document.

(2) Sketch a graphics box and import a small SVG.

(3) Apply the SVGimage action to that box.

(4) Preview in browser. (Of course, there’s no link and no mouseover/rollover yet.)

(5) I used TextWrangler to edit the Freeway-generated “index.html” file, specifically the tag to add the HREF link, as follows:

<div id="DigiLinkIcon">
<a href="https://kiramek.com" style="display: block;">
<object style="pointer-events: none;" type="image/svg+xml" data="DigiLink_Icon.svg">
</a>
<img src="digilinkicon.png" width="57" height="62" alt="DigiLinkIcon" style="float:left"/></object></div>

(6) I then changed “sheet1.css” to add these 2 lines:

.path1:link { fill: #646464; }
.path1:hover { fill: #ff0000; }

(7) And then I edited the SVG code to add a “class” tag within every “path” tag, like this:

<g>
	<path class="path1" fill="#646464"...

(8) I then previewed my handiwork in Safari. The clickable link is now working, but still no mouseover/rollover SVG color-change! I’ve read in various places that the SVG tag “cannot be manipulated” and that would appear to be the case, seeing my code-hack of “svg-shim.js” (despite Walter’s help) and now my CSS trick hasn’t worked either.

So unless one of you kind people can see a flaw in my CSS implementation, then it is settled – cannot be manipulated.

I know that I can get the job done when the SVG is placed inline inside the HTML code. But the problem with that is the SVG is not cached, and I intend to use the same SVG on many pages, making caching more important.

Since this discussion can be applying to any web design tool, not just the now-defunct Freeway, I think it is good to discuss and find a solution. I would therefore appreciate hearing your thoughts and advice. (Surely I cannot be the only one among us who uses SVG and has tried to add links and mouseover color-changes on them!)

Thanks,

James Wages


freewaytalk mailing list
email@hidden
Update your subscriptions at:

I suspect you may want to only apply the color in the class, not inline. Inline trumps the class, so your rollover isn’t getting to shine through. Just for fun, I tried making a transparent SVG (surprisingly hard, Illustrator wrote ten times too much code that I ended up deleting), and put it inside an tag. With the following styles, I was able to change the background of the entire SVG very easily, because the background of the a shone through it.

a { display: inline-block }
a:hover { background-color: #ccc }

<a href="#"><img src="logo.svg" /></a>

Walter

On Jan 31, 2017, at 8:13 PM, JDW email@hidden wrote:

After 2.5 years, there’s no better time than the present to reopen an old thread, especially one that was left with no answer at all!

snip I then changed “sheet1.css” to add these 2 lines:

.path1:link { fill: #646464; }
> .path1:hover { fill: #ff0000; }

(7) And then I edited the SVG code to add a “class” tag within every “path” tag, like this:

<g>
> 	<path class="path1"    ---> delete this part right here ---> fill="#646464"...

freewaytalk mailing list
email@hidden
Update your subscriptions at:
https://freewaytalk.softpress.com/person/options