html - How to change the logo depending on page with javascript? -
i trying set logo display bigger logo desktops , smaller image smaller devices smartphones...
this i've tried until now, doesn't works.
can me? what's wrong code?
<html> <head> <script type="text/javascript"> function load(){ document.getelementbyid('logo').src = displaylogo(); } function displaylogo(){ if ($(window).width() < 1024) {// code small viewports return "http://i.imgur.com/ozyv740.png"; // small logo } else {// code large viewports return "http://i.imgur.com/rmv6af0.png"; //big logo } } </script> </head> <body onload="load()"> <img src="http://i.imgur.com/rmv6af0.png" id="logo" name="logo" title="the original logo big logo"/> </body> </html>
because of not use jquery
try in case :
to work in js:
if (window.innerwidth < 1024) {// code small viewports //^^^^^^^^^^^
if want use jquery:
function displaylogo() { if ($(window).width() < 1024) { $("#logo").attr("src","http://i.imgur.com/ozyv740.png"); // small logo } else { $("#logo").attr("src","http://i.imgur.com/rmv6af0.png"); //big logo } } $(window).resize(function () { displaylogo(); }); $(document).ready(function () { displaylogo(); });
i suggest use css
instead:
<span id="logo"></span>
css:
#logo { display: inline-block; width: 50px; height: 50px; background: url(http://i.imgur.com/ozyv740.png) no-repeat; /* small */ } @media screen , (min-width: 1024px) { #logo { width: 100px; height: 100px; background: url(http://i.imgur.com/rmv6af0.png) no-repeat; /* big */ } }
Comments
Post a Comment