javascript - 如何选择页面中的所有图像?

标签 javascript iphone ipad resolution retina-display

我有这个 jQuery 脚本,可以为任何带有标签 data-retina="true" 的图像或对象提供 retina-resolution-imagebackground-图片

但我如何从脚本中删除最后一部分,以便此函数将应用于每张图像,而不仅仅是带有 data-retina 标签的图像?

 (function($) {
    "use strict";

    var retinaReplace = function(element, options) {

        this.options = options;
        var $el = $(element);
        var is_image = $el.is('img');
        var normal_url = is_image ? $el.attr('src') : $el.backgroundImageUrl();
        var retina_url = this.options.generateUrl($el, normal_url);

        $('<img/>').attr('src', retina_url).load(function() {

            if (is_image) {
                $el.attr('src', $(this).attr('src'));
            } else {
                $el.backgroundImageUrl($(this).attr('src'));
                $el.backgroundSize($(this)[0].width, $(this)[0].height);
            }

            $el.attr('data-retina', 'complete');
        });
    }

    retinaReplace.prototype = {
        constructor: retinaReplace
    }

    $.fn.retinaReplace = function(option) {

        if (getDevicePixelRatio() <= 1) { return this; }

        return this.each(function() {
            var $this = $(this);
            var data = $this.data('retinaReplace');
            var options = $.extend({}, $.fn.retinaReplace.defaults, $this.data(), typeof option == 'object' && option);
            if (!data) { $this.data('retinaReplace', (data = new retinaReplace(this, options))); }
            if (typeof option == 'string') { data[option](); }
        });
    }

    $.fn.retinaReplace.defaults = {
        suffix: '-x2', 
        generateUrl: function(element, url) {
            var dot_index = url.lastIndexOf('.');
            var extension = url.substr(dot_index + 1);
            var file = url.substr(0, dot_index);
            return file + this.suffix + '.' + extension;
        }
    }

    $.fn.retinaReplace.Constructor = retinaReplace;

    var getDevicePixelRatio = function() {
        if (window.devicePixelRatio === undefined) { return 1; }
        return window.devicePixelRatio;
    }

    $.fn.backgroundImageUrl = function(url) {
        return url ? this.each(function () { 
            $(this).css("background-image", 'url("' + url + '")')
        }) : $(this).css("background-image").replace(/url\(|\)|"|'/g, "");
    }

    $.fn.backgroundSize = function(retinaWidth, retinaHeight) {
        var sizeValue = Math.floor(retinaWidth/2) + 'px ' + Math.floor(retinaHeight/2) + 'px';
        $(this).css("background-size", sizeValue);
        $(this).css("-webkit-background-size", sizeValue);
    }

    $(function(){
        $("[data-retina='true']").retinaReplace();
    });

})(window.jQuery);

最佳答案

how do I remove the last part from the script so that this function will apply on every image, not just the once with the data-retina-tag?

变化:

$(function(){
    $("[data-retina='true']").retinaReplace();
});

收件人:

$(function(){
    $("img").retinaReplace();
});

关于javascript - 如何选择页面中的所有图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11220606/

相关文章:

iphone - iOS线程问题

javascript - 在 HTML 中正确实现 CSS 和 JS

javascript - jQuery:TypeError 'undefined' 不是一个函数

iphone - 将 youtube channel 加载到 uitableview

iphone - 使用麦克风作为MTAudioProcessingTap的输入

ipad - UITabBarController 中的 UIViewController 和 UISplitViewController shouldAutorotateToInterfaceOrientation

Javascript 从 div 或 span 中选择数据

javascript - asp :XmlDataSource Expected DTD markup was not found. 第 1 行,位置 3

iphone - 不要在自己的静态库中公开已用库中的符号

使用 spritekit 的 iPad 和 iPhone 游戏的 iOS 坐标