-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.srcset.min.js
1 lines (1 loc) · 2.08 KB
/
jquery.srcset.min.js
1
+function(t){"use strict";function i(i,s){return this.each(function(){var r=t(this),n=r.data("srcset"),a=t.extend({},e.DEFAULTS,r.data(),"object"==typeof i&&i);n||r.data("srcset",n=new e(this,a)),"string"==typeof i?n[i](s):a.update&&n.update(s)})}var e=function(i,e){this.options=e,this.$element=t(i),this.init();var s=this;t(window).resize(function(){s.init()})};e.VERSION="0.0.1",e.DEFAULTS={ajax:!0},e.prototype.init=function(){this.detect_display_densitiy(),this.get_current_window_width();this.$element.attr("srcset")&&this.choose_image_and_apply_it()},e.prototype.choose_image_and_apply_it=function(){var i=this,e=this.$element,s=this.$element.attr("srcset").trim(),r=s.split(","),n=[];t(r).each(function(){var t=this.trim(),e=t.split(" ");e=i.parse_set(e),n.push(e)});var a=n.filter(this.filter_sets),o=a[0];if(void 0!==o)this.prepare_swap_src_with_match(o);else{var c=e.attr("data-original-src");void 0!==c&&this.swap_src_with_match(e,c)}},e.prototype.filter_sets=function(t){var i=!1,e=!1,s=parseInt(t[2]),r=parseInt(t[1]);return window.density==s&&(i=!0),window.width<=r&&(e=!0),i===!0&&e===!0?t:i===!0&&isNaN(r)?t:isNaN(s)&&e===!0?t:void 0},e.prototype.prepare_swap_src_with_match=function(i){var e=this.$element,s=i[0];if(this.options.ajax){t.ajax(img_src).success(function(){this.swap_src_with_match(e,s)})}else this.swap_src_with_match(e,s)},e.prototype.swap_src_with_match=function(t,i){var e=t.attr("src"),s=t.attr("data-original-src");void 0===s&&t.attr("data-original-src",e),t.attr("src",i)},e.prototype.detect_display_densitiy=function(){var t=window.devicePixelRatio;return window.density=t,t},e.prototype.get_current_window_width=function(){var i=t(window).outerWidth(!0);return window.width=i,i},e.prototype.parse_set=function(i){var e=(i[0],null),s=null,r=i.slice();return r.splice(0,1),t(r).each(function(){var t=this;t.indexOf("w")>-1&&(e=t.replace("w","")),t.indexOf("x")>-1&&(s=t.replace("x",""))}),i[1]=e,i[2]=s,i},e.prototype.update=function(){console.log("coming soon")};var s=t.fn.srcset;t.fn.srcset=i,t.fn.srcset.Constructor=e,t.fn.srcset.noConflict=function(){return t.fn.srcset=s,this}}(jQuery);