自动使用页面上所有大图像的Javascript库
我有一个在一个大页面上有很多图片的网站 最简单的是我可以包含的一个脚本,它可以自动搜索同一页面,并使用所有大于100px的图像从中创建幻灯片库 有谁知道这么简单的脚本,不需要任何编程技巧吗 我发现这是一个开始: 要将所有图像放大到一定大小,您可以使用以下方法:自动使用页面上所有大图像的Javascript库,javascript,jquery,image-gallery,Javascript,Jquery,Image Gallery,我有一个在一个大页面上有很多图片的网站 最简单的是我可以包含的一个脚本,它可以自动搜索同一页面,并使用所有大于100px的图像从中创建幻灯片库 有谁知道这么简单的脚本,不需要任何编程技巧吗 我发现这是一个开始: 要将所有图像放大到一定大小,您可以使用以下方法: var allImages = $('img', yourDivElement) var largeImages = allImages.filter(function(){ return ($(this).width() >
var allImages = $('img', yourDivElement)
var largeImages = allImages.filter(function(){
return ($(this).width() > 70) || ($(this).height() > 70)
})
var img = $("img"),
a = "<a href='",
b = "' rel='group' class='fancybox'>";
img.each(function() {
var $this = $(this);
if ($this.width() > 50 && $this.height() > 50) {
$this.wrap(a + $this.attr("src") + b);
}
});
更新: 经过进一步的研究,我发现这是最合适的: 应在本页上实施:
这真的取决于你最喜欢的灯箱(“画廊开幕式”)。让我们说你喜欢。它需要
rel=“shadowbox[库名]”
,其中库名是可选的。shadowbox有趣的一面是,lightbox
而不是shadowbox
也可以工作
然后需要做的是在具有此rel属性的图像周围添加链接标记
var img = $("img"),
a = "<a href='",
b = "' rel='lightbox[",
galName = "chooseName",
c = "]'>";
img.each(function() {
var $this = $(this);
if ($this.width() > 100 || $this.height() > 100) {
$this.wrap(a + $this.attr("src") + b + galName + c);
}
});
var img=$(“img”),
a=“.@Bram Vanroy几乎是正确的,但您需要注意实际大小(不受CSS影响)和未加载的图像(这就是为什么我的过滤器需要回调以返回过滤后的图像):
$(函数(){
$('img').filterBiggerThan(100,函数(大){
console.log(大);
});
});
$.fn.filterBiggerThan=函数(限制,回调){
var-imgs=[];
var last=this.length-1;
这是每个功能(i){
var original=$(本);
变量img=$('
这里有另一个例子:
正如布拉姆建议的那样,这里有一个Fancybox画廊:
没有任何东西可以阻止您使用所需的标记包装图像(您已经找到的图像),并将它们传递给fancybox
:
largeImages.each(function(){
$(this).wrap('<a></a>').parent().attr({'rel':'gallery', href: this.src});
});
$('a[rel=gallery]').fancybox();
largeImages.each(函数(){
$(this).wrap('(请注意,我在演示中使用了body
作为查找图像的根元素,您最好在元素中添加一些类/属性,以保存所有要处理的图像,并改用它)。您是否尝试过这样做以获得图像的原始宽度和高度:
// loop through img elements
$('.img-class').each(function(){
// create new image object
image = new Image();
// assign img src attribute value to object src property
image.src = $(this).attr('src');
// function that adds class to image if width and height is greater that 100px
image.onload = function(){
// assign width and height values
var width = this.width,
height = this.height;
// if an image is greater than 100px width and height assign the
// string fancybox to image object className property
image.className = (width > 100 && height > 100) ? 'fancybox' : '';
}
});
谢谢
我是这样解决的:
var allImages = $('img', yourDivElement)
var largeImages = allImages.filter(function(){
return ($(this).width() > 70) || ($(this).height() > 70)
})
var img = $("img"),
a = "<a href='",
b = "' rel='group' class='fancybox'>";
img.each(function() {
var $this = $(this);
if ($this.width() > 50 && $this.height() > 50) {
$this.wrap(a + $this.attr("src") + b);
}
});
我下载了fancybox,并从页面底部添加了以下代码:
50&&$this.height()>50){
$this.wrap(a+this.attr(“src”)+b);
}
});
这只适用于渲染大小。对于css或img标记中指定的ex width。如果较大的图片渲染大小较小,则您的条件不起作用。如果您需要过滤器,请检查我的第一个示例,如果您只需要应用一些lightbox,请转到第二个示例。@rubo77查看coma的解决方案。他是对的ut原来的大小。另外,你需要修改你的网站。表格不是为了布局目的而做的。你是对的,没有考虑过。打得好!(一个电话)我过去在“加载”方面受了很多苦,呵呵。注意,这不允许画廊。一个简单的变量可以解决这个问题。例如var gallery=true;如果(gallery==true)original.parent().attr('rel','gallery-1')
(或类似的内容)。是的,这取决于您要使用的库插件/库。但是您是对的,它们使用rel属性对图像进行分组。@rubo77我已经为您保存了一个工作版本。完全相同的代码。当然,您的代码一般都可以工作,但这并不能解决我的问题。但是谢谢。但是,如果有超过个jquery,我怎么才能加载整个jquery内容呢页面上的一个大图像?以下是解决方案:
var img = $("img"),
a = "<a href='",
b = "' rel='group' class='fancybox'>";
img.each(function() {
var $this = $(this);
if ($this.width() > 50 && $this.height() > 50) {
$this.wrap(a + $this.attr("src") + b);
}
});