當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


script.aculo.us Opacity效果用法及代碼示例


script.aculo.us不透明度效果是一種核心效果,它使我們能夠將元素的不透明度逐漸更改為給定級別。除非定義了‘from’選項,否則此效果從元素的當前不透明度開始,並以‘to’選項定義的不透明度結束,默認不透明度值為1.0。

用法:

new Effect.Opacity('id_of_element', [options]);

範例1:本示例說明如何更改不透明度以顯示或隱藏內容。

HTML

<!DOCTYPE html> 
<html> 
  
<head> 
    <script type="text/javascript" 
        src="./javascript/prototype.js"> 
    </script> 
      
    <script type="text/javascript" 
src="./javascript/scriptaculous.js?load = effects"> 
    </script> 
      
    <script type="text/javascript"> 
        function ShowElement(element) { 
            new Effect.Opacity(element, {  
                duration:1, from:0, to:1.0 }); 
        } 
  
        function HideElement(element) { 
            new Effect.Opacity(element, {  
                duration:1, from:1.0, to:0 }); 
        } 
    </script> 
</head> 
  
<body> 
    <div onclick="ShowElement('element')"> 
        <Button>Show Content</Button> 
    </div> 
    <br /> 
  
    <div onclick="HideElement('element')"> 
        <Button>Hide Content</Button> 
    </div> 
    <br /> 
    <img id="element" src="./gfg.png"> 
</body> 
  
</html>

輸出:



Out

範例2:更改內容的不透明度。

HTML

<!DOCTYPE html> 
<html> 
  
<head> 
    <script type="text/javascript" 
        src="./javascript/prototype.js"> 
    </script> 
      
    <script type="text/javascript" 
src="./javascript/scriptaculous.js?load = effects"> 
    </script> 
      
    <script type="text/javascript"> 
        function ShowElement(element) { 
            new Effect.Opacity(element, {  
                duration:1.5, from:0.5, to:1.0 }); 
        } 
  
        function HideElement(element) { 
            new Effect.Opacity(element, {  
                duration:1.5, from:1.0, to:0.3 }); 
        } 
    </script> 
</head> 
  
<body> 
    <div onclick="ShowElement('element')"> 
        <Button>Show Content</Button> 
    </div> 
    <br /> 
  
    <div onclick="HideElement('element')"> 
        <Button>Fade Content</Button> 
    </div> 
    <br /> 
    <img id="element" src="./gfg.png"> 
</body> 
  
</html>

輸出:

Output

相關用法


注:本文由純淨天空篩選整理自swapnil074大神的英文原創作品 script.aculo.us Opacity Effect。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。