javascript - How can I avoid flickering when image is changed for another with MouseEvent? -


i have clickable image (logo) want defined in html (not css) since it's logo sits in menu, should scaled text , semantic part of content.

i want change on hover so:

<img src="logo.svg"     onmouseover="this.src='logo_hover.svg'"     onmouseout="this.src='logo.svg'"> 

the problem menu , logo flickers when hover image. doesn't happen seems have takes blink new image scaled right. have width: 100% on logo , width: 5em set on div holds it.

how can avoid flickering? should set both width , height on image, if how find out height image should have? or there other way avoid browser being "surprised" when new image loaded?


what i've triend far:

i've tried preload image javascript, , <link rel='prefetch'> tag, flickering still happens.

based on looks you're trying do, advise against approach can manipulate properties of svg using css hover state.

what changing on logo - if may ask?

if case of colours etc use approach of using inline svg code directly html , using css apply colours etc. enables edit css properties hover state. problem using svgs via img tag can't edit properties — sure aware of.

for istance - svg logo:

<svg version="1.1" id="my-logo" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"      viewbox="0 0 140.9 36.1" xml:space="preserve"> <g>     <path d="m0,35.8v-35h7.8v28h14.9v7h0z"/>     <path d="m56.9,30.9c-3.5,3.5-7.9,5.2-13.1,5.2c-5.2,0-9.5-1.7-13-5.2c-3.5-3.4-5.3-7.7-5.3-12.9c0-5.1,1.8-9.4,5.3-12.9         c3.5-3.4,7.9-5.2,13-5.2c5.2,0,9.5,1.7,13.1,5.2c3.5,3.5,5.3,7.7,5.3,12.9c62.1,23.2,60.4,27.5,56.9,30.9z m54.2,18.1         c0-3.1-1-5.8-3-8c-2-2.2-4.5-3.3-7.4-3.3c-2.9,0-5.4,1.1-7.4,3.3c-2,2.2-3,4.9-3,8c0,3.1,1,5.8,3,7.9c2,2.2,4.5,3.3,7.4,3.3         c2.9,0,5.4-1.1,7.4-3.3c53.2,23.8,54.2,21.2,54.2,18.1z"/>     <path d="m91.4,18h7.9v12.4c-3.5,3.9-8.3,5.8-14.4,5.8c-5.1,0-9.4-1.7-12.9-5.1c-3.5-3.4-5.2-7.7-5.2-12.9c0-5.2,1.8-9.5,5.3-13         s79.9,0,84.9,0c5,0,9.4,1.6,13.1,5l-4,5.8c-1.6-1.4-3-2.3-4.3-2.8c-1.3-0.5-2.7-0.8-4.3-0.8c-3,0-5.5,1-7.5,3c-2,2-3,4.7-3,8         c0,3.3,1,5.9,2.9,7.9c2,2,4.3,3,7,3s5-0.5,6.8-1.6v18z"/>     <path d="m135.6,30.9c-3.5,3.5-7.9,5.2-13.1,5.2c-5.2,0-9.5-1.7-13-5.2c-3.5-3.4-5.3-7.7-5.3-12.9c0-5.1,1.8-9.4,5.3-12.9         c3.5-3.4,7.9-5.2,13-5.2c5.2,0,9.5,1.7,13.1,5.2c3.5,3.5,5.3,7.7,5.3,12.9c140.9,23.2,139.2,27.5,135.6,30.9z m133,18.1         c0-3.1-1-5.8-3-8c-2-2.2-4.5-3.3-7.4-3.3c-2.9,0-5.4,1.1-7.4,3.3c-2,2.2-3,4.9-3,8c0,3.1,1,5.8,3,7.9c2,2.2,4.5,3.3,7.4,3.3         c2.9,0,5.4-1.1,7.4-3.3c132,23.8,133,21.2,133,18.1z"/> </g> </svg> 

css hover color change:

#my-logo{   fill: orange; }  #my-logo:hover{   fill: red; } 

you have far more flexibility if want edit / manipulate svg properties method can avoid doing on convoluted javascript magician shazaam.

live example


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -