1
<script type="text/javascript">
  $(window).resize(function(){
  var width = $(window).width(); 

   if (width < 361) {
      $(".infograph-image").attr("src","/images/infographicHowMobile.png");
   }
 }); 
</script>

I wish to change the image source of a given image, based on the viewport width size. If the viewport width size is 360 or less, change to a mobile version of the image.

I have two simple questions:

1) How can we do both: a) detect windows resize AND document ready ?

This, I believe I got:

I need to change that into a function. Then call it on load;

checkResponsive();

Then bind a event listener:

   

$(window).resize(checkResponsive);

2) We have several images that need to have a mobile version, should this be converted to a function ? Or several ifs may work ?

Can you please give me a kick-off start on those two questions please.

9
  • did you try $(window).resize() or css media queries? Commented Oct 8, 2012 at 15:16
  • @prashanth: window.resize is that I have on the code sample posted. Media queries, is there a way to change the img src using media queries ? Commented Oct 8, 2012 at 15:18
  • see the answers in stackoverflow.com/q/2676436/441860 Commented Oct 8, 2012 at 15:25
  • I would say use media queries. also if you want you can do something like this also : jquerypicture.com it is a great plugin, just resize the window and you will understand it. I hope it helps\ Commented Oct 8, 2012 at 15:27
  • @prashanth thanks a lot for your answer. However, that will not work. I'm totally aware of background image change using css, but that's not the case here. Commented Oct 8, 2012 at 15:27

1 Answer 1

1

Create a seperate function and call them on both events. So, for example, create the function mobileImg() like this:

function mobileImg(targetClass, imageSrc) {
    var width = window.innerWidth; // No need for jQuery here, raw JS can do this

    if(width < 361) {
        $(targetClass).attr("src", imageSrc);
    }
}

And then call this on both events.

$(document).ready(mobileImg(".infograph-image", "/images/infographicHowMobile.png"));
$(window).resize(mobileImg(".infograph-image", "/images/infographicHowMobile.png"));

You can then call the mobileImg method as much as you want and with whatever params. If you really want to make it clean, also add a check if the passed element and image exist at all and use fallbacks where needed.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not getting this. If I have infographicHowMobile and infographicAboutMobile and infographicOtherImageMobile and infographicYetAnotherImageMobile, I need to call 8 times document.ready and window.resize ? :s
@MEM No, you would stack them in 1 call. Like: $(document).ready(mobileImg(".class1", "img1"); mobileImg(".class2", "img2")); and so on... Or pass targetClass/imageSrc as arrays to the function and loop over all of them.
@Oldskook - thanks a lot, I end up using the following very VERY nice script that does the job the same way I wished to have been done: responsejs.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.