javascript - 使用 JQuery 查找和复制图像

标签 javascript jquery image

我正在尝试使用 JQuery 使用文章中存在的图像创建动态页眉。我打算给图像一个类 (.thumbnail)。一旦用于标题,我想从文章中删除图像。这是逻辑:

  • 使用 .thumbnail 类查找 IMG 标签
  • 将该图像移动到新的 DIV (#dynHeader),将其分类为 .Image1
  • 将图像缩放到 x 像素的高度,保持宽度不变
  • 找到新缩放图像的宽度 (var remainWidth)
  • 计算#dynHeader的剩余宽度
  • 将IMG复制到.Image1右边,命名为.Image2
  • 将其宽度设置为remainWidth的值
  • 裁剪到 .Image1 的高度
  • 将其定位在具有特定值的 Y 轴上

我需要知道如何查找和复制 IMG .thumbnail。我敢肯定,随着我继续解决这个问题,会出现更多问题,但我完全被困住了。我在想这个错误吗?有没有办法在页面上两次放置相同的图像?

感谢您的帮助。

-亚历克斯

编辑:我将解决方案发布在我的网站上,供可能遇到此问题的其他人使用。从答案中获取代码并对其进行调整以使其正常运行。

 //need to clone before removing class so that the original is deletable later.
    var cache = $('img.thumbnail').clone().removeClass('thumbnail').addClass('Image1').css('float','left');
//remove the original from the text         
            $('img.thumbnail').remove();
//attach the cloned image to the header
            $('#dynHeader').append(cache);
//find the ratio
            var ratio = (cache.width() / cache.height());
//set variable to hold image height
            var imageHeight = (365);
//set variable to hold image width 
            var imageWidth = (imageHeight*ratio);
//set the image height & width
            cache.height(imageHeight).width(imageWidth);
//figure the amount of width remaining in the header
            var remainWidth = $('#dynHeader').width() - imageWidth;
//clone the header image
            var dupe = cache.clone();
//work with the cloned image - change the class to Image2, set the width and height         dupe.removeClass('Image1').addClass('Image2').css("width",remainWidth+"px").css("height","auto");
//place Image2 to the right of Image1
            cache.after(dupe);

然后我使用一些 CSS 来定位 Image2 并 overflow hidden (我正在拍摄的缩放和裁剪效果)。

#dynHeader{
    height: 365px;
    overflow: hidden;
    margin-bottom: 30px;
}
img.Image2{
    position: relative;
    top: -150px;
}

希望这对其他人有帮助!感谢 Alex 指出了正确的方法。

最佳答案

这应该让你开始:(请记住它 100% 离我的头顶和这里很晚......可能有一些拼写错误!)

//Find IMG tag with .thumbnail class:
$('img.thumbnail')

//Move that image to a new DIV (#dynHeader), class it .Image1

// change class before and then grab image
var cache = $('img.thumbnail').removeClass('thumbnail').addClass('Image1').clone();

// remove from context
$('img.thumbnail').remove();

// add it to the div
$('#dynHeader').append(cache);

// Scale the image to x pixels in height, maintain aspect for width
// cache selector
var cache = $('.Image1');

// calculate aspect
var ratio = (cache.width() / cache.height());

// calculate & store width
var remainWidth = (x*ratio);

// scale to x pixels in height
cache.height(x).width(remainWidth);

// Calculate the remaining width of #dynHeader
var remainHeaderWidth = $('#dynHeader').width() - remainWidth;

// Duplicate the IMG to the right of .Image1, call it .Image2
// assuming you mean "duplicate it and add to the right"
var dupe = cache.clone();
dupe.removeClass('Image1).addClass('Image2');

// Set its width to the value of remainWidth
dupe.width(remainHeaderWidth);

// Crop it to the height of .Image1
// not sure about the cropping, here's how you would change it without maintaing aspect
dupe.height(cache.height());

// add it after the first image
cache.after(dupe);

// Position it on the Y axis with a specific value
// cant remember off the top of my head but as far as I know you have to first find its    
// position on the axis and then shift it maybe applying relative css positioning

它肯定可以改进,但应该给你一个大概的想法:)

哦,至于在页面上放置相同的图像,没问题,只需 clone() 元素并将其添加到您想要的位置,次数不限!

关于javascript - 使用 JQuery 查找和复制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2360416/

相关文章:

javascript V8优化和 "leaking arguments"

javascript - 如何通过 POST 传递 Javascript 变量并重定向到新页面?

javascript - 如何用正则表达式匹配特定的字符串?

javascript - react JS : Collapsible sidebar

javascript - 如何批量删除HTML5本地存储?

javascript - 将路径 ('../mywebsite/images/$newname' ) 保存到数据库中

css - 对 div 内的内联图像应用悬停图像效果

javascript - 使用 jQuery 将 img 元素复制到另一个 div

javascript - JSON.stringify 会增加字符串的长度吗?

javascript - 将数据表行子级与通过 foreach 显示的列表一起使用