Last active
December 5, 2016 14:17
-
-
Save Polyducks/dcc4b270f0f458bf261101e8bfac6103 to your computer and use it in GitHub Desktop.
Revisions
-
Polyducks revised this gist
Dec 5, 2016 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,6 @@ <img data-mob-src="mobile.png" data-desk-src="desktop.png" /> <script> /* RESPONSIVE-IMAGES @@ -33,7 +32,7 @@ (function(){ var instructions = "see uncompressed file for instructions"; //SETTINGS var defaultThreshold = 800; @@ -96,6 +95,5 @@ }); } })(); </script> -
Polyducks revised this gist
Dec 5, 2016 . 1 changed file with 28 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,9 +4,8 @@ <script> /* RESPONSIVE-IMAGES ._______________________. .________. || || /| |\ | | || || / '----' \ | | @@ -15,14 +14,30 @@ || || \ ,----, / | | || || \| |/ | | ;L____________________(); ;___()___; Just add these attributes to your img tag(s): mobile image source: data-mob-src="www.x.com/y/mobile.jpg" desktop image source: data-desk-src="www.x.com/y/desktop.jpg" optional responsive threshold: data-threshold="900" <img data-mob-src="mobile.png" data-desk-src="desktop.png" data-threshold="900" /> Then include this js at the foot of the page. */ (function(){ var instructions = "see original file for commented instructions"; //SETTINGS var defaultThreshold = 800; //ELEMENTS //all img tags with data-mob-src var imageList = Array.prototype.slice.call( document.querySelectorAll("img[data-mob-src]") ); @@ -37,16 +52,16 @@ dataArray[3] = "unset"; //unset / mobile / desktop imgDatabase.push( dataArray ); } //VARIABLES var windowWidth; ImageRespond(); //FUNCTIONS function Check_Window_Size(){ return window.innerWidth; } //SWITCH BETWEEN IMAGE SOURCES function ImageRespond(){ windowWidth = Check_Window_Size(); @@ -64,15 +79,15 @@ } } } if (imageList.length > 0){ //WINDOW RESIZE EVENT LISTENER var resizeCheckPause = false; window.addEventListener("resize", function(){ if (!resizeCheckPause){ ImageRespond(); resizeCheckPause = true; window.requestAnimationFrame(function(){ resizeCheckPause = false; -
Polyducks revised this gist
Dec 5, 2016 . 1 changed file with 29 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,21 @@ <script> /* RESPONSIVE-IMAGES ._______________________. .________. || || /| |\ | | || || / '----' \ | | || || / \ | | || DESKTOP || \ / | MOBILE | || || \ ,----, / | | || || \| |/ | | ;L____________________(); ;___()___; */ (function(){ //SETTINGS @@ -51,19 +65,21 @@ } } if (imageList.length > 0){ //WINDOW RESIZE EVENT LISTENER var resizeCheckPause = false; window.addEventListener("resize", function(){ if (!resizeCheckPause){ ImageRespond(); resizeCheckPause = true; window.requestAnimationFrame(function(){ resizeCheckPause = false; }); } }); } })(); -
Polyducks created this gist
Dec 5, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ <img data-mob-src="mobile.png" data-desk-src="desktop.png" data-threshold="900" /> <img data-mob-src="mobile.png" data-desk-src="desktop.png" /> <script> //on resize (function(){ //SETTINGS var defaultThreshold = 750; //ELEMENTS //all img tags with data-mob-src var imageList = Array.prototype.slice.call( document.querySelectorAll("img[data-mob-src]") ); //makes searching easier var imgDatabase = []; for ( var i = 0; i < imageList.length; i++ ){ var dataArray = []; dataArray[0] = imageList[i].getAttribute("data-threshold") || defaultThreshold; dataArray[0] = parseInt(dataArray[0]); dataArray[1] = imageList[i].getAttribute("data-mob-src"); dataArray[2] = imageList[i].getAttribute("data-desk-src"); dataArray[3] = "unset"; //unset / mobile / desktop imgDatabase.push( dataArray ); } //VARIABLES var windowWidth; ImageRespond(); //FUNCTIONS function Check_Window_Size(){ return window.innerWidth; } //SWITCH BETWEEN IMAGE SOURCES function ImageRespond(){ windowWidth = Check_Window_Size(); for ( var i = 0; i < imgDatabase.length; i++ ){ if ( imgDatabase[i][0] >= windowWidth ){ if ( imgDatabase[i][3] !== "mobile" ){ imageList[i].src = imgDatabase[i][1]; //set to mobile imgDatabase[i][3] = "mobile"; } }else{ if ( imgDatabase[i][3] !== "desktop" ){ imageList[i].src = imgDatabase[i][2]; //set to desktop imgDatabase[i][3] = "desktop"; } } } } //WINDOW RESIZE EVENT LISTENER var resizeCheckPause = false; window.addEventListener("resize", function(){ if (!resizeCheckPause){ ImageRespond(); resizeCheckPause = true; window.requestAnimationFrame(function(){ resizeCheckPause = false; }); } }); })(); </script>