I'm trying to work out a short and simple solution to responsive images (only loading smaller or larger images, depending on screen size) using Javascript's MatchMedia. (Yes, I'm aware this can sorta be done in CSS3 by using background-image, but that's hack-y and not ideal for me).
I've been using the following script for rollover and onClick types of image changes. I like it because (unlike most scripts I've found) it doesn't require a separate function to be written out for every single set of images:
It works with this HTML:
Now, I'm Javascript illiterate, but it seems like if you replaced the onClick HTML with something static (but what?) this script could be dropped into this:
I've been using the following script for rollover and onClick types of image changes. I like it because (unlike most scripts I've found) it doesn't require a separate function to be written out for every single set of images:
Code:
function imageOn(imageName) {
if (document.images) {
currentImage = document.images[imageName];
currentImage.src = imageName + "-version2.jpg";
}
}
It works with this HTML:
Code:
<img name="test-image" src="test-image-version1.jpg" onClick="imageOn('test-image')">
Now, I'm Javascript illiterate, but it seems like if you replaced the onClick HTML with something static (but what?) this script could be dropped into this:
Code:
var screencheck = window.matchMedia("(min-width: 32em)");
if (screencheck.matches) {
/*ImageOn function*/
}
Last edited: