jQuery函数,将改变图像链接
jQuery function that will change image link
我使用srcset为我的图像获得了以下代码。我宁愿完全远离srcset。我想用一个特定的类来处理所有的图像。
<img src="images/Connect-Groups-600.jpg"
srcset="
images/Connect-Groups-600.jpg 600w,
images/Connect-Groups-1000.jpg 1000w,
images/Connect-Groups-1920.jpg 1920w,
images/Connect-Groups-2880.jpg 2880w
" alt=""/>
有谁知道使用jquery的解决方案,将采取标签,并根据屏幕大小改变位置。我需要这个工作在移动设备也
,
- 如果屏幕大于1920w,则使用图片/2880/Connect-Groups.jpg
- 如果屏幕大于1000w1920w或更小,它会拉出图片/1920/Connect-Groups.jpg
- 如果屏幕在601和1000之间,它将拉图片/1000/Connect-Groups.jpg
- 如果屏幕是600w或更小,它拉图像/600/Connect-Groups.jpg
如果大小检测失败,我希望它总是在标签中拉图像。
我需要这个工作,无论什么图像是…我甚至不知道如何寻找解决方案来做到这一点。
编辑
在这结束时,我希望能够把在页面上…然后代码将从img标签中提取URL,并附加正确的大小文件夹。
它最终看起来像最终用户端。
请注意,这种方法是不可持续的,我将不建议使用它用于其他任何事情,但它将帮助您解决这个特定的问题:
假设你的文件夹结构仍然是这样的:
images/<size>
/<filename.ext>
我们可以根据这些信息重新创建一个源:
function resize() {
// First, we'll grab all the images on the page that can be changed:
var img = document.querySelectorAll('img.resizable');
for(var i = 0; i < img.length; i++) {
// Grab the image extension
var src = img[i].src.split('/')
if (window.innerWidth > 1920) {
img[i].src = "/images/2880/" + src[src.length - 1]; // Add the file name and extension
}
else if (window.innerWidth > 1000 && window.innerWidth <= 1920) {
img[i].src = "/images/1920/" + src[src.length - 1]; // Add the file name and extension
}
else if (window.innerWidth > 601 && window.innerWidth <= 1000) {
img[i].src = "/images/1000/" + src[src.length - 1]; // Add the file name and extension
}
else if (window.innerWidth <= 600) {
img[i].src = "/images/600/" + src[src.length - 1]; // Add the file name and extension
}
}
}
resize(); // execute when document is loaded to insert manual images for the first time without resizing
当然,加载基本的600宽度文件作为默认值。
jQuery示例:
function resize() {
var img = $('img.resizable');
var width = $(window).innerWidth();
img.each(function(index, element) {
var name = element.src.split('/') // Split is a native javascript function, which 'splits' the string into an array with the components
if(width <= 600) {
$(element).attr('src', 'images/600/' + name[name.length - 1]) // This name[name.length -1] trick is being used to select the 'last value in the string' based on the length of the string.
}
else if(width <= 1000) {
$(element).attr('src', 'images/1000/' + name[name.length - 1])
}
else if(width <= 1920) {
$(element).attr('src', 'images/1920/' + name[name.length - 1])
}
else {
$(element).attr('src', 'images/2880/' + name[name.length - 1])
}
})
}
resize();
与Muhammad的答案几乎相同,但使用了jQuery。如果你想按类做,只需删除'#'并添加'.'
window.onresize = resize;
function resize() {
console.log($(window).innerWidth);
if ($(window).innerWidth > 1920) {
$("#imageid").src = "images/2880/Connect-Groups.jpg";
} else if ($(window).innerWidth > 1000 && $(window).innerWidth <= 1920) {
$("#imageid").src = "images/1920/Connect-Groups.jpg";
} else if ($(window).innerWidth > 601 && $(window).innerWidth <= 1000) {
$("#imageid").src = "images/1000/Connect-Groups.jpg";
} else if ($(window).innerWidth <= 600) {
$("#imageid").src = "images/600/Connect-Groups.jpg";
}
}
<img id="imageid">
[Modified]设这是你的图像标签
<img class="imageid" src="images/Connect-Groups-600.jpg">
那么这就是你要找的js希望
var images = document.getElementsByClassName("imageid");
var sizes = ["images/2880/Connect-Groups.jpg","images/1920/Connect-Groups.jpg","images/1000/Connect-Groups.jpg","images/600/Connect-Groups.jpg"];
window.onresize = resize;
function resize() {
console.log(window.innerWidth);
if (window.innerWidth > 1920) {
images[0].src = sizes[0];
} else if (window.innerWidth > 1000 && window.innerWidth <= 1920) {
images[0].src = sizes[1];
} else if (window.innerWidth > 601 && window.innerWidth <= 1000) {
images[0].src = sizes[2];
} else if (window.innerWidth <= 600) {
images[0].src = sizes[3];
}
}
<img id="imageid" src="images/Connect-Groups-600.jpg">
相关文章:
- Javascript图像链接
- 更改标记.js图像/链接表达式
- 如何获取显示在谷歌搜索右侧的图像链接
- 来自流星模板的 HTML 图像链接未由浏览器呈现
- 警报后转到 iframe 中的图像链接
- JavaScript渐变幻灯片-图像链接
- 使用JavaScript从网页复制HTML代码或图像链接
- 响应图像tinymce图像链接
- 谷歌图书预览没有在安全的https上显示图书图像链接,但在http上工作.在Google Chrome和Touch浏览器
- 如何在这个javascript中添加图像链接
- 悬停图像链接,更改文本css
- 如何告诉 iframe 将图像链接调整为给定的宽度和高度
- jQuery - 在其下方显示每个图像链接
- 如何将图像链接与 HTML 和 CSS 一起使用时,悬停时在其他位置显示图像
- 如何在鼠标悬停在另一个图像/链接上时显示新图像/窗口
- 如何在数据库值达到一定数量后禁用图像链接
- 当我们单击多个图像链接时,将在同一页面上显示多个工具提示
- 用于在网页上填充图像链接的 JavaScript 代码
- 在.js文件中写入图像链接 - Symfony2
- 如何加载带有图像链接的 XML 文件,以便在 2D JavaScript 数组中使用