(function() { this.isMobile = function(breakpoint, option) { return window.innerWidth < breakpoint; }; this.switchImages = function(breakpoint, option) { // Variable var selector = 'img[src$="_pc.png"], img[src$="_pc.jpg"], img[src$="pc.jpeg"], img[src$="pc.gif"], img[src$="pc.tiff"], img[src$="_sp.png"], img[src$="_sp.jpg"], img[src$="sp.jpeg"], img[src$="sp.gif"], img[src$="sp.tiff"]', flag = ''; // Function function onResize() { if (window.innerWidth < breakpoint && (flag === 'pc' || flag === '')) { flag = 'sp'; $(selector).each(function() { var imgPath = $(this).attr('src').replace(/_pc\.(.*)$/, '_sp.$1'); $(this).attr('src', imgPath); }); } else if (window.innerWidth >= breakpoint && flag === 'sp' || flag === '') { flag = 'pc'; $(selector).each(function() { var imgPath = $(this).attr('src').replace(/_sp\.(.*)$/, '_pc.$1'); $(this).attr('src', imgPath); }); } } // do something (function() { onResize(); $(window).on('resize', function() { onResize(); }); })(); }; }).call(this);