媒体查询图像文件名随屏幕尺寸变化

发布于 2024-09-13 13:46:14 字数 1587 浏览 7 评论 0原文

我一直在考虑在网站上使用媒体查询,但突然想到下载一个大文件,因为通过 3G 在 iPhone 上下载需要很长时间。

当在 iphone / 上网本 / ipad 上查看网站时,是否可以使用 jquery 更改文件名(即,将 -iphone 添加到 background.jpg 来制作 background-iphone.jpg),也许也使用其中的媒体查询。

在我看来这应该是可能的。如果有人也能解决这个问题,那就太好了:-D

提前谢谢你

我已经找到了这个用于查找屏幕尺寸的代码

$(document).ready(function() {

if ((screen.width>=1024) && (screen.height>=768)) {
alert('Screen size: 1024x600 or larger');
$("link[rel=stylesheet]:not(:first)").attr({href : "detect1024.css"});
}
else  {
alert('Screen size: less than 1024x768, 800x600 maybe?');
$("link[rel=stylesheet]:not(:first)").attr({href : "detect800.css"});
}
});

,并且我找到了这个脚本,我认为它添加了文本来记录

$('.rollover').hover(
        function(){ // Change the input image's source when we "roll on"
            var t = $(this);
            t.attr('src',t.attr('src').replace(/([^.]*)\.(.*)/, "$1-over.$2"));
        },
        function(){ 
            var t= $(this);
            t.attr('src',t.attr('src').replace('-mobile',''));
        }
 );

我如何可以的任何想法将两者结合起来??

也许是这样的 -

$(document).ready(function() {

if ((screen.width>=1024) && (screen.height>=768)) {
var t= $(this);
t.atter('src',t.attr('src').replace('-mobile',''))
}
);

但不知道如何修复语法错误

或者也许是这样 - 但仍然有结束语法错误

$(document).ready(function() {

if ((screen.width>=1024) && (screen.height>=768)) {

   var re = new RegExp("(.+)_hover\\.(gif|png|jpg)", "g");
   return filename.replace(re, "$1.$2");
}

I've been looking at using media queries on a site but bolt at the idea of downloading a huge file only for it take ages to download on a iphone over 3G.

Is it possible using jquery to alter a filename (ie. add -iphone to background.jpg to make background-iphone.jpg) when the site is viewed on an iphone / netbook / ipad perhaps using media queries in there too.

In my head it seems like it should be possible. It'd be great if anyone out there could work it out too :-D

thanks in advance

stu

I've found this code for finding screen sizes

$(document).ready(function() {

if ((screen.width>=1024) && (screen.height>=768)) {
alert('Screen size: 1024x600 or larger');
$("link[rel=stylesheet]:not(:first)").attr({href : "detect1024.css"});
}
else  {
alert('Screen size: less than 1024x768, 800x600 maybe?');
$("link[rel=stylesheet]:not(:first)").attr({href : "detect800.css"});
}
});

and I've found this script which I think adds text to file

$('.rollover').hover(
        function(){ // Change the input image's source when we "roll on"
            var t = $(this);
            t.attr('src',t.attr('src').replace(/([^.]*)\.(.*)/, "$1-over.$2"));
        },
        function(){ 
            var t= $(this);
            t.attr('src',t.attr('src').replace('-mobile',''));
        }
 );

any ideas how I can combine the two ??

maybe something like this -

$(document).ready(function() {

if ((screen.width>=1024) && (screen.height>=768)) {
var t= $(this);
t.atter('src',t.attr('src').replace('-mobile',''))
}
);

but don't know how to fix syntax errors

Or maybe this - still has ending syntax error though

$(document).ready(function() {

if ((screen.width>=1024) && (screen.height>=768)) {

   var re = new RegExp("(.+)_hover\\.(gif|png|jpg)", "g");
   return filename.replace(re, "$1.$2");
}

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

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

发布评论

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

评论(2

゛清羽墨安 2024-09-20 13:46:14

这是可能的,而且实现起来也不难。但为了简单起见,为什么不使用类似:

$(document).ready(function() {

  if ((screen.width<=960) && (screen.height<=640)) {

    $("#someid").addClass("iphone");

  }

});

这样,当浏览器检测到等于或低于 960x640(iPhone 4 分辨率)的分辨率时,它会自动向您指定的任何元素添加“iphone”类。这可以是正文标签、自定义 ID 等。

然后您需要做的就是为“iphone”类指定不同的背景图像。

简单的。

It is possible, and also not too hard to achieve. But for simplicity's sake, why not use something like:

$(document).ready(function() {

  if ((screen.width<=960) && (screen.height<=640)) {

    $("#someid").addClass("iphone");

  }

});

That way, when the browser detects a resolution equal to or lower than 960x640 (iPhone 4 resolution), it will automatically add a class of "iphone" to any element you specify. This could be the body tag, a custom ID and so on.

All you need to do then is specify a different background image for the class "iphone".

Simple.

吃不饱 2024-09-20 13:46:14

或者对于允许您使用图像标签的解决方案,请参见下文:

$(document).ready(function() {

  if ((screen.width<=960) && (screen.height<=640)) {

    $("img").attr("src", $("img").attr("src").replace(/([^.]*)\.(.*)/, "$1-iphone.$2"));

  }

});

这应该可以解决问题。

Or for a solution allowing you to use image tags, see below:

$(document).ready(function() {

  if ((screen.width<=960) && (screen.height<=640)) {

    $("img").attr("src", $("img").attr("src").replace(/([^.]*)\.(.*)/, "$1-iphone.$2"));

  }

});

That should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文