javascript - Using media queries for dynamic image content -


i have seen several examples using media queries dynamically display different images based on screen width.

here jfiddle example found elsewhere: http://jsfiddle.net/ruddy/byh2j/

the image paths in above example hard coded in css

@media screen , (min-width: 1080px) { .site-header .site-branding {     background-image: url("../images/logo_big.png") !important;     background: blue;   } } @media screen , (max-width: 1079px) {     .site-header .site-branding {     background-image: url("../images/logo_med.png") !important;     background: green;   } } 

how search results page required images aren't known until page loads?

found great solution this. instead of using media queries can use jquery's $(window).resize() function , change src attribute based on current window size.

codepen here: http://codepen.io/dustinpage/pen/ytwjb

$(window).load(function() {     //wait images load before displaying them.     //this prevents image flashing.   resizeimage();     $('.image-resize').show(); });  $(window).resize(function() {   //whenever window resized check see if new image needed.   //using code call images in seven7 way optional.   //performance better if don't use ".resize" event.   resizeimage(); });  function resizeimage() {  $('.image-resize').each(function() {     var element = $(this),         src = $(this).attr('src'),         regx = /wid=\d+(\.\d)*/g,         currentwidth,         newwidth,         newsrc;     if ($(window).width() > 1824) {       /* large displays ----------- */      //return large image if screen size on 768px      currentwidth = src.match(regx);      newwidth = 'wid=2000';      newsrc = src.replace(currentwidth, newwidth);      //begin temporary code testing output      textwidth = newwidth.replace('wid=', '');      $(".dsply-screen-size").text($(window).width());      $(".dsply-image-size").text(textwidth);     //end temporary code testing output    }   else if ($(window).width() <= 1824 && $(window).width() > 1366) {       /* desktops ----------- */      //return large image if screen size on 768px      currentwidth = src.match(regx);      newwidth = 'wid=1824';      newsrc = src.replace(currentwidth, newwidth);      //begin temporary code testing output      textwidth = newwidth.replace('wid=', '');      $(".dsply-screen-size").text($(window).width());      $(".dsply-image-size").text(textwidth);     //end temporary code testing output    }    else if ($(window).width() <= 1366 && $(window).width() > 767) {       /* desktops ----------- */      //return large image if screen size on 768px      currentwidth = src.match(regx);      newwidth = 'wid=1024';      newsrc = src.replace(currentwidth, newwidth);      //begin temporary code testing output      textwidth = newwidth.replace('wid=', '');      $(".dsply-screen-size").text($(window).width());      $(".dsply-image-size").text(textwidth);     //end temporary code testing output    }    else if ($(window).width() <= 767 && $(window).width() > 480) {      /* tablets (portrait) ----------- */      //return medium image if screen size between 481-767px      currentwidth = src.match(regx);      newwidth = 'wid=767';      newsrc = src.replace(currentwidth, newwidth);      //begin temporary code testing output      textwidth = newwidth.replace('wid=', '');      $(".dsply-screen-size").text($(window).width());      $(".dsply-image-size").text(textwidth);     //end temporary code testing output    }    else if ($(window).width() <= 480) {      /* smartphones ----------- */      //return small image if screen size less 480      //note: default image state small "else if" technically not needed.      currentwidth = src.match(regx);      newwidth = 'wid=480';      newsrc = src.replace(currentwidth, newwidth);      //begin temporary code testing output      textwidth = newwidth.replace('wid=', '');      $(".dsply-screen-size").text($(window).width());      $(".dsply-image-size").text(textwidth);     //end temporary code testing output    }     element.attr('src', newsrc); });  }; 

credit dustin page great pen.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

python 3.5 - Pyqtgraph string in x tick -