jQuery 中的图像替换
我无法让它做我想做的事!我正在尝试用移动浏览器的较小图像替换大图像。
这是代码:
function js_imgs() {
if (document.documentElement.clientWidth <= 600) {
$("img").each(function(){
if ($(this).hasClass('big')) {
var preSrc = $(this).attr('src');
var newSrc = preSrc.substring(preSrc.lastIndexOf('/'), preSrc.lastIndexOf('.'));
$(this).attr('src', newSrc + '-m.' + /[^.]+$/.exec(preSrc));
$(this).removeClass('big').addClass('mobile');
}
});
} else {
$("img").each(function(){
if ($(this).hasClass('mobile')) {
var preSrc = $(this).attr('src');
var newSrc = preSrc.substring(preSrc.lastIndexOf('/'), preSrc.lastIndexOf('.'));
$(this).attr('src', newSrc + '.' + /[^.]+$/.exec(preSrc));
$(this).removeClass('mobile').addClass('big');
}
});
}
};
对于第一张图像,此代码运行顺利。不幸的是,所有其他图像的 SRC 都被重写为第一个图像的 SRC。都化作同一个形象;其大小调整得很好,但不是正确的图像。
我做错了什么?我已经尝试了 此线程没有成功。
I can't get this to do what I want it to do! I'm trying to replace large images with smaller ones for mobile browsers.
Here is the code:
function js_imgs() {
if (document.documentElement.clientWidth <= 600) {
$("img").each(function(){
if ($(this).hasClass('big')) {
var preSrc = $(this).attr('src');
var newSrc = preSrc.substring(preSrc.lastIndexOf('/'), preSrc.lastIndexOf('.'));
$(this).attr('src', newSrc + '-m.' + /[^.]+$/.exec(preSrc));
$(this).removeClass('big').addClass('mobile');
}
});
} else {
$("img").each(function(){
if ($(this).hasClass('mobile')) {
var preSrc = $(this).attr('src');
var newSrc = preSrc.substring(preSrc.lastIndexOf('/'), preSrc.lastIndexOf('.'));
$(this).attr('src', newSrc + '.' + /[^.]+$/.exec(preSrc));
$(this).removeClass('mobile').addClass('big');
}
});
}
};
For the first image, this code works swimmingly. Unfortunately, all the other images have their SRCs rewritten as the first image's SRC. They all turn into the same image; which resize wonderfully, but aren't the right image.
What have I done wrong? I've tried almost all of the variations on this in this thread with no success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出去休息了一下,然后回来发现我一直在通过刷新和刷新服务器上过时的副本而不是我在本地主机上编辑的副本来测试我的代码。
对不起!我已经工作太久了。
这是代码的工作版本:
这是为即时切换窗口大小而编写的,这几乎肯定是完全不必要的。也许当在高分辨率 iPhone 或平板电脑上从纵向切换到横向时,额外的代码可能是值得的;我必须做一些测试。
非常欢迎对即时切换的价值进行改进和评论。
Went away for a rest, then came back and discovered I had been testing my code by refreshing and refreshing the out-of-date copy on the server instead of the one I was editing at localhost.
Sorry! I have been working too long.
Here is a working version of the code:
This is written for on-the-fly switching on window resizes, which is almost certain to be entirely unnecessary. Perhaps when switching from portrait to landscape on high-resolution iPhones or tablets, the extra code might be worthwhile; I will have to do some testing.
Improvements and comments on the worthiness of on-the-fly switching more than welcome.
首先我会简化这个功能,不值得验证你是否在移动设备上,因为你第一次进入页面时,它会验证它是否足够大。
它会是这样的:
尽管如此,这项工作可以在服务器端完成得更好,在不同的服务器语言(asp.net、php...)中有一个很棒的脚本来检测它是否是移动设备:
detectmobilebrowser.com/
另一件事,我不太确定宽度(600)是否太大,我会使用 350px,尝试 facebook在移动设备中,它适用于这些尺寸。
Firstly I would simplify the function, it is not worthy to verify if you are in mobile, because the first time you enter the page, it verifies if it is big enough or not.
It would be like this:
Notwithstanding, this work could be done better in serverside, there is a fantastic script in different server languages (asp.net, php...) to detect if it is a mobile:
detectmobilebrowser.com/
Another thing, I am not quite sure if the width (600) is too big, I woulld have use 350px, try facebook in mobile, it works for these dimensions.