简体   繁体   English

如何获得网站中最上方图片的链接?

[英]How do i get the link of the topmost positioned image in a website?

I'm writing a script which needs the src of the image which is positioned topmost on any website, NOT the FIRST image in the source, but the one positioned the highest. 我正在编写一个脚本,该脚本需要在任何网站上位于最高位置的图像的src,而不是源中的第一张图像,而是位于最高位置的图像。

i tried something really basic, which is, retrieving the first image tag, however this wont work for images positioned by css/jquery/javascript. 我尝试了一些非常基本的操作,即检索第一个图像标签,但是这不适用于css / jquery / javascript定位的图像。

so any idea how i can accomplish this? 所以任何想法我怎么能做到这一点? |----------------- | .....title.... | |------| | |image | <==== link needed | |------| |//text content |Lorem dolor ipsum

I'm not certain about the jQuery reply but I believe that will still only give you relative image coordinate. 我不确定jQuery的答复,但我相信那仍然只会给您相对的图像坐标。 Following this earlier post showing a method to get the absolute x, y coordinates of a html element on a page , and stealing the same method from PrototypeJS , the following code should do what you need, 在此之前的帖子中显示了一种获取页面上html元素的绝对x,y坐标的方法,并从PrototypeJS窃取了相同的方法之后,以下代码应该可以满足您的要求,

Caveats, I think that the 0 top check is safe to use to determine if an image is invisible or not, but it might be problematic. 请注意,我认为可以安全地使用0 top检查来确定图像是否不可见,但这可能是有问题的。 Also, this will only get images inside img tags, not image links or anything set with css. 同样,这只会在img标签中获取图像,而不会获取图像链接或使用CSS设置的任何内容。

// cumulative offset function stolen from PrototypeJS
var cumulativeOffset = function(element) {
    var top = 0, left = 0;
    do {
        top += element.offsetTop  || 0;
        left += element.offsetLeft || 0;
        element = element.offsetParent;
    } while(element);

    return {
        top: top,
        left: left
    };
};

// get all images
var results = document.getElementsByTagName("img");
var images = [];
for (result in results) {
    if (results.hasOwnProperty(result)) {
        images.push(results[result]);
    }
}

// map our offset function across the images
var offsets = images.map(cumulativeOffset);

// pull out the highest image by checking for min offset
// offset of 0 means that the image is invisible (I think...)
var highest = images[0];
var minOffset = offsets[0];
for (i in offsets) {
    if (minOffset.top === 0 ||
            (offsets[i].top > 0 && offsets[i].top < minOffset.top)) {
        minOffset = offsets[i];
        highest = images[i];
    }
}

// highest is your image element
console.log(highest);

You need to compare the .y property of each element. 您需要比较每个元素的.y属性。

function getTopMostImage() {
    //Get all images
    var imgs = document.querySelectorAll("img");

    //Define a variable that marks the topmost image
    var topmost = imgs[0];

    //Check all image positions and mark the topmost
    for (i = 1; i < imgs.length; i++) {
        if (topmost.y > imgs[i].y)
            topmost = imgs[i];
    }
    return topmost.src;
}

加载后,您可以通过first()检查Dom中存在的第一个img元素。文档在这里希望对您有所帮助

// Use the most appropriate selector for your images such as "body img" or bla bla...
var images = jQuery('img');
var topMostImageIndex = 0;

for (var i = 0; i < images.length; i++) {

    if (jQuery(images[i]).position().top < jQuery(images[topMostImageIndex]).position().top) {

        topMostImageIndex = i;
    }
}

// It's the top-most image
images[topMostImageIndex];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM