js實現(xiàn)html頁面水印
要在 HTML 頁面中添加水印并防止截圖,可以使用 JavaScript。以下是實現(xiàn)的基本步驟:
1、在 HTML 中添加一個 div 元素作為水印容器,并設(shè)置其樣式。
2、使用 JavaScript 動態(tài)生成水印內(nèi)容,并將其添加到水印容器中。
3、通過 CSS 設(shè)置水印文本的樣式,例如顏色、字體大小等。
4、使用 CSS 將水印容器置于所有其他元素的最頂層,從而覆蓋整個頁面。
5、監(jiān)聽窗口的 resize 和 scroll 事件,以便及時更新水印位置。
6、使用 Canvas 繪制圖片或者使用 CSS 的 mix-blend-mode 屬性來實現(xiàn)防截圖效果。
下面是一個示例代碼片段:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Watermark Example</title>
<style>
#watermark {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 9999;
pointer-events: none;
}
#watermark span {
font-size: 24px;
color: #ccc;
position: absolute;
transform: rotate(-30deg);
padding:100px;
pointer-events: none;
}
</style>
</head>
<body>
<div id="watermark"></div>
<script>
function createWatermark() {
var watermark=document.getElementById('watermark');
var angle=-30;
var top=-30;
var text='My Watermark';
for (var i=0; i < 10; i++) {
var span=document.createElement('span');
span.style.left=i * 200 + 'px';
span.style.top=i * top + 'px';
span.style.webkitTransform='rotate(' + angle + 'deg)';
span.style.MozTransform='rotate(' + angle + 'deg)';
span.style.msTransform='rotate(' + angle + 'deg)';
span.style.OTransform='rotate(' + angle + 'deg)';
span.style.transform='rotate(' + angle + 'deg)';
span.appendChild(document.createTextNode(text));
watermark.appendChild(span);
angle +=15;
top +=15;
}
}
createWatermark();
window.addEventListener('resize', function() {
createWatermark();
});
window.addEventListener('scroll', function() {
createWatermark();
});
// 防截圖
var canvas=document.createElement('canvas');
canvas.width=window.innerWidth;
canvas.height=window.innerHeight;
canvas.style.position='fixed';
canvas.style.top=0;
canvas.style.left=0;
canvas.style.pointerEvents='none';
canvas.style.mixBlendMode='multiply';
document.body.appendChild(canvas);
var ctx=canvas.getContext('2d');
ctx.fillRect(0, 0, canvas.width, canvas.height);
</script>
</body>
</html>
這個示例代碼添加了一個水印容器,并在其中添加了一些旋轉(zhuǎn)的文本。使用 CSS 將水印容器置于最頂層,并禁用了其指針事件,以防止干擾用戶操作。同時在窗口 resize 和 scroll 事件中更新水印位置,以適應(yīng)頁面變化。
如果想保護上面的JavaScript代碼邏輯,可以用JShaman進行JavaScript代碼混淆加密,加密后的代碼不可讀、可起到防分析的作用。
此外,這個示例代碼還使用 Canvas 繪制了一個與頁面大小相同的黑色矩形,并將其與水印容器疊加在一起。由于 mix-blend-mode 屬性的作用,截圖時就無法完整地復(fù)制水印文本,從而達到防截圖的效果。
效果:
端功能問題系列文章,點擊上方合集↑
序言
大家好,我是大澈!
本文約3100+字,整篇閱讀大約需要5分鐘。
本文主要內(nèi)容分三部分,如果您只需要解決問題,請閱讀第一、二部分即可。如果您有更多時間,進一步學(xué)習(xí)問題相關(guān)知識點,請閱讀至第三部分。
感謝關(guān)注微信公眾號:“程序員大澈”,然后加入問答群,從此讓解決問題的你不再孤單!
這里分享一個我平時常用的水波特效步驟,加在按鈕上特好使。
首先,是直接創(chuàng)建一個div盒子,不需要在里面添加其他內(nèi)容,我們直接對盒子本身添加css就能形成水波效果。
html部分,我們div添加白色的波紋,所以在這里設(shè)置html背景為藍色。
<body style="background-color: cadetblue ;">
<div class="video"></div>
</body>
css部分,先設(shè)置好div的基本屬性
.video {
/* 基本屬性 */
width: 100px;
height: 100px;
border-radius: 50px;
/* 給背景顏色添加不透明度 */
/* 不透明度還可以通過添加opacity屬性修改 */
background-color: rgb(255, 255, 255, 0.6);
}
然后就是在video中添加這個特效中重中之重的內(nèi)容,在css中設(shè)置animation。
Animation 是由三部分組成。
.video {
/* 添加ripple動畫效果 */
/* -webkit-animation適配-webkit內(nèi)核的瀏覽器*/
-webkit-animation: ripple 1s linear infinite;
animation: ripple 1s linear infinite;
}
/* 定義ripple動畫效果 */
@-webkit-keyframes ripple {
/* 關(guān)鍵幀播放到0%時的狀態(tài) */
0% {
/* 在box四周添加三層白色陰影 */
box-shadow: 0 0 0 0 rgb(255 255 255 / 25%),
0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%);
}
/* 關(guān)鍵幀播放到100%時的狀態(tài) */
100% {
/* 分別改變?nèi)龑雨幱暗木嚯x
形成兩幀的動畫,然后在transition的過渡下形成動畫 */
box-shadow: 0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%),
0 0 0 40px rgba(50, 100, 245, 0);
}
}
/* 多種瀏覽器兼容性設(shè)置 */
@keyframes ripple {
0% {
box-shadow: 0 0 0 0 rgb(255 255 255 / 25%),
0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%);
}
100% {
box-shadow: 0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%),
0 0 0 40px rgba(50, 100, 245, 0);
}
}
其中,linear是表示動畫的timing-function,其總共大致有以下幾種效果。
圖源水印
為了實現(xiàn)按鈕的響應(yīng)式操作,我們可以給div再加上一個hover選擇器
/* 鼠標(biāo)懸浮時的狀態(tài) */
.video:hover {
/* 背景顏色不透明度變化 */
background-color: #FFFFFF;
/* 將對象放大1.2倍 */
transform: scale(1.2);
}
再給div添加一個transition屬性,讓div在鼠標(biāo)移動的時候能自然過渡,其原理跟animation類似。
.video {
/* 添加動畫的過渡效果 */
transition: all 0.3s ease-in-out;
}
然后就能得到我們的結(jié)果,整體的代碼如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.video {
width: 100px;
height: 100px;
border-radius: 50px;
background-color: rgb(255, 255, 255, 0.6);
transition: all 0.3s ease-in-out;
-webkit-animation適配-webkit內(nèi)核的瀏覽器*/
-webkit-animation: ripple 1s linear infinite;
animation: ripple 1s linear infinite;
}
.video:hover {
background-color: #FFFFFF;
transform: scale(1.2);
}
@-webkit-keyframes ripple {
0% {
/* 在box四周添加三層白色陰影 */
box-shadow: 0 0 0 0 rgb(255 255 255 / 25%),
0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%);
}
100% {
/* 分別改變?nèi)龑雨幱暗木嚯x
形成兩幀的動畫,然后在transition的過渡下形成動畫 */
box-shadow: 0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%),
0 0 0 40px rgba(50, 100, 245, 0);
}
}
@keyframes ripple {
0% {
box-shadow: 0 0 0 0 rgb(255 255 255 / 25%),
0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%);
}
100% {
box-shadow: 0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%),
0 0 0 40px rgba(50, 100, 245, 0);
}
}
</style>
</head>
<body style="background-color: cadetblue ;">
<div class="video"></div>
</body>
</html>
效果圖
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。