iamchubby
iamchubby

Reputation: 15

how do you change img src with javascript depending on browser width?

I would like to change the source of the image element with the id of "front". I have a basic script at the top, but it doesn't work. I was just attempting to come up with something but I'm very bad at JavaScript.

<html>

<head>
<link rel="stylesheet" type="text/css" href="home.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="home.js"></script>
<script type="text/javascript">

    var reswidth=screen.width;

if (reswidth<400){
  var x = document.getElementsById("front");
  x.src="../images/colbysmall.png"
}

</script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<div class="header">
<div class="navigation">
<ul>
  <li><a id="home" href="">Home</a></li>
  <li><a href="">Services</a></li>
  <li><a href="../portfolio/portfolio.html">Portfolio</a></li>
  <li><a href="../contact/contact.html">Contact</a></li>
  <li><a href=""></a></li>
</ul>

</div>
<div class="yellow"></div>
<div class="x"></div>

</div>
</head>



<body>
<div class="website">


<div class="logo"></div>

<img src="../images/ColbyFaceblack.jpg" id="front" width="100%" >
<div class="content">
<div class="menu"></div>
<div class="office"></div>


</div>



<div class="body2">

<img src="../images/simple_dashed_full.png" width="100%">

<div class="weboutline">
    <img src="../images/web_outline.png" width="100%">
</div>
<div class="body2img2">
    <img src="../images/portfolio.png" width="100%">
</div>

<div class="body2img3">
    <img src="../images/blog.png" width="100%">
</div>

</div>
<div class="body3">

<img src="../images/body.png" width="100%">
<img class="touch" src="../images/get_in_touch_beige.png" width="50%">




</div>

<footer>
<img src="../images/footer.png" width="100%">
    <div class="copyright"> copyright COLBY MOFFETT 2015 </div>
    <div class="facebook"></div>
    <div class="instagram"></div>
    <div class="twitter"></div>
</footer>






</body>
</div>

</html>

Upvotes: 0

Views: 3255

Answers (4)

Scott
Scott

Reputation: 5379

Javascript aside, you can do this with just HTML5. Browser support isn't great, but isn't bad, either.

<picture>
   <source media="(min-width: 400px)" srcset="../images/colbysmall.png">
   <img src="../images/ColbyFaceblack.jpg">
</picture>

<p>With placeholders:</p>

<picture>
   <source media="(min-width: 400px)" srcset="http://placehold.it/400?text=colbyfaceblack">
   <img src="http://placehold.it/200?text=colbysmall">
</picture>

Upvotes: 0

Ren
Ren

Reputation: 330

Since you are already using jQuery, the following could be one of the solutions:

$( window ).resize(function() {
    if($( window ).width() < 400) { 
        $("img#front").attr("src","../images/colbysmall.png");
    } else {
        $("img#front").attr("src","../images/ColbyFaceblack.jpg");
    }
});

(PS. Untested code) :)

Upvotes: 0

Theo Orphanos
Theo Orphanos

Reputation: 1447

changing image according to screen resolution:

$(window).resize(function() {
    var scrWidth = $(window).width();
    if(scrWidth < 500){
        $('#myImage').attr('src', 'http://wallpaper-download.net/wallpapers/logo-wallpapers-google-chrome-background-wallpaper-33143.jpg');
    }
    if(scrWidth > 500){
        $('#myImage').attr('src', 'http://www.bhmpics.com/download/google_colorful_background-1920x1080.jpg');
    }
});

(random images from google)

Fiddle here: http://jsfiddle.net/59ehtLde/

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

This is right, but has to be executed on change of screen size:

$(function () {
  $(window).resize(function () {
    var reswidth=screen.width;
    if (reswidth<400){
      var x = document.getElementsById("front");
      x.src="../images/colbysmall.png"
    }
  });
});

Also make sure you execute it inside the $(function () { }); to execute on the DOM loaded contents. So, to make sure it also executes when it gets loaded, you need to store it in a named function and execute it every time when the window is resized.

Your final code will be:

$(function () {
  var reszWindow = function () {
    var reswidth=screen.width;
    if (reswidth<400){
      var x = document.getElementsById("front");
      x.src="../images/colbysmall.png"
    }
  };
  reszWindow();
  $(window).resize(reszWindow);
});

Upvotes: 1

Related Questions