用層實現為網頁中的圖添加浮水印的效果

來源:互聯網
上載者:User

首先獲得網頁元素的絕對位置後在相應的位置建立一個遮擋層,可實現一個浮水印樣式。該例可用於為圖片加著作權,或隱藏圖片的等。缺點是改變瀏覽器大小時有頁面有閃爍。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無標題文檔</title>
<script>
 //取得元素(映像)的絕對位置
 function getDim(el)
 {
  for (var lx=0,ly=0; el!=null; lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
  return {x:lx,y:ly}
 }
 //建立遮擋層
 function createLayer(el)
 {
  myImgDim = getDim(el);
 
  document.writeln("<div id='Layer1' style='position:absolute; z-index:1; left: "+myImgDim.x+"px; top: "+myImgDim.y+"px;'>");
  document.writeln("<img src=logo.gif width=38 height=17>");
  document.writeln("</div>");
 }
 //瀏覽器視窗大小改變時重新整理頁面
 window.onresize=function()
 {
  location.reload();
 }
</script>
<link href="j.css" rel="stylesheet" type="text/css">
</head>

<body>
<br>
<br>
<table width="407" height="268" align="center">
  <tr>
    <td align="center"><span id=MySpan><img src="image/logo1.gif" width="150" height="78" id=MyImage></span><span id=MySpan><img src="image/logo1.gif" width="150" height="78" id=MyImage2></span></td>
  </tr></table>

 

<span id=MySpan><img src="image/logo1.gif" width="150" height="78" id=MyImage3></span><br>
<span id=MySpan><img src="image/logo1.gif" width="150" height="78" id=MyImage4></span>
<script>
 createLayer(document.images.MyImage);
 createLayer(document.images.MyImage2);
 createLayer(document.images.MyImage3);
 createLayer(document.images.MyImage4);
</script>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.