html - jQuery change folder with css change -
i trying change folders contain images when div changes. using media queries change div on page load. need jquery check size div has been set , change image folder accordingly. example if max-width of div 700px want jquery change string photography/mobile/ photography/fullscreen/ original string photography/fullscreen/stage.jpg not want whole string change, word fullscreen, mobile or tablet. ok css jquery awful learning!
here code jquery not working images not changing when media queries do:
<!doctype html> <html> <head> <title>change image!</title> <style type="text/css"> #surround{position: relative; margin: 0px auto; width: 100%; max-width: 700px; height: 467px; border: 1px solid black;} @media screen , (max-width:800px){ #surround{max-width: 400px; height: 262px; border: 1px solid blue;} } @media screen , (max-width: 500px){ #surround{max-width: 200px; height: 133px; border: 1px solid red;} } </style> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/init.js"></script> </head> <body> <div id="surround"> <image src="photography/fullscreen/stage.jpg" class="picture" /> </div> </body> </html> $(document).ready(function(){ if($("#surround").css("max-width") == "700px"){ $("img").each(function(){ $(this).attr("src", $(this).attr("src")("photography/fullscreen/")); }); } if($("#surround").css("max-width") == "400px"){ $("img").each(function(){ $(this).attr("src", $(this).attr("src")("photography/tablet/")); }); } if($("#surround").css("max-width") == "200px"){ $("img").each(function(){ $(this).attr("src", $(this).attr("src")("photography/mobile/")); }); } });
try - replace subdirectory part of src attribute new target_subdir value. modified encapsulation, caching selected elements, , code re-use (very important things in software development).
function changeimgsrc(target_subdir) { $("img").each(function() { var replacementsrc = $(this).attr("src").replace( /photography\/(\w*)\//, "photography/"+target_subdir+"/" ); $(this).attr("src", replacementsrc); }); } $(document).ready(function() { var $surround = $("#surround"); var $surroundmaxwidth = $surround.css("max-width"); if ($surroundmaxwidth == "700px") { changeimgsrc("fullscreen"); } if ($surroundmaxwidth == "400px") { changeimgsrc("mobile"); } if ($surroundmaxwidth == "200px") { changeimgsrc("tablet"); } });
Comments
Post a Comment