简体   繁体   English

使用JavaScript在其他图像上添加透明图像

[英]Add Transparent Image on top of other Images with JavaScript

I need to add a Transparent image on top of all images on a page. 我需要在页面上的所有图像上添加透明图像。 The goal is if a user were to do a simple right click and save of an image, they would save the transparent image. 目标是如果用户进行简单的右键单击并保存图像,他们将保存透明图像。

I do realize this is not a guaranteed method and that none exist to prevent image theft but simply a measure that a client wants added to prevent your average non tech person from saving images. 我确实意识到这不是一种保证方法,并且没有一种方法可以防止图像被盗,而只是一种客户想要添加的措施,以防止普通的非技术人员保存图像。

  • Using JavaScript I would like to find all images or all images within a certain Div. 使用JavaScript我想找到某个Div中的所有图像或所有图像。
  • Apply a new image overlay on top of these images that will have the same width and height of the image they are covering 在这些图像的顶部应用新的图像叠加,这些图像的宽度和高度与它们所覆盖的图像相同

I am not sure how to do this with JavaScript and was hoping someone would have a quick fix or example. 我不知道如何用JavaScript做这件事,并希望有人能快速修复或举例。 I was unable to find anything so far on Google or SO. 到目前为止,我无法在Google或SO上找到任何内容。 Appreciate any help 感谢任何帮助

I have this JS which gets all images on a page so far... 我有这个JS,到目前为止在页面上获取所有图像...

// Get all images on a Page
function checkimages() {
     var images = document.images;
     for (var i=0; i<images.length; i++){
        var img =images[i].src;

       // Add new transparent image on top of this image
       alert(img);
     }
}

I would advise you to work with jQuery (or similar library) to keep things easier. 我建议你使用jQuery(或类似的库)来保持简单。 I would even write a small jquery extension to make it easy to recycle the code, and apply it on any div (or other wrapper), wich child images you want to be overlayed. 我甚至会写一个小的jquery扩展,以便于回收代码,并将其应用于任何div(或其他包装器),以及要覆盖的子图像。

My code would look something like this: 我的代码看起来像这样:

// jquery plugin to create overlays
// src is the url of the overlay image
// apply to any container that contains images to be overlayed
$.fn.overlayImages = function(src) {
    // loop trough the images
    $(this).find('img').each(function() {
        // cache some variables
        var $img =  $(this);
        var $parent = $img.parent();
        // make the parent relative, if not yet absolute or fixed, for easy positioning
        if ($parent.css('position') !== 'fixed' && $parent.css('position') !== 'absolute') {
            $parent.css('position', 'relative');            
        }
        // get the position of the image
        var position = $img.position();
        // clone the image
        var $overlay = $img.clone();
        // set the styling, based on the img, for exact positioning
        $overlay.css({
            top: position.top,
            left: position.left,
            position: 'absolute',
            width: $img.width(),
            height: $img.height()
        });
        // change the src attribute for the overlay
        $overlay.attr('src', src);
        // insert the overlay to the DOM
        $overlay.insertAfter($img);
    });
}

// when the DOM is loaded (not just ready, the images need to be there to copy their position and size)
$(window).load(function() {
    // apply the overlay plugin to the wrapper of the images
    $('#replace-images').overlayImages("http://www.riptideinnovations.com/images/watermark.png");
});

I added the step by step explanation inside the code as comments, but do feel free to ask if you want any further explanation. 我在代码中添加了逐步解释作为注释,但请随时询问是否需要进一步解释。

I set up a small fiddle to demonstrate: http://jsfiddle.net/pP96f/6/ 我设置了一个小小提琴来演示: http//jsfiddle.net/pP96f/6/

我不知道这是否会有所帮助,但你可以使用这样的背景制作你的图像所有div:

<div style="background-image: url('<your_image>');"></div>

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

相关问题 Babylonjs在彼此之上加载两个图像,第二个图像是透明的 - Babylonjs load two images on top of eachother with the second image being transparent Javascript在彼此之上绘制图像 - Javascript paints images over top of each other 如何在视频顶部添加透明图像叠加层? - How can I add a transparent image overlay on top of a video? 用于跟踪图像并在图像上放置多个图像的JavaScript - javascript to track and place multiple images on top of an image 将7个图像合并为一个,一个在另一个上面并输出为单个图像? - Combine 7 images into one, one on top of each other and output as single image? 如何在JavaScript画布中向透明PNG图像添加笔触/轮廓 - How to add stroke/outline to transparent PNG image in JavaScript canvas 我想使用javascript将图像叠加在其他现有图像之上 - I want to overlay image on top of other existing image using javascript 透明图像始终位于画布顶部。 在透明图像后面绘制 - Transparent image always on top of Canvas. Draw behind transparent image 反转图像存档:使用CSS / Javascript从下到上堆叠图像? - Reverse image archive : stacking images from bottom to top with CSS / Javascript? 如何知道单击了哪个图像,然后知道其他图像中隐藏的透明位置 - How to know which image is clicked, then a transparent place hidden from view other images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM