[英]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. 我确实意识到这不是一种保证方法,并且没有一种方法可以防止图像被盗,而只是一种客户想要添加的措施,以防止普通的非技术人员保存图像。
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.