首页>>Jquery图片>>JS实现图片倒影效果 给图片加上倒影(2014-05-18)

JS实现图片倒影效果 给图片加上倒影

 本例不需要jquery库

JS实现图片倒影效果 给图片加上倒影
赞赏支持
立刻微信赞赏支持 关闭

 

XML/HTML Code
  1. <center>  
  2. <img class="reflect" src="../freejs.jpg" />  
  3. <br />  
  4. 加效果的  
  5. <br />  
  6. <img src="../freejs.jpg" />  
  7. <br />  
  8.  没效果的  
  9. <br /><br />  
  10. 用法很简单,给你需要加倒影效果的图片加上 class="reflect" ,加了就有效果,没加就没有!  
  11. </center>  

 

JavaScript Code
  1. /* From prototype.js */  
  2. if (!document.myGetElementsByClassName) {  
  3.     document.myGetElementsByClassName = function(className) {  
  4.         var children = document.getElementsByTagName('*') || document.all;  
  5.         var elements = new Array();  
  6.         
  7.         for (var i = 0; i < children.length; i++) {  
  8.             var child = children[i];  
  9.             var classNames = child.className.split(' ');  
  10.             for (var j = 0; j < classNames.length; j++) {  
  11.                 if (classNames[j] == className) {  
  12.                     elements.push(child);  
  13.                     break;  
  14.                 }  
  15.             }  
  16.         }  
  17.         return elements;  
  18.     }  
  19. }  
  20.   
  21. var Reflection = {  
  22.     defaultHeight : 0.5,  
  23.     defaultOpacity: 0.5,  
  24.       
  25.     add: function(image, options) {  
  26.         Reflection.remove(image);  
  27.           
  28.         doptions = { "height" : Reflection.defaultHeight, "opacity" : Reflection.defaultOpacity }  
  29.         if (options) {  
  30.             for (var i in doptions) {  
  31.                 if (!options[i]) {  
  32.                     options[i] = doptions[i];  
  33.                 }  
  34.             }  
  35.         } else {  
  36.             options = doptions;  
  37.         }  
  38.       
  39.         try {  
  40.             var d = document.createElement('div');  
  41.             var p = image;  
  42.               
  43.             var classes = p.className.split(' ');  
  44.             var newClasses = 'zxx_add_class';  
  45.             for (j=0;j<classes.length;j++) {  
  46.                 if (classes[j] != "reflect") {  
  47.                     if (newClasses) {  
  48.                         newClasses += ' '  
  49.                     }  
  50.                       
  51.                     newClasses += classes[j];  
  52.                 }  
  53.             }  
  54.   
  55.             var reflectionHeight = Math.floor(p.height*options['height']);  
  56.             var divHeight = Math.floor(p.height*(1+options['height']));  
  57.               
  58.             var reflectionWidth = p.width;  
  59.               
  60.             if (document.all && !window.opera) {  
  61.                 /* Fix hyperlinks */  
  62.                 if(p.parentElement.tagName == 'A') {  
  63.                     var d = document.createElement('a');  
  64.                     d.href = p.parentElement.href;  
  65.                 }    
  66.                       
  67.                 /* Copy original image's classes & styles to div */  
  68.                 d.className = newClasses;  
  69.                 p.className = 'reflected'; 
  70.                  
  71.                 d.style.cssText = p.style.cssText; 
  72.                 p.style.cssText = 'vertical-align: bottom'; 
  73.              
  74.                 var reflection = document.createElement('img'); 
  75.                 reflection.src = p.src; 
  76.                 reflection.style.width = reflectionWidth+'px'; 
  77.                 reflection.style.display = 'block'; 
  78.                 reflection.style.height = p.height+"px"; 
  79.                  
  80.                 reflection.style.marginBottom = "-"+(p.height-reflectionHeight)+'px'; 
  81.                 reflection.style.filter = 'flipv progid:DXImageTransform.Microsoft.Alpha(opacity='+(options['opacity']*100)+', style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy='+(options['height']*100)+')'; 
  82.                  
  83.                 d.style.width = reflectionWidth+'px'; 
  84.                 d.style.height = divHeight+'px'; 
  85.                 p.parentNode.replaceChild(d, p); 
  86.                  
  87.                 d.appendChild(p); 
  88.                 d.appendChild(reflection); 
  89.             } else { 
  90.                 var canvas = document.createElement('canvas'); 
  91.                 if (canvas.getContext) { 
  92.                     /* Copy original image's classes & styles to div */  
  93.                     d.className = newClasses;  
  94.                     p.className = 'reflected';  
  95.                       
  96.                     d.style.cssText = p.style.cssText;  
  97.                     p.style.cssText = 'vertical-align: bottom';  
  98.               
  99.                     var context = canvas.getContext("2d");  
  100.                   
  101.                     canvas.style.height = reflectionHeight+'px';  
  102.                     canvas.style.width = reflectionWidth+'px';  
  103.                     canvas.height = reflectionHeight;  
  104.                     canvas.width = reflectionWidth;  
  105.                       
  106.                     d.style.width = reflectionWidth+'px';  
  107.                     d.style.height = divHeight+'px';  
  108.                     p.parentNode.replaceChild(d, p);  
  109.                       
  110.                     d.appendChild(p);  
  111.                     d.appendChild(canvas);  
  112.                       
  113.                     context.save();  
  114.                       
  115.                     context.translate(0,image.height-1);  
  116.                     context.scale(1,-1);  
  117.                       
  118.                     context.drawImage(image, 0, 0, reflectionWidth, image.height);  
  119.       
  120.                     context.restore();  
  121.                       
  122.                     context.globalCompositeOperation = "destination-out";  
  123.                     var gradient = context.createLinearGradient(0, 0, 0, reflectionHeight);  
  124.                       
  125.                     gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");  
  126.                     gradient.addColorStop(0, "rgba(255, 255, 255, "+(1-options['opacity'])+")");  
  127.           
  128.                     context.fillStyle = gradient;  
  129.                     context.rect(0, 0, reflectionWidth, reflectionHeight*2);  
  130.                     context.fill();  
  131.                 }  
  132.             }  
  133.         } catch (e) {  
  134.         }  
  135.     },  
  136.       
  137.     remove : function(image) {  
  138.         if (image.className == "reflected") {  
  139.             image.className = image.parentNode.className;  
  140.             image.parentNode.parentNode.replaceChild(image, image.parentNode);  
  141.         }  
  142.     }  
  143. }  
  144.   
  145. function addReflections() {  
  146.     var rimages = document.myGetElementsByClassName('reflect');  
  147.     for (i=0;i<rimages.length;i++) {  
  148.         var rheight = null;  
  149.         var ropacity = null;  
  150.           
  151.         var classes = rimages[i].className.split(' ');  
  152.         for (j=0;j<classes.length;j++) {  
  153.             if (classes[j].indexOf("rheight") == 0) {  
  154.                 var rheight = classes[j].substring(7)/100;  
  155.             } else if (classes[j].indexOf("ropacity") == 0) {  
  156.                 var ropacity = classes[j].substring(8)/100;  
  157.             }  
  158.         }  
  159.           
  160.         Reflection.add(rimages[i], { height: rheight, opacity : ropacity});  
  161.     }  
  162. }  
  163.   
  164. var previousOnload = window.onload;  
  165. window.onload = function () { if(previousOnload) previousOnload(); addReflections(); }  

 


原文地址:http://www.freejs.net/article_jquerytupiantexiao_339.html