替换 jQuery 中的图像扩展名,即时从 .png 变为 .gif

发布于 2024-09-10 16:14:41 字数 501 浏览 4 评论 0原文

因此,我使用 CSS3 在我的网站设计中创建侧边栏框,这效果很好而且很漂亮。使用 http://css3pie.com 在 IE 中创建效果。

再说一次,又好又花花公子。现在,我的问题来了 -

对于每个相对定位的模块,其中都有一个绝对定位的图标,将其设置为 top: -20px 以使其具有弹出效果。显然这在 IE6 中不起作用,并且它与任何 PNG 修复代码都不能很好地配合。

不过,对于 IE6 用户,我可以改用低质量的 gif,他们可以处理。

现在,我的问题是,如果用户使用 IE6,如何将 image.png 实时切换为 image.gif?

对于这个想法我还没有找到合理的解决方案。我可以使用 IE6 样式表来替换图像 - 但是在某些情况下图像不应该基于 CSS。它们应该是正确的图像。

有什么建议吗?我想要某种 jQuery 替换,但我也没有找到合理的解决方案。

So, I'm using CSS3 in order to create sidebar boxes in my site design, this works nice and dandy. Using http://css3pie.com in order to create the effect in IE.

Again, nice and dandy. Now, comes my issue -

For each relatively positioned module, there is an absolutely positioned icon within it, that is set to being top: -20px in order to give it a pop-out effect. Obviously this doesn't work in IE6, as well it doesn't play nice with any PNG fix codes.

However, for IE6 users I can switch to a low-quality gif instead, and they can deal with it.

Now, my question is, how can I manage to live-switch image.png to image.gif if the user is using IE6?

I have yet to find a reasonable solution for this idea. I could use an IE6 stylesheet to replace the images - however there are several instances where images shouldn't be CSS-based. They should be proper images.

Any suggestions? I wanted some sort of a jQuery replace, but I haven't found a reasonable solution for that either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天气好吗我好吗 2024-09-17 16:14:41

有几种解决方案。也许最简单的方法是创建一个 jQuery 函数,当用户切换到低保真站点(通过链接)或您代表他们确定这一点(通过标题或条件注释)时调用该函数。

jQuery 函数可以利用属性选择器让生活变得更轻松。

function switch_to_low_fi() {
        $('img[src$=".png"]').each(function(index,element) {
          element.src = element.src.replace('.png','.gif');
        });
}

要切换 CSS 背景,我建议使用 degrade 或类似的东西对它们进行分类,以创建 jQuery 的挂钩,然后执行与上面类似的操作。

        $('.degrade').each(function(index,element) {
          element = $(element);
          var background = element.css('background-image');
          background = background.replace('.png','.gif');
          element.css('background-image', background);
        });

当然,您需要根据您的标记修改上面的代码,但这应该可以帮助您开始。

There are several solutions. Probably the easiest would be to create a jQuery function that was called when the user switches to the low-fi site (by link) or you determine this on their behalf (by headers or conditional comment).

The jQuery function could utilize attribute selectors to make life easier.

function switch_to_low_fi() {
        $('img[src$=".png"]').each(function(index,element) {
          element.src = element.src.replace('.png','.gif');
        });
}

To switch the CSS backgrounds, I would recommend classing them with degrade or something of the sort to create a hook for jQuery, then doing something similar as the above.

        $('.degrade').each(function(index,element) {
          element = $(element);
          var background = element.css('background-image');
          background = background.replace('.png','.gif');
          element.css('background-image', background);
        });

Of course, you will need to modify the code above base on your markup, but that should get you started.

陌上青苔 2024-09-17 16:14:41

这应该适用于页面上的所有非 CSS 图像:

// when DOM is loaded
jQuery(document).ready(function(){
    // and browser is MSIE 6 or lower
    if (jQuery.browser.msie && jQuery.browser.version <= 6) {
        // go through every IMG
        jQuery('img').each(function(index,element){
            // if there's a .png in the source, I'll assume it's at the end
            // (of course, more complex test could be made) ...
            if (element.src.indexOf('.png') != -1) {
                // ...and replace it with a GIF version
                element.src = element.src.replace('.png','.gif'); 
            }
        });
    }
});

This should work for all non-CSS images on the page:

// when DOM is loaded
jQuery(document).ready(function(){
    // and browser is MSIE 6 or lower
    if (jQuery.browser.msie && jQuery.browser.version <= 6) {
        // go through every IMG
        jQuery('img').each(function(index,element){
            // if there's a .png in the source, I'll assume it's at the end
            // (of course, more complex test could be made) ...
            if (element.src.indexOf('.png') != -1) {
                // ...and replace it with a GIF version
                element.src = element.src.replace('.png','.gif'); 
            }
        });
    }
});
别闹i 2024-09-17 16:14:41

您可以在 CSS 中使用以下 PHP 代码来了解正在使用的浏览器:

<?php
$useragent = $_SERVER[‘HTTP_USER_AGENT’]);
if (preg_match(‘|MSIE ([0-9].[0-9]{1,2})|’,$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = ‘IE’;
    echo ... ;//css selector for Internet Explorer and so on...
} elseif (preg_match( ‘|Opera ([0-9].[0-9]{1,2})|’,$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = ‘Opera’;
} elseif(preg_match(‘|Firefox/([0-9\.]+)|’,$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = ‘Firefox’;
} elseif(preg_match(‘|Safari/([0-9\.]+)|’,$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = ‘Safari’;
} else {
// browser not recognized!
$browser_version = 0;
$browser= ‘other’;
}
?>

You can use this PHP code in the CSS to know what browser is being used:

<?php
$useragent = $_SERVER[‘HTTP_USER_AGENT’]);
if (preg_match(‘|MSIE ([0-9].[0-9]{1,2})|’,$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = ‘IE’;
    echo ... ;//css selector for Internet Explorer and so on...
} elseif (preg_match( ‘|Opera ([0-9].[0-9]{1,2})|’,$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = ‘Opera’;
} elseif(preg_match(‘|Firefox/([0-9\.]+)|’,$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = ‘Firefox’;
} elseif(preg_match(‘|Safari/([0-9\.]+)|’,$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = ‘Safari’;
} else {
// browser not recognized!
$browser_version = 0;
$browser= ‘other’;
}
?>
伴梦长久 2024-09-17 16:14:41

您想知道如何替换元素或如何获取浏览器/版本吗?

jQuery 具备这两种功能。

获取浏览器非常简单。只需使用jQuery.browser

要更改图像,CSS 需要采用与 不同的方法。将所有内容设置为使用 PNG,然后执行以下操作:

if ( useGIF() ) {
    var src;
    $('img').each(function() {
        src = $(this).attr('src');
        $(this).attr('src', src.substr(0, src.length-3) + 'gif');
    };
    $('.has_bg_png').each(function() {
        src = $(this).css('background-image');
        $(this).css('background-image', src.substr(0, src.length-3) + 'gif');
    };
}

Do you want to know how to replace the elements or how to get the browser/version?

jQuery has facilities for both.

Getting the browser is straightforward. just use jQuery.browser.

To change the images would require a different approach for CSS vs. <img>. Set everything to use PNG's and then do something like:

if ( useGIF() ) {
    var src;
    $('img').each(function() {
        src = $(this).attr('src');
        $(this).attr('src', src.substr(0, src.length-3) + 'gif');
    };
    $('.has_bg_png').each(function() {
        src = $(this).css('background-image');
        $(this).css('background-image', src.substr(0, src.length-3) + 'gif');
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文