纯JS半透明Tip效果

本文介绍了一个使用纯JS实现的半透明提示框效果,包括动态改变元素透明度及位置的方法。通过自定义函数实现了不同浏览器下的一致效果。
自己根据网上提供的一个透明功能类库写的纯JS半透明Tip效果
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Test</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <script type="text/ecmascript">
  7.     function opacity(id, opacStart, opacEnd, millisec) {
  8.   //speed for each frame
  9.   var speed = Math.round(millisec / 100);
  10.   var timer = 0;
  11.   //determine the direction for the blending, if start and end are the same nothing happens
  12.   if(opacStart > opacEnd) {
  13.   for(i = opacStart; i >= opacEnd; i--) {
  14.   setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
  15.   timer++;
  16.   }
  17.   } else if(opacStart < opacEnd) {
  18.   for(i = opacStart; i <= opacEnd; i++)
  19.   {
  20.   setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
  21.   timer++;
  22.   }
  23.   }
  24. }
  25. //change the opacity for different browsers
  26. function changeOpac(opacity, id) {
  27.     var obj = document.getElementById(id);
  28.     if (obj) {
  29.         var s = obj.style;
  30.         s.opacity = (opacity / 100);
  31.         s.MozOpacity = (opacity / 100);
  32.         s.KhtmlOpacity = (opacity / 100);
  33.         s.filter = "alpha(opacity=" + opacity + ")";
  34.         s = null;
  35.     }
  36. }
  37. function shiftOpacity(id, millisec) {
  38.   //if an element is invisible, make it visible, else make it ivisible
  39.   if(document.getElementById(id).style.opacity == 0) {
  40.   opacity(id, 0, 100, millisec);
  41.   } else {
  42.   opacity(id, 100, 0, millisec);
  43.   }
  44. }
  45. function blendimage(divid, imageid, imagefile, millisec) {
  46.   var speed = Math.round(millisec / 100);
  47.   var timer = 0;
  48.   //set the current image as background
  49.   document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
  50.   //make image transparent
  51.   changeOpac(0, imageid);
  52.   //make new image
  53.   document.getElementById(imageid).src = imagefile;
  54.   //fade in image
  55.   for(i = 0; i <= 100; i++) {
  56.   setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
  57.   timer++;
  58.   }
  59. }
  60. function currentOpac(id, opacEnd, millisec) {
  61.   //standard opacity is 100
  62.   var currentOpac = 100;
  63.   //if the element has an opacity set, get it
  64.   if(document.getElementById(id).style.opacity < 100) {
  65.   currentOpac = document.getElementById(id).style.opacity * 100;
  66.   }
  67.   //call for the function that changes the opacity
  68.   opacity(id, currentOpac, opacEnd, millisec)
  69. }
  70. function showContent(i, event){
  71.     showid = "content" + i;
  72.     var target = document.getElementById(showid);
  73.     target.style.position = "absolute";
  74.     if(navigator.appName!="Netscape"){
  75.         event=window.event;
  76.         event.srcElement.style.fontWeight = "700";
  77.     } else {
  78.         event.target.style.fontWeight = "700";
  79.     }
  80.     target.style.top = event.clientY + 22 +"px";
  81.     target.style.left = event.clientX + 12 + "px";
  82.     //复制一个背景
  83.     var bg = target.cloneNode(true);
  84.     if (bg) {
  85.         bg.id="bg1";
  86.         if (bg.style.backgroundColor.length==0) {
  87.             bg.style.backgroundColor ="#FFFFE1";
  88.         }
  89.         bg.style.filter = "alpha(opacity=0)";
  90.         bg.style.opacity = 0;
  91.         target.parentNode.appendChild(bg);
  92.         
  93.         opacity("bg1", 0, 90, 300);
  94.         bg.style.display="block";
  95.     }
  96.     target.style.display = "block";
  97. }
  98. function hiddenContent(i, event){
  99.     if(navigator.appName!="Netscape"){
  100.         event=window.event;
  101.         event.srcElement.style.fontWeight = "400";
  102.     } else {
  103.         event.target.style.fontWeight = "400";
  104.     }
  105.     hiddenid = "content" + i;
  106.     document.getElementById(hiddenid).style.display = "none";
  107.     var bg = document.getElementById("bg1");
  108.     if (bg) {
  109.         bg.parentNode.removeChild(bg);
  110.     }
  111. }
  112. </script>
  113. <style type="text/css">
  114.     p{text-indent:2em}
  115. </style>
  116. </head>
  117. <body>
  118. <a href="#" onmouseover="showContent(0, event)" onmouseout="hiddenContent(0, event)"
  119.     style="cursor:default">链接</a>
  120. <div id="content0" style="width:200px;height:300px;padding:2em;display:none"> 这是文章标题1,文字是不透明的
  121. </div>
  122. <span onmouseover="showContent(1, event)" onmouseout="hiddenContent(1, event)"
  123.     style="cursor:default">有提示文字</span>
  124. <div id="content1" style="width:200px;height:300px;padding:2em;display:none;font-weight:700"> 这是文章标题2,文字是不透明的
  125. </div>
  126. <div style="width:34%;color:#226">
  127. <p>教育是开启完全不同未来大门的重要钥匙。在美国十大城市中,要求 
  128. 中学以下文化程度的职位数从 1970 年起已经减少了一半。从 1989 年起, 
  129. 美国新设职位的 2/3 都是专业人员和管理人员(38)。在德国,到 2010 年 
  130. 将只有 10%的职位适合非技术工人,而在 1976 年为 35%。(39) </p>
  131. <p>但这并不只是一个失业问题。失业的年轻人倾向于从事更多的暴力犯 
  132. 罪,并不愿担当家长的责任。“青春期男孩是最反复无常并最具暴力的了。 
  133. 美国一半的暴力犯罪是由 24 岁以下的男孩干的,四分之一的暴力犯罪事 
  134. 件是由 18 岁以下男孩干的。大多数西方国家的数据也与美国相似。”</p>
  135. </div>
  136. </body>
  137. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值